DEV Community

A Beginner’s Guide to GraphQL

Leonardo Maldonado on January 05, 2019

One of the most commonly discussed terms today is the API. A lot of people don’t know exactly what an API is. Basically, API stands for Applicati...
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
 
mvaldes 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...

Collapse
 
cbmgit profile image
codeblogmoney •

This is very good article to explain what is GraphQL and why it's being used. I have been using graphQL for many time and this tool helped to read it better sometime from API, GraphQL Formatter jsonformatter.org/graphql-formatter.

Keep up the good work @leonardomso

Collapse
 
rapidtables1 profile image
rapidtables •

I like this formatting tool too.. and yes @leonardomso this is amazing article.

Collapse
 
kayis profile image
K •

Added this to my reading list :D

Do you have any resources concerning directives? I saw they're extensively used in AWS AppSync and seem really powerful.

Collapse
 
leonardomso profile image
Leonardo Maldonado •

I didn't work with AWS AppSync yet, so I won't be able to help you with that 😞

Collapse
 
kayis profile image
K •

No problem.

I just had the impression directives are a general GraphQL concept :)

Collapse
 
riekus profile image
Riekus van Montfort • • Edited

Thanks for the very comprehensible intro to graphQL!

If I may add, the part about deleting users is a little confusing:

deleteUser: we should pass an ID, name, email, and age. It should return a new user to us.

It shouldn't return a new user, am I right? And is there a particular reason you want to return the user after deleting it?

Collapse
 
leonardomso profile image
Leonardo Maldonado •

Oh, my bad. It should return the deleted user to us, just to make sure that the mutation worked fine.

Collapse
 
tomekponiat profile image
Tomek Poniatowicz •

A good read, thanks!
@Schema part: Now it's easier to fully understand a schema with visualization provided by GraphQL Editor (graphqleditor.com/). Just upload it from URL and enjoy beautifully drawn schema :)

Collapse
 
starswan profile image
Stephen Dicks •

The error handling in delete is interesting. REST would dictate that the deletion of a non existant user works (idempotency) and hence that the deleted user cannot be returned. Does GraphQL not have this concept?

Collapse
 
leonardomso profile image
Leonardo Maldonado •

I don't know if I understood correctly what you mean, but the way I think is, instead you return the deleted user you could return anything like a string saying "User deleted!" or something.

Collapse
 
starswan profile image
Stephen Dicks •

I was asking if GraphQL had the REST concept of idempotency (i.e. if you repeat a data-altering operation, then it behaves the same way - hence the 'deleting a deleted user'). Your implementation isn't idempotent, and I was asking if it should have been. Obviously if you delete something that's not there, the only sensible reply is probably 204 - again REST (IIRC) says this is what you should do, GraphQL...?

Collapse
 
prasadchillara profile image
PrasadChillara •

Nicely explained Leonardo! Thank you !!

Collapse
 
chengxi profile image
chengxi •

Hello, Thanks for your post!!
I am using the graphql in vue js
so api is frontend-test-api.aircall.io/graphql
and query is as follows
query call($id:ID!) {
Call {
id
direction,
from,
to,
duration,
via,
is_archived,
call_type,
created_at,
notes{
id,
content
}
}
}
but I am getting the 404 errors
can you help me?

Collapse
 
sauloco profile image
Saulo Vargas •

Hola Leonardo.
Can it become a series? 🤔

Collapse
 
leonardomso profile image
Leonardo Maldonado •

Yes, why not? I've been thinking to write a series about GraphQL, explaining more deep about Queries, Mutations, and Subscriptions. What do you think?

Collapse
 
sauloco profile image
Saulo Vargas •

I'll be looking forward for it.
Please do it!

Collapse
 
clemblanco profile image
Clément Blanco • • Edited

deleteUser: we should pass an ID, name, email, and age. It should return the deleted user to us.

Typo here. Only the ID is necessary.

Collapse
 
diguifi profile image
Diego Penha •

Muito foda cara, parabéns pelo artigo e muito obrigado por uma explicação tão concisa, simples e bem descrita!

Collapse
 
leonardomso profile image
Leonardo Maldonado •

Obrigado!!!

Collapse
 
wirtzdan profile image
Daniel Wirtz •

Hey Leonardo! I was just looking for a comprehensive and good tutorial on the topic. Great writeup, really helped me!

Collapse
 
whiteadi profile image
Adrian Albu •

Not sure why but for me the strict eq comp operator didn't work but the abstract one did, like they are of different type?!

Anyway with == (user => user.id == id) it did work (in latest chrome...)

Collapse
 
rokkoo profile image
Alfonso •

Clear and simple explained.
Nice post mate! 😊

Collapse
 
bharathnallamothu profile image
bhaRATh •

what about relational data (class has students ). example:hasMany, belongTo

Collapse
 
rushannotofficial profile image
Rushan S J •

Wow, this was an amazing article. Been searching this from ages !

Collapse
 
haroldv22 profile image
Harold Villalobos •

Leonardo excelent post, thank you very much

Collapse
 
capochiani profile image
Francesca Capochiani 🌺 •

Added to my reading list, too!
Thank you @leonardomso

Collapse
 
rebaiahmed profile image
Ahmed Rebai •

Good Job, Nice explication, What about Graphql integration with microservices architecture

Collapse
 
seanmclem profile image
Seanmclem •

Does GraphQL rely on a mongoDB server? Or does it directly run within a node instance? If the later -how does this work? The database is stored on the node-server?