GraphQL query returns error “Cannot return null for non-nullable field”












4















I have a basic GraphQL query setup as follows:



Query.js:



const Query = {
dogs(parent, args, ctx, info) {
return [{ name: 'Snickers' }, { name: 'Sunny' }];
},
};

module.exports = Query;




schema.graphql:



type Dog {
name: String!
}
type Query {
dogs: [Dog]!
}




I created a function createServer() for starting the server as follows:



const { GraphQLServer } = require('graphql-yoga');
const Mutation = require('./resolvers/Mutation');
const Query = require('./resolvers/Query');
const db = require('./db');

function createServer() {
return new GraphQLServer({
typeDefs: 'src/schema.graphql',
resolvers: {
Mutation,
Query,
},
resolverValidationOptions: {
requireResolversForResolveType: false,
},
context: req => ({ ...req, db }),
});
}

module.exports = createServer;


I then tried querying dogs as follows:



query {
dogs {
name
}
}


But instead of getting the names from the array of dogs, I got the following error instead:



{
"data": null,
"errors": [
{
"message": "Cannot return null for non-nullable field Query.dogs.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"dogs"
]
}
]
}


What seems to be causing this error?










share|improve this question

























  • Sounds like you're not importing Query.js correctly or your resolvers are not set up correctly. Please show the rest of the relevant code, including how you're constructing the resolvers object and how you're providing the type defs and resolvers to the GraphQLServer constructor.

    – Daniel Rearden
    Jan 1 at 12:49











  • @DanielRearden Ok hold on.

    – AndrewL64
    Jan 1 at 12:49











  • @DanielRearden My prisma.graphql has more than 300 lines of code. Should I copy a certain part of it or do you want me to copy the whole thing?

    – AndrewL64
    Jan 1 at 12:52






  • 1





    Odd, I can run a graphql-yoga server with the above typeDefs and resolvers with no problems. Unless ./resolvers/Query is not the right path for Query, I'm not sure what else could be wrong :/

    – Daniel Rearden
    Jan 1 at 13:23






  • 1





    If it were an incorrect path node wouldn't be able to resolve it and would throw an error: "Error: Cannot find module ..." and the server would be down. It looks like it does resolve, but to an empty object, so the dogs resolver is not there. Are you sure Query.js exports are correct?

    – Ionut Achim
    Jan 1 at 14:01


















4















I have a basic GraphQL query setup as follows:



Query.js:



const Query = {
dogs(parent, args, ctx, info) {
return [{ name: 'Snickers' }, { name: 'Sunny' }];
},
};

module.exports = Query;




schema.graphql:



type Dog {
name: String!
}
type Query {
dogs: [Dog]!
}




I created a function createServer() for starting the server as follows:



const { GraphQLServer } = require('graphql-yoga');
const Mutation = require('./resolvers/Mutation');
const Query = require('./resolvers/Query');
const db = require('./db');

function createServer() {
return new GraphQLServer({
typeDefs: 'src/schema.graphql',
resolvers: {
Mutation,
Query,
},
resolverValidationOptions: {
requireResolversForResolveType: false,
},
context: req => ({ ...req, db }),
});
}

module.exports = createServer;


I then tried querying dogs as follows:



query {
dogs {
name
}
}


But instead of getting the names from the array of dogs, I got the following error instead:



{
"data": null,
"errors": [
{
"message": "Cannot return null for non-nullable field Query.dogs.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"dogs"
]
}
]
}


What seems to be causing this error?










share|improve this question

























  • Sounds like you're not importing Query.js correctly or your resolvers are not set up correctly. Please show the rest of the relevant code, including how you're constructing the resolvers object and how you're providing the type defs and resolvers to the GraphQLServer constructor.

    – Daniel Rearden
    Jan 1 at 12:49











  • @DanielRearden Ok hold on.

    – AndrewL64
    Jan 1 at 12:49











  • @DanielRearden My prisma.graphql has more than 300 lines of code. Should I copy a certain part of it or do you want me to copy the whole thing?

    – AndrewL64
    Jan 1 at 12:52






  • 1





    Odd, I can run a graphql-yoga server with the above typeDefs and resolvers with no problems. Unless ./resolvers/Query is not the right path for Query, I'm not sure what else could be wrong :/

    – Daniel Rearden
    Jan 1 at 13:23






  • 1





    If it were an incorrect path node wouldn't be able to resolve it and would throw an error: "Error: Cannot find module ..." and the server would be down. It looks like it does resolve, but to an empty object, so the dogs resolver is not there. Are you sure Query.js exports are correct?

    – Ionut Achim
    Jan 1 at 14:01
















4












4








4


2






I have a basic GraphQL query setup as follows:



Query.js:



const Query = {
dogs(parent, args, ctx, info) {
return [{ name: 'Snickers' }, { name: 'Sunny' }];
},
};

module.exports = Query;




schema.graphql:



type Dog {
name: String!
}
type Query {
dogs: [Dog]!
}




I created a function createServer() for starting the server as follows:



const { GraphQLServer } = require('graphql-yoga');
const Mutation = require('./resolvers/Mutation');
const Query = require('./resolvers/Query');
const db = require('./db');

function createServer() {
return new GraphQLServer({
typeDefs: 'src/schema.graphql',
resolvers: {
Mutation,
Query,
},
resolverValidationOptions: {
requireResolversForResolveType: false,
},
context: req => ({ ...req, db }),
});
}

module.exports = createServer;


I then tried querying dogs as follows:



query {
dogs {
name
}
}


But instead of getting the names from the array of dogs, I got the following error instead:



{
"data": null,
"errors": [
{
"message": "Cannot return null for non-nullable field Query.dogs.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"dogs"
]
}
]
}


What seems to be causing this error?










share|improve this question
















I have a basic GraphQL query setup as follows:



Query.js:



const Query = {
dogs(parent, args, ctx, info) {
return [{ name: 'Snickers' }, { name: 'Sunny' }];
},
};

module.exports = Query;




schema.graphql:



type Dog {
name: String!
}
type Query {
dogs: [Dog]!
}




I created a function createServer() for starting the server as follows:



const { GraphQLServer } = require('graphql-yoga');
const Mutation = require('./resolvers/Mutation');
const Query = require('./resolvers/Query');
const db = require('./db');

function createServer() {
return new GraphQLServer({
typeDefs: 'src/schema.graphql',
resolvers: {
Mutation,
Query,
},
resolverValidationOptions: {
requireResolversForResolveType: false,
},
context: req => ({ ...req, db }),
});
}

module.exports = createServer;


I then tried querying dogs as follows:



query {
dogs {
name
}
}


But instead of getting the names from the array of dogs, I got the following error instead:



{
"data": null,
"errors": [
{
"message": "Cannot return null for non-nullable field Query.dogs.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"dogs"
]
}
]
}


What seems to be causing this error?







javascript node.js graphql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 14:16







AndrewL64

















asked Jan 1 at 12:36









AndrewL64AndrewL64

10.2k41847




10.2k41847













  • Sounds like you're not importing Query.js correctly or your resolvers are not set up correctly. Please show the rest of the relevant code, including how you're constructing the resolvers object and how you're providing the type defs and resolvers to the GraphQLServer constructor.

    – Daniel Rearden
    Jan 1 at 12:49











  • @DanielRearden Ok hold on.

    – AndrewL64
    Jan 1 at 12:49











  • @DanielRearden My prisma.graphql has more than 300 lines of code. Should I copy a certain part of it or do you want me to copy the whole thing?

    – AndrewL64
    Jan 1 at 12:52






  • 1





    Odd, I can run a graphql-yoga server with the above typeDefs and resolvers with no problems. Unless ./resolvers/Query is not the right path for Query, I'm not sure what else could be wrong :/

    – Daniel Rearden
    Jan 1 at 13:23






  • 1





    If it were an incorrect path node wouldn't be able to resolve it and would throw an error: "Error: Cannot find module ..." and the server would be down. It looks like it does resolve, but to an empty object, so the dogs resolver is not there. Are you sure Query.js exports are correct?

    – Ionut Achim
    Jan 1 at 14:01





















  • Sounds like you're not importing Query.js correctly or your resolvers are not set up correctly. Please show the rest of the relevant code, including how you're constructing the resolvers object and how you're providing the type defs and resolvers to the GraphQLServer constructor.

    – Daniel Rearden
    Jan 1 at 12:49











  • @DanielRearden Ok hold on.

    – AndrewL64
    Jan 1 at 12:49











  • @DanielRearden My prisma.graphql has more than 300 lines of code. Should I copy a certain part of it or do you want me to copy the whole thing?

    – AndrewL64
    Jan 1 at 12:52






  • 1





    Odd, I can run a graphql-yoga server with the above typeDefs and resolvers with no problems. Unless ./resolvers/Query is not the right path for Query, I'm not sure what else could be wrong :/

    – Daniel Rearden
    Jan 1 at 13:23






  • 1





    If it were an incorrect path node wouldn't be able to resolve it and would throw an error: "Error: Cannot find module ..." and the server would be down. It looks like it does resolve, but to an empty object, so the dogs resolver is not there. Are you sure Query.js exports are correct?

    – Ionut Achim
    Jan 1 at 14:01



















Sounds like you're not importing Query.js correctly or your resolvers are not set up correctly. Please show the rest of the relevant code, including how you're constructing the resolvers object and how you're providing the type defs and resolvers to the GraphQLServer constructor.

– Daniel Rearden
Jan 1 at 12:49





Sounds like you're not importing Query.js correctly or your resolvers are not set up correctly. Please show the rest of the relevant code, including how you're constructing the resolvers object and how you're providing the type defs and resolvers to the GraphQLServer constructor.

– Daniel Rearden
Jan 1 at 12:49













@DanielRearden Ok hold on.

– AndrewL64
Jan 1 at 12:49





@DanielRearden Ok hold on.

– AndrewL64
Jan 1 at 12:49













@DanielRearden My prisma.graphql has more than 300 lines of code. Should I copy a certain part of it or do you want me to copy the whole thing?

– AndrewL64
Jan 1 at 12:52





@DanielRearden My prisma.graphql has more than 300 lines of code. Should I copy a certain part of it or do you want me to copy the whole thing?

– AndrewL64
Jan 1 at 12:52




1




1





Odd, I can run a graphql-yoga server with the above typeDefs and resolvers with no problems. Unless ./resolvers/Query is not the right path for Query, I'm not sure what else could be wrong :/

– Daniel Rearden
Jan 1 at 13:23





Odd, I can run a graphql-yoga server with the above typeDefs and resolvers with no problems. Unless ./resolvers/Query is not the right path for Query, I'm not sure what else could be wrong :/

– Daniel Rearden
Jan 1 at 13:23




1




1





If it were an incorrect path node wouldn't be able to resolve it and would throw an error: "Error: Cannot find module ..." and the server would be down. It looks like it does resolve, but to an empty object, so the dogs resolver is not there. Are you sure Query.js exports are correct?

– Ionut Achim
Jan 1 at 14:01







If it were an incorrect path node wouldn't be able to resolve it and would throw an error: "Error: Cannot find module ..." and the server would be down. It looks like it does resolve, but to an empty object, so the dogs resolver is not there. Are you sure Query.js exports are correct?

– Ionut Achim
Jan 1 at 14:01














1 Answer
1






active

oldest

votes


















0














The above code works as you can see in codesandbox: https://codesandbox.io/s/olzj9vvpk5



But when I convert Query to something like {} it returns the same error so please check your paths and console.log Query to validate the path. Your export looks correct but you might have forgotten to save the file as I can see from the course starter files Query is an {}. Please double check.



Also if this code is in a public git repo please share the link.






share|improve this answer
























  • The problem was with a typo in one of my files apparently. I marked your answer as correct regardless because your codepen helped me in finding the typo. Cheers.

    – AndrewL64
    Feb 6 at 6:31











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%2f53995484%2fgraphql-query-returns-error-cannot-return-null-for-non-nullable-field%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









0














The above code works as you can see in codesandbox: https://codesandbox.io/s/olzj9vvpk5



But when I convert Query to something like {} it returns the same error so please check your paths and console.log Query to validate the path. Your export looks correct but you might have forgotten to save the file as I can see from the course starter files Query is an {}. Please double check.



Also if this code is in a public git repo please share the link.






share|improve this answer
























  • The problem was with a typo in one of my files apparently. I marked your answer as correct regardless because your codepen helped me in finding the typo. Cheers.

    – AndrewL64
    Feb 6 at 6:31
















0














The above code works as you can see in codesandbox: https://codesandbox.io/s/olzj9vvpk5



But when I convert Query to something like {} it returns the same error so please check your paths and console.log Query to validate the path. Your export looks correct but you might have forgotten to save the file as I can see from the course starter files Query is an {}. Please double check.



Also if this code is in a public git repo please share the link.






share|improve this answer
























  • The problem was with a typo in one of my files apparently. I marked your answer as correct regardless because your codepen helped me in finding the typo. Cheers.

    – AndrewL64
    Feb 6 at 6:31














0












0








0







The above code works as you can see in codesandbox: https://codesandbox.io/s/olzj9vvpk5



But when I convert Query to something like {} it returns the same error so please check your paths and console.log Query to validate the path. Your export looks correct but you might have forgotten to save the file as I can see from the course starter files Query is an {}. Please double check.



Also if this code is in a public git repo please share the link.






share|improve this answer













The above code works as you can see in codesandbox: https://codesandbox.io/s/olzj9vvpk5



But when I convert Query to something like {} it returns the same error so please check your paths and console.log Query to validate the path. Your export looks correct but you might have forgotten to save the file as I can see from the course starter files Query is an {}. Please double check.



Also if this code is in a public git repo please share the link.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 14:06









Harshit PantHarshit Pant

1337




1337













  • The problem was with a typo in one of my files apparently. I marked your answer as correct regardless because your codepen helped me in finding the typo. Cheers.

    – AndrewL64
    Feb 6 at 6:31



















  • The problem was with a typo in one of my files apparently. I marked your answer as correct regardless because your codepen helped me in finding the typo. Cheers.

    – AndrewL64
    Feb 6 at 6:31

















The problem was with a typo in one of my files apparently. I marked your answer as correct regardless because your codepen helped me in finding the typo. Cheers.

– AndrewL64
Feb 6 at 6:31





The problem was with a typo in one of my files apparently. I marked your answer as correct regardless because your codepen helped me in finding the typo. Cheers.

– AndrewL64
Feb 6 at 6:31




















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%2f53995484%2fgraphql-query-returns-error-cannot-return-null-for-non-nullable-field%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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith