Go GraphQL: Handling GraphQL Errors in Golang #5

In this chapter, youll 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

All applications fail, and GraphQL is no different. Some clients may ask for information that’s not available or execute a forbidden action. On the application level, you can use the gqlerrors for raising the exception in your application.

A ) Whithout gqlerrors
So first look at how our code looks like before using the gqlerrors while throwing the GraphQL errors

Below is human Query resolver that finds the human by given id in a list of humans array, if nothing is found it will throw the error as follows:

func GetHuman(id int) (StarWarsChar, error) {
for _, human := range HumanData {
if human.ID == id {
return human, nil
}
}
return StarWarsChar{}, fmt.Errorf("cound not find the person with given id")
}

Then inside the resolver, you can check for the errors as follows:
"human": &graphql.Field{
Type: types.HumanType,
... more code
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
#1
char, err := resolvers.GetHuman(id)
if err != nil {
#2
return nil, err
}
#3
return char, nil
},
}

Let's walk through the comments to understand what's going on in the resolver:
  1. We have a function which could return the Human Character or a GraphQL error
  2. If we have GraphQL error, we are returning it, and data is nil
  3. If there is no error, data is returned from the resolver

Here is how the errors looks like:


B ) Throwing errors with gqlerrors
On the application level, you can use the gqlerrors for raising the exception in your application.

You can modify the human resolver as follows with gqlerrors:
func GetHuman(id int) (StarWarsChar, error) {
for _, human := range HumanData {
if human.ID == id {
return human, nil
}
}
return StarWarsChar{}, gqlerrors.FormatError(errors.New("cound not find the person with given id"))
}

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, below are some threads related to error handling you can explore to understand more
. . .


Never miss a post from Gufran Mirza, when you sign up for Ednsquare.