ERROR "Cannot return null for non-nullable field Todo.id"
See original GitHub issueI am getting non-nullable error for the below code, everything looks fine
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test',{
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log('DB Connection Error: ${err.message}');
});
const Todo = mongoose.model('Todo', {
text: String,
compelte: Boolean
});
//Mutation: Need a String Compulsory
const typeDefs = `
type Query {
hello(name: String): String!
todos: [Todo]
}
type Todo{
id: ID!
text: String!
done: Boolean!
}
type Mutation{
createTodo(text: String!): Todo
updateTodo(id: ID!, complete: Boolean!): Boolean
removeTodo(id: ID!): Boolean
}
`;
const resolvers = {
Query: {
hello: (_, { name }) => `Hey! ${name || 'World'}`,
todos: () => Todo.find()
},
Mutation:{
createTodo: async(_,{text}) => {
const todo = new Todo({text, done: false});
await todo.save(); // saving the todo to the database using .save()[returns promise] and we await to get it saved on to the database
// return JSON.parse(JSON.stringify(todo)) ;
return todo;
},
updateTodo: async(_,{id, compelte}) => {
await Todo.findByIdAndUpdate(id, {complete});
return true;
},
removeTodo: async(_, {id}) => {
await Todo.findByIdAndRemove(id);
return true;
}
}
}
THE ERROR
{
"data": {
"createTodo": null
},
"errors": [
{
"message": "Cannot return null for non-nullable field Todo.done.",
"locations": [
{
"line": 5,
"column": 5
}
],
"path": [
"createTodo",
"done"
]
}
]
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Why am I getting a "Cannot return null for non-nullable field ...
The biggest problem with your resolver is that in any number of scenarios, instead of returning a User object, you return a string....
Read more >Cannot return null for non-nullable type graphql - Fauna Forums
I get the following error when i query allTodos index through graphql "errors": [ { "message": "Cannot return null for non-nullable type ...
Read more >Issue in "Exploring our first query" - Help - Apollo GraphQL
... I get the below error: { "errors": [ { "message": "Cannot return null for non-nullable field Query.tracksForHome.", "locations": [ { …
Read more >"Cannot return null for non-nullable field" error - GraphQL API
I have an Item custom class, and I use a custom graphql mutation to update an item via a resolver in Cloud code...
Read more >Why Am I Getting A "Cannot Return Null For Non-Nullable ...
These are fields that won't return a null value when you query them. errors: [{ message: Cannot return null for nonnullable field If...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

From the issue template:
You must add .lean() after .find()