Understanding GraphQL Error Handling Mechanisms in Spring-Boot

Philippe Simo
Feb 16, 2020 · 9 min read

Overview

This article is the second part of the series GraphQL, from Theory to real-world with Spring boot.

TL;DR

Check the corresponding video

GraphQL quick summary

GraphQL is a specification that defines not only a new query language for APIs but also how data is returned in response to these queries.
It is an alternative to using Restful APIs in the sense that it solves some of their drawbacks with new features such as these described ones:

  • The client can request several resources at once in a single request, no need to send multiple requests. Ideal for slow mobile connections.
  • The server defines what is possible or not with the notion of types, to force the client to request only what is possible.

With great power comes great responsibilities

According to the features mentioned above, the client must be highly considered when setting up such an API. It is more than important to return a response format that is consistent and predictable across several scenarios, to make it easier for the customer to consume the response.

Application Set up

We will use a simple spring-boot sample-graphql-error-handling that will expose two endpoints:

Minimal maven dependencies to enable graphql implementation within a spring boot app
The User model class
User.graphqls
UserService.java
UserAlreadyExistsException.java
UserNotFoundException.java
UserMutation.java
UserQuery.java

The generic error (case#1)

Within the graphiql tool, let’s start by creating a user:

GraphQL request to create a user
Response received when creating a user
GraphQL request to retrieve a user
Generic error returned
Generic error returned

Customizing errors (case#2)

We’ve already taken a step forward by defining and throwing problem-related customized exceptions. However, these exceptions need to be transformed into GraphQL errors that respect the standard format before being sent to the client.

UserAlreadyExistsException.java as GraphQLError
UserNotFoundException.java as GraphQLError with extra data
throw new UserNotFoundException(“We were unable to find a user with the provided credentials”, “username”);
SimpleDataFetcherExceptionHandler.java
ExceptionWhileDataFetching.java
The Simple DataFetcherExceptionHandler process
The DefaultGraphQLErrorHandler process
DefaultGraphQLErrorHandler::processErrors()

Sending unwrapped exception

We create the CustomGraphErrorHandler as follows:

CustomGraphQLErrorHandler.java

Conclusion

In this article, we were focusing two notions:

  • Understanding the standard Graphql error response format
  • Diving into the mechanisms behind generic graphql errors and more tailored ones
  • Adding additional custom information to the error
  • Getting to know the possibilities of further error customizations.

The Startup

Get smarter at building your thing. Join The Startup’s +785K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Philippe Simo

Written by

FULLSTACK DEVELOPER, FOUNDER, and CEO of zerofiltre.tech: A company that aims to provide technology services and transform people’s lives with code.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Philippe Simo

Written by

FULLSTACK DEVELOPER, FOUNDER, and CEO of zerofiltre.tech: A company that aims to provide technology services and transform people’s lives with code.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store