Building GraphQL API with Python & Django Part #5: GraphQL Errors

In this chapter, you will understand how to handle GraphQL errors in your application.

All applications fail, and GraphQL is no different. Some clients may ask for information that’s not available or execute a forbidden action. In this section, you’ll learn how to handle errors in your GraphQL API.
So lets get started with error handling in GraphQL!
. . .

GraphQL Error Types

There are a plethora of errors that a client can encounter when querying a GraphQL Server. Whether it’s a query, mutation, or subscription, they all fall into 6 types:
  1. Server problems (5xx HTTP codes, 1xxx WebSocket codes)
  2. Client problems e.g. rate-limited, unauthorized, etc. (4xx HTTP codes)
  3. The query is missing/malformed
  4. The query fails GraphQL internal validation (syntax, schema logic, etc.)
  5. The user-supplied variables or context is bad and the resolve/subscribe function intentionally throws an error (e.g. not allowed to view requested user)
  6. An uncaught developer error occurred inside the resolve/subscribe function (e.g. poorly written database query)

So, which of these errors is critical enough to ignore all the data?
  • Numbers 1–3 for sure, since they occur before GraphQL even gets called.
  • Number 4, too, it calls GraphQL, but only receives errors in response.
  • For 5–6, GraphQL responds with both partial data and an array of errors. Some would conflate type 5 with type 2, for example, running out of query “points” (like what GitHub does) could constitute an HTTP 429 (too many requests). But at the end of the day, the simplest answer is the best:

If GraphQL gives you a result with data, even if that result contains errors, it is not an error.
. . .

Schema Errors

Being a language with a strong type system, GraphQL can predetermine if a query is valid. All the fields from queries and mutations have a strong type, so requesting and inputting wrong data will generate an error.

Try it out! In the human query, ask for the age field and see how GraphQL returns back an error:

If you want to know more about the graphql type system you can refer this post
. . .

Handling GraphQL Errors

On the application level, you can use the GraphQLError class or the good and old Python exceptions.

For example, what happens if you try to get the human character which does not exist in our database, it will throw the error, we can use GraphQL errors to throw errors in our application code

You can modify the human resolver as follows with GraphQLError:

def resolver_human(id):
return Human.objects.get(id=id)
except Human.DoesNotExist:
raise GraphQLError("Could not find the human object with given id: " + str(id))

Let's walk through the comments to understand the code
  1. We have defined the resolver which returns the Human character
  2. We have wrapped the code which may throw an exception
  3. We have Queried the database to get the human character by ID, it will raise an exception if record not found
  4. We are catching the DoesNotExist exception and using the GraphqlErrors to raise custom GraphQL error which will be returned to the client

Here is how the errors look like:

We have just looked at the surface of error handling in your graphql server, but it can be modified according to your requirements.
. . .
In the next tutorial, we will add the JWT authentication to the GraphQL server which will allow users to authenticate

On a mission to build Next-Gen Community Platform for Developers