最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - GraphQL query returns error "Cannot return null for non-nullable field" - Stack Overflow

programmeradmin0浏览0评论

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?

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 edited Jan 1, 2019 at 14:16 AndrewL64 asked Jan 1, 2019 at 12:36 AndrewL64AndrewL64 16.3k8 gold badges50 silver badges85 bronze badges 9
  • 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 Commented Jan 1, 2019 at 12:49
  • @DanielRearden Ok hold on. – AndrewL64 Commented Jan 1, 2019 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 Commented Jan 1, 2019 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 Commented Jan 1, 2019 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 Commented Jan 1, 2019 at 14:01
 |  Show 4 more ments

4 Answers 4

Reset to default 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.

I know this question has been answered, but for me the only thing that fixed this issue was to also pass the info argument.

In my case, I create a new Query.js file at the src folder but I import Query with Query = require('./resolvers/Query') and coding there. So, try to check the path, I think the problem is there.

This problem es from AWS requiring certain standard values in the dynamoDB table, such as createdAt and updatedAd, just add these fields manually with a timestamp in dynamo db for further testing. A mutation always needs to be requested via id, this somehow was not clear to me when my schema was created by amplify codegen...

发布评论

评论列表(0)

  1. 暂无评论