Apollo GraphQL "Cannot return null for non-nullable field Mutation.createUser"

Viewed 14318

I know this is a somewhat common issue, but my implementation differs from the other posts. I'm using the most basic implementation which I can't get to work. I'm using Sequelize with MySQL as the database implementation.

resolvers.js

const resolvers = {
    Query: {
        async getStudent (root, { id }, { models }) {
            return models.User.findByPk(id)
        },
    },
    Mutation: {
        async createUser (root, { name, email }, { models }) {
            return models.User.create({
                name,
                email
            })
        },
    },
}

schema.js

const { gql } = require('apollo-server-express');
const typeDefs = gql`
    type User {
        id: Int!
        name: String!
        email: String!
    }
    type Query {
        getUser(id: Int!): User
        getAllUsers: [User!]!
    }
    type Mutation {
        createUser(name: String!, email: String!): User!
    }`
module.exports = typeDefs;

User model

'use strict';
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    name: DataTypes.STRING,
    email: DataTypes.STRING
  }, {});
  User.associate = function(models) {
    // associations can be defined here
  };
  return User;
};

Yet when running the following mutation:

mutation{ createUser(name:"Nate", email:"nate@test.com"){ id } }

I receive:

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

6 Answers

In my case it was because the createUser call was not asynchronous. Hopefully this helps someone:

return models.User.create({
    name,
    email
})

I had the same error. In my case the issue was that I was importing @Mutation from type-graphl instead of @nestjs/graphql.

this error is because your resolver not return any valid user object. just console.log() the createUser resolver to check what it return. then you can trace the cause of the problem

my answer won't be specific to this question but I thought it may help someone as I faced this issue myself and was difficult to figure out the solution. Some keys are used for Associations in sequelize and exact that key should be used in graphql when you intend to retrieve that type. For example Orders belongs to orderStatuses and key used for that is status in my case as on line 56 enter image description here

and exact this status should be used in orders.graphql in Order type as shown here on line 12 enter image description here

In my case, it was a type mismatch between what was specified in the schema vs. what was returned by the resolver: the schema specified a single User (incorrect), but the resolver was returning [User!]!

You can simply remove ! after fields for what them can be have null, for example

type User {
    id: ID!
    name: String
    email: String
    created_at: DateTime!
    updated_at: DateTime!
}
Related