GraphQL API with Java: Step-by-Step Practical Tutorial

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to GraphQL API in Java

\n\n

GraphQL is a query language for APIs that allows clients to request only the data they actually need. Unlike REST, where the response structure is fixed, GraphQL enables flexible response formation, significantly reducing the amount of data transferred and speeding up development.

\n\n

In this tutorial, we will practically explore how to create a GraphQL API with Java using the graphql-java library and the Spring Boot framework. You will learn how to set up a schema, write resolvers, and execute queries.

\n\n

This article will be useful for Java developers who want to master a modern approach to building APIs and improve client-server interaction.

\n\n

1. Project Setup and Dependencies

\n\n

First, let's create a new Spring Boot project. The easiest way is to use Spring Initializr. Add the following dependencies:

\n\n
    \n
  • Spring Web — to run the web server
  • \n
  • Spring Boot DevTools — to speed up development
  • \n
  • Lombok — to reduce boilerplate code
  • \n
\n\n

After creating the project, add the main GraphQL library to pom.xml:

\n\n
<dependency>\n    <groupId>com.graphql-java</groupId>\n    <artifactId>graphql-java-spring-boot-starter-webmvc</artifactId>\n    <version>2021-06-30T00-00-00-1e4b5e5</version>\n</dependency>
\n\n

You will also need a library for working with the GraphiQL tool (a web interface for testing queries):

\n\n
<dependency>\n    <groupId>com.graphql-java</groupId>\n    <artifactId>graphiql-spring-boot-starter</artifactId>\n    <version>5.0.2</version>\n</dependency>
\n\n

After adding the dependencies, update the project and run it. By default, GraphiQL will be available at http://localhost:8080/graphiql.

\n\n

2. Creating the Data Model and GraphQL Schema

\n\n

For demonstration, let's create a simple Book model. Define a class with fields id, title, author, and pages.

\n\n
import lombok.AllArgsConstructor;\nimport lombok.Data;\nimport lombok.NoArgsConstructor;\n\n@Data\n@NoArgsConstructor\n@AllArgsConstructor\npublic class Book {\n    private String id;\n    private String title;\n    private String author;\n    private int pages;\n}
\n\n

Now we need to define the GraphQL schema. Create a file schema.graphqls in the src/main/resources folder:

\n\n
type Query {\n    allBooks: [Book]\n    bookById(id: String): Book\n}\n\ntype Book {\n    id: String\n    title: String\n    author: String\n    pages: Int\n}
\n\n

The schema describes two queries: getting all books and getting a book by ID. The Book type contains four fields.

\n\n

To handle queries, let's create a service that will store data in memory (for simplicity):

\n\n
import org.springframework.stereotype.Service;\nimport javax.annotation.PostConstruct;\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.Optional;\n\n@Service\npublic class BookService {\n    private List<Book> books = new ArrayList<>();\n\n    @PostConstruct\n    public void init() {\n        books.add(new Book("1", "War and Peace", "Leo Tolstoy", 1225));\n        books.add(new Book("2", "Crime and Punishment", "Fyodor Dostoevsky", 671));\n        books.add(new Book("3", "The Master and Margarita", "Mikhail Bulgakov", 480));\n    }\n\n    public List<Book> getAllBooks() {\n        return books;\n    }\n\n    public Book getBookById(String id) {\n        Optional<Boo                

Blogs

Book Recommendations