Spring Boot is a popular framework for building microservices in Java. It simplifies the bootstrapping and development of a new Spring application. GraphQL, on the other hand, is a query language for APIs and a runtime for executing those queries with your existing data. It provides an efficient and powerful alternative to REST and offers significant benefits over it. In this blog post, we will learn how to provide a GraphQL endpoint in a Spring Boot application.
Setting Up the Project
First, we need to set up a new Spring Boot project. You can do this using the Spring Initializr web tool or directly in your IDE if it supports it. Make sure to include the Web and JPA dependencies.
Next, we need to add the necessary GraphQL dependencies to our pom.xml:
##language-xml
<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>
Defining the Schema
GraphQL uses a strong type system to define the capabilities of an API. All the types that are exposed in an API are written down in a schema using the GraphQL Schema Definition Language (SDL). This schema serves as the contract between the client and the server.
Let's define a simple schema for a blog post application:
##language-json
type Post {
id: ID!
title: String!
content: String!
author: String!
}
type Query {
allPosts: [Post]
post(id: ID!): Post
}
type Mutation {
createPost(title: String!, content: String!, author: String!): Post!
}
This schema file should be placed in src/main/resources/graphql directory.
Implementing the Resolvers
Next, we need to implement the resolvers for our schema. Resolvers in GraphQL are functions that resolve data for your GraphQL types. In Spring Boot, we can define these resolvers as methods in a @Component class.
##language-java
@Component
public class GraphQLResolvers implements GraphQLQueryResolver, GraphQLMutationResolver {
@Autowired
private PostRepository postRepository;
public Iterable<Post> allPosts() {
return postRepository.findAll();
}
public Post post(Long id) {
return postRepository.findById(id).orElse(null);
}
public Post createPost(String title, String content, String author) {
Post post = new Post(title, content, author);
postRepository.save(post);
return post;
}
}
Testing the GraphQL Endpoint
Now, we are ready to test our GraphQL endpoint. If you start the Spring Boot application, GraphQL endpoint will be available at http://localhost:8080/graphql. You can use any HTTP client to send a POST request to this endpoint. The body of the request should be a JSON object with a query field containing your GraphQL query.
Here is an example of a query to fetch all posts:
##language-json
{
"query": "{ allPosts { id title content author } }"
}
And here is a mutation to create a new post:
##language-json
{
"query": "mutation { createPost(title: \"New Post\", content: \"This is a new post.\", author: \"John Doe\") { id title } }"
}