GraphQL APIs use HTTP status codes to indicate the success or failure of a request. A 200 OK status code means the request was successful. In addition to HTTP status codes, GraphQL APIs also return error objects in response to specific errors.
These error objects have a code and message property. The code is a string that identifies the type of error. For example, you'll receive something like this if you try to request restricted fields without passing the authorization header.
{
"errors": [
{
"message": "You must be authenticated.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": ["me"],
"extensions": {
"code": "UNAUTHENTICATED"
}
}
],
"data": null
}
Some of the error codes are:
-
GRAPHQL_VALIDATION_FAILED
-
UNAUTHENTICATED
-
FORBIDDEN
-
BAD_USER_INPUT
-
NOT_FOUND
You must check for the presence of error objects along with error codes and messages to handle GraphQL errors in a structured way.