Javascript GraphQL“不能为不可为空返回空值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42409005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GraphQL "Cannot return null for non-nullable"
提问by King Julian
Trying to make my first graphQL server, here's what I have written so far.
尝试制作我的第一个 graphQL 服务器,这是我到目前为止所写的内容。
https://gist.github.com/tharakabimal/7f2947e805e69f67af2b633268db0406
https://gist.github.com/tharakabimal/7f2947e805e69f67af2b633268db0406
Following error pops up on GraphQL when I try to filter the users by username.
当我尝试按用户名过滤用户时,GraphQL 上会弹出以下错误。
The error occurs in the users field in UserQueriesQL.js.
错误发生在 UserQueriesQL.js 中的用户字段中。
Is there anything wrong the way I pass arguments on the resolve functions?
我在解析函数上传递参数的方式有什么问题吗?
user: {
type: UserType,
args: {
username: {
name: 'username',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: function(parentValue, args) {
return User.find( args ).exec();
}
回答by Neville Dabreo
As I am beginner into GraphQL, even I ran into this issue. After going through each file individually I found that I forgot to import into my resolvers
因为我是 GraphQL 的初学者,所以我也遇到了这个问题。单独浏览每个文件后,我发现我忘记导入到我的解析器中
import User from './User';
**import Post from './Post';**
const resolvers = [User, **Posts**];
Maybe this will help!
也许这会有所帮助!
回答by p0k8_
user: {
type: UserType,
args: {
username: { type: new GraphQLNonNull(GraphQLString) }
},
resolve: function(parentValue, args) {
return User.find( args ).exec(); // User.find({username: 'some name'}).exec();
// will work as matches your mongoose schema
}
Previously, in the argsyou are providing an an objectwith nested object usernameso,
以前,在args您提供object带有嵌套对象的 an时username,
args: { // this won't match your mongoose schema field as it's nested object
username: {
name: 'username',
type: new GraphQLNonNull(GraphQLString)
}
}
so when the user queries and provides args then
your args would be { username: { name: 'abcd' } }
因此,当用户查询并提供 args 时,您的 args 将是 { username: { name: 'abcd' } }
// args = {username: {name: 'abcd'}}
and resolve()is executing User.find({username: {name: 'abcd'}}).exec();
并且resolve()正在执行User.find({username: {name: 'abcd'}}).exec();
/* searching for username{} object, but
your mongoose schema is username: String */
which doesn't match your database fields, which will always return an empty array [],also which will not match your GraphQL field type, as it is GraphQLNonNull
与您的数据库字段不匹配,它将始终返回一个空数组[],也不会与您的 GraphQL 字段类型匹配,因为它是GraphQLNonNull
after viewing the gistthe problem is with rootquery
查看gist问题后rootquery
the problem is with rootquery
问题出在 rootquery
let RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: () => ({
users: { type:UserQueries.users, resolve: UserQueries.users }
user: { type: UserQueries.user, resolve: UserQueries.user }
})
});

