Introduction to GraphQL API in Java
\n\nGraphQL 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\nIn 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.
This article will be useful for Java developers who want to master a modern approach to building APIs and improve client-server interaction.
\n\n1. Project Setup and Dependencies
\n\nFirst, 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
After creating the project, add the main GraphQL library to pom.xml:
<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\nYou 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\nAfter adding the dependencies, update the project and run it. By default, GraphiQL will be available at http://localhost:8080/graphiql.
2. Creating the Data Model and GraphQL Schema
\n\nFor demonstration, let's create a simple Book model. Define a class with fields id, title, author, and pages.
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\nNow we need to define the GraphQL schema. Create a file schema.graphqls in the src/main/resources folder:
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\nThe schema describes two queries: getting all books and getting a book by ID. The Book type contains four fields.
\n\nTo handle queries, let's create a service that will store data in memory (for simplicity):
\n\nimport 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