【发布时间】:2017-07-13 12:52:13
【问题描述】:
尝试制作我的第一个 graphQL 服务器,这是我目前所写的。
https://gist.github.com/tharakabimal/7f2947e805e69f67af2b633268db0406
当我尝试按用户名过滤用户时,GraphQL 上会弹出以下错误。
错误发生在 UserQueriesQL.js 中的用户字段中。
我在解析函数上传递参数的方式有什么问题吗?
user: {
type: UserType,
args: {
username: {
name: 'username',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: function(parentValue, args) {
return User.find( args ).exec();
}
【问题讨论】:
-
该错误意味着您正在返回
null,但使用type: new GraphQLNonNull(GraphQLString),您声明可能永远不会返回用户名null。要么返回null以外的其他内容,要么将类型声明为type: new GraphQLString() -
User.find将解析为一个数组,但 GraphQL 期望的是一个对象。请参阅this answer 中的常见场景 #2。
标签: javascript node.js express graphql