GraphQL is a query language for APIs, and it’s a powerful alternative to REST. Spring Boot, combined with the Spring GraphQL module, makes it easy to create GraphQL-based APIs.
Prerequisites:
- Basic knowledge of Spring Boot and Java.
- JDK 8 or later installed on your machine.
- An Integrated Development Environment (IDE) such as IntelliJ IDEA or Eclipse.
Let’s go through the steps:
Step 1: Set Up a Spring Boot Project
Create a new Spring Boot project using your preferred method (Spring Initializer or your IDE). Include the “Spring Web” and “Spring Data JPA” dependencies.
Step 2: Add GraphQL Dependencies
Open your project’s pom.xml
file and add the necessary dependencies for GraphQL. Here’s an example:
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL Connector for database connection -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Jakarta Persistence -->
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- GraphQL Dependencies -->
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>15.0.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>13.0.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-extended-scalars</artifactId>
<version>22.0</version>
</dependency>
<!-- Spring Boot Starter Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Step 3: Create GraphQL Schema
Create a GraphQL schema in a file named schema.graphqls
in the src/main/resources
folder. This file defines your GraphQL types, queries, and mutations. Here’s an example:
Step 4: Create Entities and Repositories
Create a Book
entity and a corresponding repository. For example:
Step 5: Create GraphQL Resolvers
Create resolvers that handle GraphQL queries and mutations. Create a class named GraphQLResolver
:
Step 6: Run the Application
Run your Spring Boot application. By default, the GraphQL endpoint will be available at http://localhost:8080/graphql
. You can access this URL using tools like GraphiQL or Insomnia.
Step 7: Test GraphQL Queries and Mutations
Open your GraphQL tool and try out some queries and mutations:
# Query to get all books
query {
getAllBooks {
id
title
author
}
}
# Query to get a book by ID
query {
getBookById(id: 1) {
id
title
author
}
}
# Mutation to add a new book
mutation {
addBook(title: "Spring Boot GraphQL", author: "John Doe") {
id
title
author
}
}
curl -X POST http://localhost:8080/graphql \
-H "Content-Type: application/json" \
-d '{"query": "query { getAllBooks { id title author } }"}'
That’s it! You’ve successfully created a Spring Boot application with GraphQL support. You can extend this example by adding more entities, queries, and mutations based on your application’s requirements.