Building GraphQL APIs with Spring Boot

GraphQL is a powerful alternative to REST for managing and querying APIs, offering developers the ability to query only the data they need—not more or less. This can lead to more efficient and flexible data retrieval strategies. In this post, we’ll explore how to create GraphQL APIs in a Spring Boot application, leveraging the Spring Boot GraphQL integration.

What is GraphQL?

GraphQL is a query language for APIs, as well as a runtime for executing those queries. Key features include:

  • Single Endpoint: All requests go through a single endpoint, making API management simpler.
  • Flexible Queries: Clients can request exactly what they need, reducing data transfer and improving performance.
  • Strongly Typed Schema: GraphQL uses a schema to define the capabilities of the API, including types and operations.

Setting Up Your Spring Boot Application with GraphQL

Follow these steps to create a Spring Boot application that utilizes GraphQL:

1. Create a Spring Boot Project

Use Spring Initializr to create a new project with the following dependencies:

  • Spring Web
  • Spring Boot Starter GraphQL
  • Spring Data JPA
  • H2 Database (for testing purposes)

2. Adding Dependencies

Add the necessary dependencies to your pom.xml file:

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

<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphql-spring-boot-starter</artifactId>
    <version>11.1.0</version>
</dependency>

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

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

3. Creating the Domain Model

Next, create an entity to represent the data you want to expose through your GraphQL API. For example, let’s create a Book entity:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;

    // Getters and Setters
}

4. Creating a Repository Interface

Next, create a repository interface for the Book entity:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
}

5. Defining GraphQL Resolvers

Now, create a resolver to fetch data from the repository:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import graphql.kickstart.tools.GraphQLQueryResolver;
import java.util.List;

@Component
public class BookQueryResolver implements GraphQLQueryResolver {

    @Autowired
    private BookRepository bookRepository;

    public List<Book> allBooks() {
        return bookRepository.findAll();
    }

    public Book book(Long id) {
        return bookRepository.findById(id).orElse(null);
    }
}

6. Running Your Application

Once all the pieces are in place, run your Spring Boot application. You can now access your GraphQL API via:

http://localhost:8080/graphql

7. Making GraphQL Queries

You can use a tool like GraphiQL to test your API. Here’s an example query to get all books:

{ 
    allBooks { 
        id 
        title 
        author 
    } 
}

Conclusion

Integrating GraphQL with Spring Boot allows you to build flexible and efficient APIs that provide precisely the data clients need. This integration not only enhances the development experience but also improves performance when managing and querying data.

To explore more about GraphQL and its advanced capabilities, consider checking out the extensive resources offered by ITER Academy to help you deepen your technical skills in modern application development.

To learn more about ITER Academy, visit our website.

Scroll to Top