Reputation: 1002
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" ],
Upvotes: 6
Views: 22626
Reputation: 11
If anyone is having this problem, then it is likely an issue with your Type Definition/GraphQL Schema. The error says it all: somewhere there is a non-nullable field.
I suspect that this user is not implementing their ID functionality correctly somewhere. It's hard to tell without seeing the results of a MySQL query of this data. My guess is that GraphQL isn't getting ID somehow.
Maybe the Mutation should be formatted differently
mutation{
createUser(name:"Nate", email:"[email protected]")
{ id }
}
asks for id to be returned, when this mutation is defined only to return User!
mutation{
createUser(name:"Nate", email:"[email protected]")
{ User {
id } }
}
Would be the format Apollo Client would suggest to me. Perhaps the issue is there.
Upvotes: 1
Reputation: 1002
In my case it was because the createUser call was not asynchronous. Hopefully this helps someone:
return models.User.create({
name,
email
})
Upvotes: 2
Reputation: 181
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!]!
Upvotes: 0
Reputation: 17301
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!
}
Upvotes: -1
Reputation: 1966
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
and exact this status
should be used in orders.graphql
in Order
type as shown here on line 12
Upvotes: 0
Reputation: 3211
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
Upvotes: 2
Reputation: 570
I had the same error. In my case the issue was that I was importing @Mutation
from type-graphl
instead of @nestjs/graphql
.
Upvotes: 1