gpt4 book ai didi

javascript - GraphQL 查询返回错误 "Cannot return null for non-nullable field"

转载 作者:数据小太阳 更新时间:2023-10-29 04:51:49 39 4
gpt4 key购买 nike

我有一个基本的 GraphQL 查询设置,如下所示:

查询.js:

const Query = {
dogs(parent, args, ctx, info) {
return [{ name: 'Snickers' }, { name: 'Sunny' }];
},
};

module.exports = Query;

schema.graphql:

type Dog {
name: String!
}
type Query {
dogs: [Dog]!
}

我创建了一个函数 createServer() 来启动服务器,如下所示:

const { GraphQLServer } = require('graphql-yoga');
const Mutation = require('./resolvers/Mutation');
const Query = require('./resolvers/Query');
const db = require('./db');

function createServer() {
return new GraphQLServer({
typeDefs: 'src/schema.graphql',
resolvers: {
Mutation,
Query,
},
resolverValidationOptions: {
requireResolversForResolveType: false,
},
context: req => ({ ...req, db }),
});
}

module.exports = createServer;

然后我尝试按如下方式查询 dogs:

query {
dogs {
name
}
}

但是我没有从狗的数组中获取名字,而是得到了以下错误:

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

似乎是什么导致了这个错误?

最佳答案

这个问题来自于 AWS 在 dynamoDB 表中需要某些标准值,例如 createdAt 和 updatedAd,只需在 dynamo db 中手动添加这些字段和时间戳以进行进一步测试。始终需要通过 id 请求突变,当我的模式是由 amplify codegen 创建时,这对我来说并不清楚...

关于javascript - GraphQL 查询返回错误 "Cannot return null for non-nullable field",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53995484/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com