【发布时间】:2019-06-13 09:19:25
【问题描述】:
Graphql 返回带有 null id 的 Oject。 使用 mongodb。
我觉得很奇怪。
如果我删除 MailType id 上的 new GraphQLNonNull(),
它适用于 id: null,另一个领域工作正常。
const MailType = new GraphQLObjectType({
name: 'Mail',
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLID), },
...
})
const Query = {
mails: {
type: new GraphQLList(MailType),
args: {
senderId: { type: GraphQLID },
isOffline: { type: GraphQLBoolean },
},
async resolve(root, args, req, ctx) {
if (args.isOffline === false) {
let a = await model.aggregate([
{ $match: { isOffline: false } },
]);
let b = await model.find({ isOffline: false });
console.log(JSON.stringify(a) == JSON.Stringify(b)) /// return true
return a // error
return b // working
}
return model.find({senderId: args.senderId});
}
}
}
// with a
"errors": [
{
"message": "Cannot return null for non-nullable field Mail.id."
}]
我遇到了 2 个小时的麻烦,但我没有得到答案。 有人可以帮帮我吗?
【问题讨论】:
-
这只是表示当你返回
a时id字段为空,这不应该是因为你将id定义为GraphQLNonNull而导致错误。 -
@Boney a, b 是邮件数组。两者都有有效 id 的邮件。