Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 485
AndrewL64 Avatar asked Jan 01 '19 12:01

AndrewL64


1 Answers

This problem comes 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...

like image 168
Coni Avatar answered Oct 29 '22 02:10

Coni