4
votes

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:"[email protected]"){ id } }

I receive:

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

6
Same struggle. It is hard to find out what is the solution.Michael Czolko
In my case, I wasn't including await before the model creation such as: return await models.User.create({name,email})NathanL
Why did you used mutation instead of query?Michael Czolko
Because creating a user is a mutation (data is being mutated) and not a query.NathanL

6 Answers

2
votes

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

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

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

0
votes

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

0
votes

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

0
votes

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!
}
0
votes

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!]!