Status and Error codes

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:

  1. GRAPHQL_VALIDATION_FAILED

  2. UNAUTHENTICATED

  3. FORBIDDEN

  4. BAD_USER_INPUT

  5. NOT_FOUND

You can check out this article to understand error codes in detail.

You must check for the presence of error objects along with error codes and messages to handle GraphQL errors in a structured way.

Updated on