GraphQL "Cannot return null for non-nullable" [duplicate]
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
import User from './User';**import Post from './Post';**const resolvers = [User, **Posts**];
Maybe this will help!
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 args
you are providing an an object
with nested object username
so,
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 = {username: {name: 'abcd'}}
and resolve()
is executing User.find({username: {name: 'abcd'}}).exec();
/* searching for username{} object, butyour 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
after viewing the gist
the problem is with rootquery
the problem is with rootquery
let RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: () => ({ users: { type:UserQueries.users, resolve: UserQueries.users } user: { type: UserQueries.user, resolve: UserQueries.user } })});