GraphQL - Error Handling
Error Handling in GraphQL
With GraphQL you should check the HTTP Status Codes in return of the HTTP POST request (and it should return status code 200 OK - Success!
). When you receive a HTTP status code different from 200 then you have probably one of the following issues:
- Server problems (5xx HTTP codes, 1xxx WebSocket codes)
- Client problems like rate-limited, unauthorized, etc. (4xx HTTP codes). We will discuss rate-limiting in the next chapter of this training.
- The GraphQL query is missing or was malformed.
But that’s not all. Once the GraphQL web service has accepted the GraphQL query, an error message can be returned in the response.
That’s what happened in the exercise where you added the Manager field without specifying a Selection Set for Manager:
When the response contains results.data
the GraphQL is ok, but when the response contains results.errors
the GraphQL query failed to execute. In that case the variables or context provided in the GraphQL query is probably bad. You need to add error handling code to your integrations that acts on the HTTP Status Codes
and on the results.errors
responses.