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

GraphQL is a modern query language for APIs that allows clients to request only the data they actually need. Unlike REST, where the response structure is fixed, GraphQL provides flexibility and efficiency. In this step-by-step tutorial, we will create a full-fledged GraphQL API in Java using the popular graphql-java library and Spring Boot. You will learn how to set up a server, define a schema, write resolvers, and execute queries. All code examples are ready to run.



What you will learn

  • How to set up a Spring Boot project for GraphQL
  • How to define a GraphQL schema (types, queries, mutations)
  • How to write resolvers in Java
  • How to execute query and mutation requests


1. Project Setup and Dependencies

First, let's create a Spring Boot project with Maven. Add the necessary dependencies to pom.xml:

<dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <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></dependencies>

These libraries provide integration of GraphQL with Spring Boot and convenient work with schemas. After adding the dependencies, update the project.



Project Structure

Our project will contain the following main components:

  • GraphQL Schema (schema.graphqls file)
  • Data Models (Java POJO)
  • Resolvers (query handlers)
  • Controller for testing


2. Defining the GraphQL Schema

Create the file src/main/resources/schema.graphqls. In it, we will describe the data types and available operations. For example, let's take the "Book" model:

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

type Query { getBook(id: ID!): Book getAllBooks: [Book]}

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

Here Book is an object type with required fields (!). Query defines entry points for reading data, and Mutation for modifying it. Note: in GraphQL, mutations are used for creating, updating, and deleting data.



3. Creating Java Models and Resolvers

Book Model

Let's create a simple POJO class Book.java:

public class Book {    private String id;    private String title;    private String author;    private int year;

public Book(String id, String title, String author, int year) { this.id = id; this.title = title; this.author = author; this.year = year; }

// getters and setters public String getId() { return id; } public String getTitle() { return title; } public String getAuthor() { return author; } public int getYear() { return year; }}


Resolver for Query

Resolvers connect the schema to the data. Let's create the class QueryResolver.java:

import com.coxautodev.graphql.tools.GraphQLQueryResolver;import org.springframework.stereotype.Component;import java.util.*;

@Componentpublic class QueryResolver implements GraphQLQueryResolver {

private final List<Book

Blogs

Book Recommendations