GraphQL API with Java: Step-by-Step Tutorial for Beginners

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 and Java

GraphQL is a modern query language for APIs, developed by Facebook. Unlike REST, where each endpoint returns a fixed data structure, GraphQL allows the client to request exactly the fields it needs. This reduces the amount of data transferred and simplifies the development of complex interfaces.

In this tutorial, we will create a simple GraphQL API in Java using Spring Boot and the graphql-java library. You will learn how to set up the project, define the schema, implement resolvers, and test the API. The article is aimed at beginners, so we will proceed step by step.

To get started, you will need: JDK 11+, Maven or Gradle, an IDE (IntelliJ IDEA, Eclipse), and basic knowledge of Java.



1. Setting up a Spring Boot Project

Let's start by creating a new Spring Boot project. The easiest way is to use Spring Initializr. Select the following dependencies:

  • Spring Web
  • Spring Boot DevTools
  • Lombok (optional, to simplify code)

After generating the project, add the GraphQL dependency to pom.xml (or build.gradle):

<dependency>    <groupId>com.graphql-java</groupId>    <artifactId>graphql-spring-boot-starter</artifactId>    <version>5.0.2</version></dependency><dependency>    <groupId>com.graphql-java</groupId>    <artifactId>graphql-java-tools</artifactId>    <version>5.2.4</version></dependency>

These libraries will simplify the integration of GraphQL with Spring Boot. After updating the dependencies, run the project with the command mvn spring-boot:run or through your IDE.



2. Defining the GraphQL Schema

The schema is the foundation of a GraphQL API. It describes data types, queries, and mutations. Create a file schema.graphqls in the src/main/resources folder:

type Book {    id: ID!    title: String!    author: String!    year: Int}

type Query { books: [Book] bookById(id: ID!): Book}

type Mutation { addBook(title: String!, author: String!, year: Int): Book}

Here we defined the Book type with required fields id, title, author and an optional year. Query contains two requests: getting all books and getting a book by ID. Mutation allows adding new books.

Note the exclamation mark — it denotes a required (non-null) field.



3. Implementing Resolvers

Resolvers are Java classes that connect the schema to the data. Let's create a Book class as a data model:

import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;

@Data@NoArgsConstructor@AllArgsConstructorpublic class Book { private String id; private String title; private String author; private Integer year;}

Now let's create a service for storing books (in a real project, this would involve a database connection):

import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;import java.util.ArrayList;import java.util.List;import java.util.Optional;import java.util.UUID;

@Servicepublic class BookService { private List<Book> books = new ArrayList<>();

@PostConstruct public void init() { books.add(new Book("1", "War and Peace", "Leo Tolstoy", 1869)); books.add(new Book("2", "Crime and Punishment", "Fyodor Dostoevsky", 1866)); }

public List<Book> getAllBooks() { return books; }

public Optional<Book> getBookById(String id) { return books.str

Blogs

Book Recommendations