DEV Community

Discussion on: A Beginner’s Guide to GraphQL

Collapse
 
danbraun profile image
Dan Braun

In the example to return a single user:

query {    
    user(id: 1) {    
        id    
        name    
        email    
        age    
    }    
}
Enter fullscreen mode Exit fullscreen mode

I get the error:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Query.user.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "user"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Query.user.",
            "    at completeValue (/Users/danielbraun/Sites/graphql-server/node_modules/graphql/execution/execute.js:631:13)",
            "    at completeValueWithLocatedError (/Users/danielbraun/Sites/graphql-server/node_modules/graphql/execution/execute.js:580:21)",
            "    at completeValueCatchingError (/Users/danielbraun/Sites/graphql-server/node_modules/graphql/execution/execute.js:550:12)",
            "    at resolveField (/Users/danielbraun/Sites/graphql-server/node_modules/graphql/execution/execute.js:497:10)",
            "    at /Users/danielbraun/Sites/graphql-server/node_modules/graphql/execution/execute.js:364:18",
            "    at Array.reduce (<anonymous>)",
            "    at executeFields (/Users/danielbraun/Sites/graphql-server/node_modules/graphql/execution/execute.js:361:42)",
            "    at executeOperation (/Users/danielbraun/Sites/graphql-server/node_modules/graphql/execution/execute.js:289:122)",
            "    at executeImpl (/Users/danielbraun/Sites/graphql-server/node_modules/graphql/execution/execute.js:154:14)",
            "    at Object.execute (/Users/danielbraun/Sites/graphql-server/node_modules/graphql/execution/execute.js:131:35)"
          ]
        }
      }
    }
  ],
  "data": null
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
leonardomso profile image
Leonardo Maldonado • Edited

I really think that this error came from de db.js, did you uploaded it right? Can you upload this code in someplace? I may be able to help you if you do so.

Collapse
 
mdings profile image
Maarten Dings

That's the type checking (===) failing there. I had the same issue. Replace the find functions to double equal checks instead of triple, then it should work: users.find(user => user.id == id)
Not sure why the typecheck doesn't work. Perhaps the ID! in schema.graphql is returning a string by default?

Thread Thread
 
danbraun profile image
Dan Braun

Yup, that was it.

Thread Thread
 
rorixrebel profile image
Miguel Valdes

spent like 30 mins trying to get it to work, triple = kills the functionality

Thread Thread
 
maybebored profile image
Mayuran • Edited

This helped me better understand '==' vs '==='

github.com/getify/You-Dont-Know-JS...

EDIT: 'ID' type is serialised to String in graphql. So really, the comparison works as expected. If anything, this needs better error handling.

apollographql.com/docs/apollo-serv...