GraphQL 查询返回 null。但不知道为什么

标签 graphql knex.js sqldatasource apollo-server

我在 graphQL 查询返回 null 时遇到了一些问题,并且不太确定为什么它返回 null。我读过几篇与这篇文章类似的文章,但这些文章都没有帮助确定我的确切问题。

我认为这与我传递/获取参数的方式有关,因为我的没有参数的查询工作正常,但我不确定,因为我在网上找不到任何其他示例。

任何帮助将不胜感激。

我正在使用 apollo-server、graphql 和 community SQL datasource implementation它使用 Knex 创建数据库连接。

我可以提出两个问题。

  1. allParts() - 此查询按预期工作,并返回我的数据库中具有请求的 graphQL 字段的所有部分
  2. getPart(itemnum) - 这是当前未运行的查询。

graphQL 查询:

query {
  getPart(itemnum: "T|0000000000001"){
    desc
  }
}

graphQL 响应:

"message": "Cannot return null for non-nullable field Part.desc.",

基于 Knex 调试消息执行的 SQL 查询:

method: 'select',
  options: {},
  timeout: false,
  cancelOnTimeout: false,
  bindings: [ 'T|0000000000001' ],
  __knexQueryUid: '3a8234eb-0a5c-46db-ad8e-5508288c9a86',
  sql: 'select * from `part` where `itemnum` = ?'

index.js:

const { ApolloServer } = require('apollo-server');
const typeDefs = require('./schema');
const resolvers = require ('./resolvers')
const PartAPI = require ('./datasources/partAPI');

const knexConfig = {
  client: "sqlite3",
  connection: {
    /* CONNECTION INFO */
    filename: "./TEAM_material.db3"
  },
  debug: true
};



const server = new ApolloServer({ 
    typeDefs,
    resolvers,
    dataSources: () => ({
      partAPI: new PartAPI(knexConfig),
    }),
    introspection: true,
 });

server.listen().then(({ url }) => {
    console.log(`🚀 Server ready at ${url}`);
  });

partAPI.js:

const { SQLDataSource } = require("datasource-sql");



const MINUTE = 60;

class PartAPI extends SQLDataSource {

    getPart(itemnum){
        return this.knex.select('*').from('part').where({itemnum});
    }

    getAllParts(){
        const query = this.knex.select('*').from('part').cache(MINUTE);
        console.log (query);
        return query;
    }
}

module.exports = PartAPI;

架构.js

// src/schema.js

const { gql } = require('apollo-server')

const typeDefs = gql`
#Types
    type Part {
        itemnum: String!
        desc: String!
        vendor: String!
        manuf: String!
        venlist: Float!
        price: Float!
        teamlist: Float!
        teamsell: Float!
        unitypart: String!
        pkgqty: Int!
        ioType: String!
        preferred: Boolean!
        filterlvl1: String!
        filterlvl2: String!
        filterlvl3: String!
        filterlvl4: String!
        ipwiretype: String!
        opwiretype: String!
        obsolete: Boolean!
    }
#Queries
    type Query {
        getPart(itemnum: String!): Part
        allParts: [Part!]!
    }
`;

module.exports = typeDefs

解析器.js

// src/resolvers.js

const resolvers = {
    Query: {
       getPart: (_,args,{dataSources}) => dataSources.partAPI.getPart(args.itemnum),
       allParts: (_,__,{dataSources}) => dataSources.partAPI.getAllParts(),
      },
};

module.exports = resolvers

最佳答案

事实证明我的架构不正确。 getPart 查询需要一个零件,但我的查询却返回一个零件数组。

旧架构

#Queries
    type Query {
        getPart(itemnum: String!): Part
        allParts: [Part!]!
    }
`;

新架构

#Queries
    type Query {
        getPart(itemnum: String!): [Part]!
        allParts: [Part!]!
    }
`;

关于GraphQL 查询返回 null。但不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60421360/

相关文章:

javascript - Apollo 服务器 - GraphQL 错误 : There can be only one type named "Query"

node.js - 将 Knex 与自己的 pg 连接池一起使用

.net - SqlDataSource 与 ObjectDataSource

c# - 使用 SQLdatasource 的下拉列表层次结构

c# - 必须声明标量变量 "@LoggedInUser"。尝试向 SqlDataSource 添加参数时出错

reactjs - GraphQL错误: Syntax Error: Cannot parse the unexpected character "\u00A0"

javascript - 如何使用 lodash 检查另一个数组中的数组值

python - GitHub GraphQL API 解析 JSON 时出现问题

javascript - Bookshelf.js - 如何仅获取一个查询的 'hidden' 字段?

mysql - 将连接返回到 knex 数据库池