Graphql returning 不能 return 对于不可为 null 的字段 Query.getDate 为 null。由于我是 graphql 的新手,我想知道我的方法是错误的还是我的代码?

Graphql returning Cannot return null for non-nullable field Query.getDate. As I am new to graphql I want to know is my approach is wrong or my code?

我已经创建了解析器、模式和处理程序,它们将从 dynamoDB 中获取一些记录。现在,当我执行查询时,出现 "Cannot return null for non-nullable field Query.getDate" 错误。我想知道我的方法是否错误或代码是否需要更改。

我的代码:https://gist.github.com/vivek-chavan/95e7450ff73c8382a48fb5e6a5b96025

lambda 的输入:

{
  "query": "query getDate {\r\n      getDate(id: \"0f92fa40-8036-11e8-b106-952d7c9eb822@eu-west-1:ba1c96e7-92ff-4d63-879a-93d5e397b18a\") {\r\n        id\r\n        transaction_date\r\n      }\r\n     }"
}

回复:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Query.getDate.",
      "locations": [
        {
          "line": 2,
          "column": 7
        }
      ],
      "path": [
        "getDate"
      ]
    }
  ],
  "data": null
}

lambda 函数的日志:

[ { Error: Cannot return null for non-nullable field Query.getDate.
         at completeValue (/var/task/node_modules/graphql/execution/execute.js:568:13)
         at completeValueCatchingError (/var/task/node_modules/graphql/execution/execute.js:503:19)
         at resolveField (/var/task/node_modules/graphql/execution/execute.js:447:10)
         at executeFields (/var/task/node_modules/graphql/execution/execute.js:293:18)
         at executeOperation (/var/task/node_modules/graphql/execution/execute.js:237:122)
         at executeImpl (/var/task/node_modules/graphql/execution/execute.js:85:14)
         at execute (/var/task/node_modules/graphql/execution/execute.js:62:229)
         at graphqlImpl (/var/task/node_modules/graphql/graphql.js:86:31)
         at /var/task/node_modules/graphql/graphql.js:32:223
         at graphql (/var/task/node_modules/graphql/graphql.js:30:10)
       message: 'Cannot return null for non-nullable field Query.getDate.',
       locations: [Object],
       path: [Object] } ],
  data: null }
2019-02-25T10:07:16.340Z    9f75d1ea-2659-490b-ba59-5289a5d18d73    { Item: 
   { model: 'g5',
     transaction_date: '2018-07-05T09:30:31.391Z',
     id: '0f92fa40-8036-11e8-b106-952d7c9eb822@eu-west-1:ba1c96e7-92ff-4d63-879a-93d5e397b18a',
     make: 'moto' } }

提前致谢!

这是您的代码:

const data = {
  getDate(args) {
    var params = {
        TableName: 'delete_this',
        Key: {
        "id": args.id
        }
    };
    client.get(params, function(err,data){
        if(err){
        console.log('error occured '+err)
        }else{
        console.log(data)
        }
    });
  },
};
const resolvers = {
  Query: {
    getDate: (root, args) => data.getDate(args),
  },
};

您看到该错误是因为 getDate 是架构中的非空字段,但它正在解析为空。您的解析器需要 return 适当类型的值,或将解析为该值的 Promise。如果你像这样改变 data

const data = {
  getDate(args) {
    return {
      id: 'someString',
      transaction_date: 'someString',
    }
  }
}

您会看到错误消失了。当然,您的目标是 return 数据库中的数据,因此我们需要重新添加该代码。但是,您现有的代码使用回调。您在回调中所做的任何事情都是无关紧要的,因为它是 运行 您的解析器函数 return 之后。所以我们需要改用 Promise。

虽然您可以 wrap a callback with Promise,但 aws-sdk 不需要这样做,因为较新的版本支持 Promises。这样的东西应该足够了:

const data = {
  getDate(args) {
    const params = //...
    // must return the resulting Promise here
    return client.get(params).promise().then(result => {
      return {
        // id and transaction_date based on result
      }
    })
  }
}

或使用 async/await 语法:

const data = {
  async getDate(args) {
    const params = //...
    const result = await client.get(params).promise()
    return {
      // id and transaction_date based on result
    }
  }
}