Integration and Using GraphQL on a Java Spring Boot Project

Enes Gür
emlakjet
Published in
7 min readMar 8, 2021

--

The advancement of technology has brought up-to-date and innovative solutions to many demands or problems. One of the most popular of these solutions in the software world is GraphQL technology, which helps to reduce network traffic and do a lot of work with less code. In this article, I am going to explain how GraphQL technology is integrated into a Java Spring Boot project and how it can be used in a sample project that I am going to create.

The main purpose of the article will be on the integration and use of GraphQL technology into the Java Spring Boot application, so I will briefly mention the definition of GraphQL, how it works, and why it is preferred, and then I will start to create the sample project, which is the main purpose of the article.

What is the GraphQL

GraphQL is an open source query language developed by Facebook. GraphQL is defined as an application level query language. So that means, GraphQL queries at application level, not database level.. It provides a more efficient way to design, create and use endpoints. Clients who need data request the data they need by describing them according to a specific scheme.

How GraphQL Works

It communicates over a common definition file called client and server with schema. This schema has a syntax called the Schema Definition Language. The SDL defines which data to take and the relationship between these data. It also provides validation between SDL client and server.

What are the Advantages

In the GraphQL architecture, the request is queried on the GraphQL server and the server returns only the queried values. It also allows you to specify a condition (argument). In RESTful services, this will either return all or return data in a predefined number or condition unless the parameter is used.

There is no need for versioning. Since it is schema based, adding or removing a new field to the API is not a problem.

Provides less bandwidth usage. Data can be taken from multiple sources with a single request. It saves time as the round trip decreases.

Another advantage is that all transactions can be done on a single endpoint. Your endpoints will not increase as the type of data you have increases.

REST vs GraphQL

Let’s move to create the project and start coding :)

Creating Sample Spring Boot Project with GraphQL

In this project, we will have a small scenario based on the Real Estate industry. We will have two fields as Listing and Trade Type. While a listing will have only one Trade Type, a Trade Type may have more than one Listing. We’ll be building a One to Many relationship. Within the scope of the project, we will be using a PostgreSQL running on Docker as a database and we will develop our project with a Code First approach.

The GraphQL library I will use in the project will be https://github.com/graphql-java-kickstart/graphql-spring-boot.

After creating an empty Spring Boot project, we add the following dependencies to the pom.xml file:

pom.xml

In the “resources” folder under the “main” folder, we create “application.properties” and “docker-compose.yml” files to manage the docker images and add the following:

docker-compose.yml
application.properties

Now we create the “graphql” folder where we will keep the GraphQL schemes in the “resources” folder. Under this folder, we create the “mutation” folder where we will keep the actions (DELETE, PUT, POST) in which we will make changes in the database, the “query” folder where we will keep the actions (GET) that bring data from the data, and the “type” folder where we will keep our types and enums. Then we create “enum” and “inputType” folders under the “type” folder.

Now we create the “Listing.graphqls” and “TradeType.graphqls” files under the “type” folder in accordance with our scenario and add the following:

Listing.graphqls
TradeType.graphqls

In the Listing Type, the currency field will be keep an enum. For this, we create the “Currency.graphqls” file in the “enum” folder and add the following:

Currency.graphqls

For the input types we will use in mutation operations, we create “ListingInput.graphqls” and “TradeTypeInput.graphqls” files under the “inputType” folder and add the following to these files:

ListingInput.graphqls
TradeTypeInput.graphqls

In our Input Types as you can see, there are exclamation mark(!) after some fields. This means mark fields as required. Clients can not perform mutation operations without sending required fields.

Now we can create our query and mutation schemes.

They will be added under the “mutation” folder:

TradeTypeMutation.graphqls
ListingMutation.graphqls

Graphql schema works with a single Mutation and Query type. The fields specified with the “extend” keyword are turned into a single mutation and query type when the application is run and combined with the mutation and query type that does not “extend”.

They will be added under the “query” folder:

TradeTypeQuery.graphqls
ListingQuery.graphqls

Skip and take fields are added for pagination in requests that we return a list from the database.

Yes, we completed our GraphQL configuration after creating our schemas. Now, let’s do the operations where we will determine the actions that will map our Query and Mutation schemes on the Java Spring Boot side.

We create a folder in the “java” folder under the “src” folder. “com.springboot.graphql”. We create the “Application” class where we keep the “main” method.

Then we create “dto”, “entity”, “helper”, “repository”, “resolver” and “services” folders.

Firstly, we are going to create our entities to map with the database. After creating “Listing” and “TradeType” classes under the “entity” folder, we are going to create “enums” folder and add the following:

Listing
TradeType
Currency

After creating entities and enums, lets move to the resolvers.

Resolvers help to map queries and mutations schemas which is in “graphql” folder, in Java side. With resolvers, we can connect our queries with service and repository. We will keep our resolvers in the related folders and for this, we will create “listing” and “tradetype” folder under the “resolver” folder. Add following classes and codes related folders:

ListingMutationResolver
ListingQueryResolver
TradeTypeMutationResolver
TradeTypeQueryResolver

So we defined our resolvers. So we defined our resolvers. We should be sure that resolvers name and in the types name in schema are equal otherwise mapping processes are not successfully done. We should be also mark resolvers as “@Component” annotations.

Now let’s create services and repositories. Rest of this article, we will continue with listing. You can add related processes for trade type, I will also attach source code of this project at end of the article so you can check for trade type operations.

In “service” folder, we will create a“listing” folder. Here, we will create “ListingService” interface and “ListingServiceImpl” class and add following:

ListingService
ListingServiceImpl

For converting ListingInput to Input entity, we will create “MapListing” class in “helper” folder and add following:

MapListing

We are going to create “ListingRepository” interface under the “repository” folder and add following:

ListingRepository

For trade type, service, repository and map operations added. Now it’s time to test :)

We are running the application and go to http://localhost:8010/playground link by browser. A GraphQL Playground will welcome us.

Example queries for trade type:

Example queries for listing:

We also added Voyager extension to pom.xml. It provide us a dashboard for our queries, mutations and types. We can access with http://localhost:8010/voyager

Conclusion

With this article, we learned how to get to know GraphQL at a basic level and how to integrate it into a Java Spring Boot project. In a bigger project, subjects such as authentication, exception handling, file upload, pagination on sub-fields should also be addressed.

We are advancing the mobile api projects used in individual and corporate app projects that we will renew at Emlakjet, using GraphQL. We will inform you about the details after the projects are completed.

On this occasion, I would like to thank Ahmet seğmen and Mehmet Ali Erdin for giving me an idea to write this article :)

You can find the source codes of this project here.

Resources:

--

--