GraphQL “Cannot return null for non-nullable”












7















Trying to make my first graphQL server, here's what I have written so far.



https://gist.github.com/tharakabimal/7f2947e805e69f67af2b633268db0406



Following error pops up on GraphQL when I try to filter the users by username.



Error on GraphQL



The error occurs in the users field in 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();
}









share|improve this question

























  • The error means you are returning null, but with type: new GraphQLNonNull(GraphQLString) you declared that there may never be returned null for the username. Either return something else than null, or declare the type as type: new GraphQLString()

    – marktani
    Feb 23 '17 at 13:49
















7















Trying to make my first graphQL server, here's what I have written so far.



https://gist.github.com/tharakabimal/7f2947e805e69f67af2b633268db0406



Following error pops up on GraphQL when I try to filter the users by username.



Error on GraphQL



The error occurs in the users field in 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();
}









share|improve this question

























  • The error means you are returning null, but with type: new GraphQLNonNull(GraphQLString) you declared that there may never be returned null for the username. Either return something else than null, or declare the type as type: new GraphQLString()

    – marktani
    Feb 23 '17 at 13:49














7












7








7


4






Trying to make my first graphQL server, here's what I have written so far.



https://gist.github.com/tharakabimal/7f2947e805e69f67af2b633268db0406



Following error pops up on GraphQL when I try to filter the users by username.



Error on GraphQL



The error occurs in the users field in 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();
}









share|improve this question
















Trying to make my first graphQL server, here's what I have written so far.



https://gist.github.com/tharakabimal/7f2947e805e69f67af2b633268db0406



Following error pops up on GraphQL when I try to filter the users by username.



Error on GraphQL



The error occurs in the users field in 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();
}






javascript node.js express graphql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 12:06









AndrewL64

10.1k41846




10.1k41846










asked Feb 23 '17 at 6:58









King JulianKing Julian

64124




64124













  • The error means you are returning null, but with type: new GraphQLNonNull(GraphQLString) you declared that there may never be returned null for the username. Either return something else than null, or declare the type as type: new GraphQLString()

    – marktani
    Feb 23 '17 at 13:49



















  • The error means you are returning null, but with type: new GraphQLNonNull(GraphQLString) you declared that there may never be returned null for the username. Either return something else than null, or declare the type as type: new GraphQLString()

    – marktani
    Feb 23 '17 at 13:49

















The error means you are returning null, but with type: new GraphQLNonNull(GraphQLString) you declared that there may never be returned null for the username. Either return something else than null, or declare the type as type: new GraphQLString()

– marktani
Feb 23 '17 at 13:49





The error means you are returning null, but with type: new GraphQLNonNull(GraphQLString) you declared that there may never be returned null for the username. Either return something else than null, or declare the type as type: new GraphQLString()

– marktani
Feb 23 '17 at 13:49












1 Answer
1






active

oldest

votes


















1














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, 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



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 }
})
});





share|improve this answer


























  • I did the suggested changes, but still having the issue gist.github.com/tharakabimal/c84ff21d6a6aa898cb3ccc02757867df

    – King Julian
    Feb 23 '17 at 10:35













  • have you entered the username in the args

    – p0k8_
    Feb 23 '17 at 10:37











  • Yes gist.github.com/tharakabimal/1883dcb50322456fe81d37e9abc22daa

    – King Julian
    Feb 23 '17 at 10:55











  • so do you have username John in users collection, put a gist and snapshot of the database

    – p0k8_
    Feb 23 '17 at 10:58











  • Here are the 3 users I have in my mongoDB gist.github.com/tharakabimal/cdd58e7534fef268801142eb7ce4266c

    – King Julian
    Feb 23 '17 at 11:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42409005%2fgraphql-cannot-return-null-for-non-nullable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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, 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



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 }
})
});





share|improve this answer


























  • I did the suggested changes, but still having the issue gist.github.com/tharakabimal/c84ff21d6a6aa898cb3ccc02757867df

    – King Julian
    Feb 23 '17 at 10:35













  • have you entered the username in the args

    – p0k8_
    Feb 23 '17 at 10:37











  • Yes gist.github.com/tharakabimal/1883dcb50322456fe81d37e9abc22daa

    – King Julian
    Feb 23 '17 at 10:55











  • so do you have username John in users collection, put a gist and snapshot of the database

    – p0k8_
    Feb 23 '17 at 10:58











  • Here are the 3 users I have in my mongoDB gist.github.com/tharakabimal/cdd58e7534fef268801142eb7ce4266c

    – King Julian
    Feb 23 '17 at 11:09
















1














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, 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



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 }
})
});





share|improve this answer


























  • I did the suggested changes, but still having the issue gist.github.com/tharakabimal/c84ff21d6a6aa898cb3ccc02757867df

    – King Julian
    Feb 23 '17 at 10:35













  • have you entered the username in the args

    – p0k8_
    Feb 23 '17 at 10:37











  • Yes gist.github.com/tharakabimal/1883dcb50322456fe81d37e9abc22daa

    – King Julian
    Feb 23 '17 at 10:55











  • so do you have username John in users collection, put a gist and snapshot of the database

    – p0k8_
    Feb 23 '17 at 10:58











  • Here are the 3 users I have in my mongoDB gist.github.com/tharakabimal/cdd58e7534fef268801142eb7ce4266c

    – King Julian
    Feb 23 '17 at 11:09














1












1








1







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, 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



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 }
})
});





share|improve this answer















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, 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



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 }
})
});






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 23 '17 at 11:30

























answered Feb 23 '17 at 7:05









p0k8_p0k8_

4,19142133




4,19142133













  • I did the suggested changes, but still having the issue gist.github.com/tharakabimal/c84ff21d6a6aa898cb3ccc02757867df

    – King Julian
    Feb 23 '17 at 10:35













  • have you entered the username in the args

    – p0k8_
    Feb 23 '17 at 10:37











  • Yes gist.github.com/tharakabimal/1883dcb50322456fe81d37e9abc22daa

    – King Julian
    Feb 23 '17 at 10:55











  • so do you have username John in users collection, put a gist and snapshot of the database

    – p0k8_
    Feb 23 '17 at 10:58











  • Here are the 3 users I have in my mongoDB gist.github.com/tharakabimal/cdd58e7534fef268801142eb7ce4266c

    – King Julian
    Feb 23 '17 at 11:09



















  • I did the suggested changes, but still having the issue gist.github.com/tharakabimal/c84ff21d6a6aa898cb3ccc02757867df

    – King Julian
    Feb 23 '17 at 10:35













  • have you entered the username in the args

    – p0k8_
    Feb 23 '17 at 10:37











  • Yes gist.github.com/tharakabimal/1883dcb50322456fe81d37e9abc22daa

    – King Julian
    Feb 23 '17 at 10:55











  • so do you have username John in users collection, put a gist and snapshot of the database

    – p0k8_
    Feb 23 '17 at 10:58











  • Here are the 3 users I have in my mongoDB gist.github.com/tharakabimal/cdd58e7534fef268801142eb7ce4266c

    – King Julian
    Feb 23 '17 at 11:09

















I did the suggested changes, but still having the issue gist.github.com/tharakabimal/c84ff21d6a6aa898cb3ccc02757867df

– King Julian
Feb 23 '17 at 10:35







I did the suggested changes, but still having the issue gist.github.com/tharakabimal/c84ff21d6a6aa898cb3ccc02757867df

– King Julian
Feb 23 '17 at 10:35















have you entered the username in the args

– p0k8_
Feb 23 '17 at 10:37





have you entered the username in the args

– p0k8_
Feb 23 '17 at 10:37













Yes gist.github.com/tharakabimal/1883dcb50322456fe81d37e9abc22daa

– King Julian
Feb 23 '17 at 10:55





Yes gist.github.com/tharakabimal/1883dcb50322456fe81d37e9abc22daa

– King Julian
Feb 23 '17 at 10:55













so do you have username John in users collection, put a gist and snapshot of the database

– p0k8_
Feb 23 '17 at 10:58





so do you have username John in users collection, put a gist and snapshot of the database

– p0k8_
Feb 23 '17 at 10:58













Here are the 3 users I have in my mongoDB gist.github.com/tharakabimal/cdd58e7534fef268801142eb7ce4266c

– King Julian
Feb 23 '17 at 11:09





Here are the 3 users I have in my mongoDB gist.github.com/tharakabimal/cdd58e7534fef268801142eb7ce4266c

– King Julian
Feb 23 '17 at 11:09




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42409005%2fgraphql-cannot-return-null-for-non-nullable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas