GraphQL is an API standard that has been gaining popularity in recent years due to its flexibility and efficiency. It allows clients to specify exactly what data they need and in what format, which can reduce the amount of data sent over the wire and improve performance. In this article, we'll explore how to build GraphQL APIs with Spring Boot.
Getting Started with GraphQL
To get started with GraphQL, you need to define a schema that describes the types of data that your API can return. A schema consists of a set of types, each of which has one or more fields. Fields can be of different types, including scalars (like strings and integers), objects (defined by other types), and lists (of other types). Here's an example schema for a simple blog API:
type Query {
posts: [Post]
}
type Post {
id: ID!
title: String!
body: String!
author: User!
}
type User {
id: ID!
name: String!
}
This schema defines three types: Query, Post, and User. The Query type has one field, posts, which returns a list of Post objects. The Post type has four fields: id, title, body, and author. The author field is an object of type User. The User type has two fields: id and name.
Building a GraphQL API with Spring Boot
Now that we have a schema, let's build a GraphQL API with Spring Boot. We'll use the graphql-java library to parse and execute GraphQL queries.
First, add the following dependencies to your build.gradle or pom.xml file:
dependencies {
implementation 'com.graphql-java:graphql-spring-boot-starter:15.0.0'
implementation 'com.graphql-java:graphql-java-tools:15.0.2'
}
Next, create a GraphQL controller that handles GraphQL queries. Here's an example controller:
@RestController
@RequestMapping("/graphql")
public class GraphQLController {
private final GraphQL graphQL;
public GraphQLController(GraphQL graphQL) {
this.graphQL = graphQL;
}
@PostMapping
public ResponseEntity<Object> query(@RequestBody String query) {
ExecutionResult result = graphQL.execute(query);
return ResponseEntity.ok(result.getData());
}
}
This controller maps HTTP POST requests to the /graphql endpoint and executes the GraphQL query passed in the request body. The result of the query is returned in the response.
To create a GraphQL schema from our schema definition, we'll use the graphql-java-tools library. Add the following code to your main Spring Boot application class:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Bean
public GraphQLSchema schema() {
TypeRegistry typeRegistry = new TypeRegistry();
SchemaParser schemaParser = new SchemaParser();
typeRegistry.merge(schemaParser.parse(
"type Query { posts: [Post] }" +
"type Post { id: ID!, title: String!, body: String!, author: User! }" +
"type User { id: ID!, name: String! }"
));
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", builder -> builder.dataFetcher("posts", new PostsFetcher()))
.type("Post", builder -> builder.dataFetcher("author", new AuthorFetcher()))
.build();
SchemaGenerator schemaGenerator = new SchemaGenerator();
return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring)
Top comments (0)