Running Spring Boot Application with GraphQL

You might have heard about GraphQL and how Facebook uses GraphQL in their mobile applications. In this blog, I will show you how to implement GraphQL in a Spring Boot application and let’s see what kind of magic does GraphQL provides.

Why GraphQL?

If you do not know about GraphQL then you are in the right place. GraphQL is a query language for REST API endpoints. GraphQL isn’t tied to any specific database or storage engine. Instead, GraphQL is backed by your existing code and data.

The main advantage of using GraphQL are:

No need to create multiple API (Application Programming Interface) endpoints in an application unlike in REST where we expose multiple endpoints to retrieve data like this.

Using GraphQL, we get the exact data we need or request. This is unlike in REST implementation, where we make an HTTP GET call to get a JSON response even if we are looking at the values for a few attributes. For example, when we query a REST API, we get the complete response in JSON format like below even we require only the id and name.

Integrating front-end applications (like mobile apps) with GraphQL are fast & responsive over REST API’s.

Here we will see how to build a Spring Boot application to store books. We then integrate the same application and query for books using GraphQL.

Note: The complete source code of this tutorial is available on GitHub and its URL will be shared at the end of this blog. We will focus more on key classes/files in this application with their explanation.

Let’s start developing our Spring Boot application with GraphQL. I am using IntelliJ IDEA Ultimate. However, you may use any IDE of your choice.

Creating the Application

Visit Spring Initializr or use IntelliJ IDEA Ultimate to generate a Spring Boot application with dependencies like Web, HSQLDB, Spring Boot 2.1.4. It will be a Maven project with JDK 1.8.

The generated POM is this.

Adding an API Endpoint

Let us start with a BookController and add a POST request handler, like this.

Adding a Model Class

Next, we will add a model class to represent a book. We will name it Book. The code of the model class is this.

Creating a Book Repository

The repository of this example extends JpaRepository, like this.

Adding a GraphQL Schema

Next, we will write a GraphQL schema, named books.graphql in our resource folder.

This is a very important file and is the backbone of GraphQL. Here, we define a schema, which you can relate with a Query. We also need to tell the type of query which is triggered by any front-end applications.

In this example, we have shown two types:

  • When a user queries all the books (by using allBooks) then the application will return an array of Book.
  • When a user queries for a specific book by passing the id, then the application will return a Book object.

Adding a GraphQL Service

Next, we need to add a GraphQL service. Lets’ name it as GraphQLService.

When the Spring Boot application runs, the Spring Framework calls the @PostConstruct method. The code inside the @PostConstruct method will load the books into the HQL database.

In the buildRuntimeWiring() method of this service class, we are doing a runtime wiring with two data fetchers: allBookand book. The names allBookand book defined here must match with the types defined in the GraphQL file that we already created.

Creating the Data Fetchers

Each type in the GraphQL schema has a corresponding data fetcher.

We need to write two separate data fetcher classes for the allBooks and Book types that we defined in the schema.

The data fetcher class for the allBooks type is this.

The data fetcher class for the Book type is this.

Running the Application

I am running this application on port 9002 and not on the default 8080 port. Therefore, I have the following property in the file.

With this, our Spring Boot GraphQL application is ready. Let’s run our Spring Boot application and test it using the Postman tool.

Notice here that we have just a single endpoint, http://localhost:9002/rest/books

Let’s query for multiple datasets with this single endpoint. To do this lets open Postman and add the following input query in the request body.

Input 1: Here we are querying for a specific book whose id is 1001 and we want only the title in its response. Along with it, we are querying for allBooks and expecting that response will contain isn, title, author, publisher and publishedDate.

Output 1: The response for both queries is this.

Input 2: Lets’ query again for the title and author of a specific book by ID.

Output 2: The output is this. We get the title and the author for the book whose id is 1001.

Input 3: Lets’ query for allBooks for their title, isn, author, publishedDate and publisher details

Output 3: The output is this.

So, that is the beauty of using GraphQL over REST API. Here we get exactly what we are looking for and not just the complete bunch of JSON response will all the attributes values in it.

You can download the complete source code of this post from GitHub.

Do share your feedback.


Software Engineer | Blogger

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