Chapter 8.5☕ 16 min read

Spring Boot GraphQL

REST gives you a fixed Thali. GraphQL lets you pick only the dishes you want.

01The Concept: Query Languages

The Paradise Takeaway Parcel Analogy:

In a REST API (Thali system), you order the “Veg Thali”. It comes with Biryani, Salan, Dal, and Curd. If you only want Biryani and Salan, too bad, you get the whole plate.

In GraphQL, you go to the counter and say: “I want Biryani, and 50ml of Salan. That’s it.” The server packs exactly those items. The client dictates the shape of the data, not the server.

02Technical Explanation
  1. Schema: A .graphqls file where you define the data types and allowed queries.
  2. @QueryMapping: A Spring method that handles a specific GraphQL query.
  3. Spring for GraphQL: The modern Spring Boot starter for building GraphQL APIs.
03Full Working Code: GraphQL API

Add the GraphQL dependency.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>

1. The Schema (src/main/resources/graphql/schema.graphqls)

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

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

2. The Java Code (BookController.java)

package com.devinhyderabad;

import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.stereotype.Controller;

@Controller
public class BookController {

@QueryMapping
public Book bookById(@Argument Long id) {
return new Book(id, "GraphQL Guide", "Deva");
}
}

class Book {
private Long id;
private String title;
private String author;

public Book(Long id, String title, String author) {
this.id = id; this.title = title; this.author = author;
}
public Long getId() { return id; }
public String getTitle() { return title; }
public String getAuthor() { return author; }
}

Start the app and go to http://localhost:8080/graphiql (GraphiQL UI) and write this query:

query {
bookById(id: 1) {
title
author
}
}

The server returns exactly: {"data":{"bookById":{"title":"GraphQL Guide","author":"Deva"}}}. It won’t return the ID because you didn’t ask for it!

04Code Walkthrough

The GraphQL setup is different from REST:

  • Single Endpoint: Unlike REST which has /api/books, /api/authors, etc., GraphQL has a single endpoint (by default /graphql). The client decides what data it wants.
  • Schema-Driven: The schema.graphqls file defines exactly what queries are possible. The ! means non-nullable (required).
  • @QueryMapping: Automatically maps the method bookById to the GraphQL query bookById in the schema. The @Argument annotation injects the parameter value from the query.
  • GraphiQL: Spring Boot auto-configures the GraphiQL UI at /graphiql for development testing.
05Why It Matters / Interview Note

Interview Question: “What is the N+1 problem in GraphQL, and how do you solve it in Spring Boot?”

Answer: If a query fetches a list of 10 Authors, and then the client asks for each Author’s Books, GraphQL might execute 1 query to fetch authors, and 10 separate queries to fetch books for each author. In Spring Boot, we solve this using @BatchMapping or GraphQL Java’s DataLoader, which batches the 10 queries into a single WHERE author_id IN (...) SQL query.

Enterprise Note: GraphQL is great for frontend developers, but it puts a heavy load on backend parsing and security. You must implement depth-limiting (preventing nested queries like user->friends->friends->friends which can crash the server) and rate limiting.

Key Takeaways

  • ✅ GraphQL lets the client request exactly the fields it needs (no over-fetching)
  • ✅ The schema (.graphqls) defines data types and allowed operations
  • ✅ @QueryMapping maps Java methods to GraphQL queries
  • ✅ Spring Boot auto-configures GraphiQL at /graphiql for testing
  • ✅ N+1 problem is solved with @BatchMapping or DataLoader