question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ERROR "Cannot return null for non-nullable field Todo.id"

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
danielreardencommented, Aug 15, 2020

From the issue template:

We want to keep signal strong in the GitHub issue tracker – to make sure that it remains the best place to track bugs and features that affect development.

If you have a question on how to use GraphQL, please post it to Stack Overflow with the tag #graphql.

Please do not post general questions directly as GitHub issues. They may sit for weeks unanswered, or may be spontaneously closed without answer.

2reactions
igdev116commented, Apr 8, 2022

You must add .lean() after .find()

const found = await User.find().lean() // it worked for me 🚀🚀
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found