Building a GraphQL API with Spring Boot and GraphQL Java
GraphQL is a query language and runtime for APIs that provides a more efficient, powerful, and flexible approach to developing APIs compared to traditional REST APIs. Spring Boot, on the other hand, is a widely used framework for building Java applications. In this article, we will explore how to build a GraphQL API using Spring Boot and GraphQL Java.
Spring Boot와 GraphQL Java를 이용한 GraphQL API 구축
To get started, we need to set up a Spring Boot project with the necessary dependencies. We can do this by creating a new Maven or Gradle project and adding the following dependencies to our build file:
com.graphql-java
graphql-spring-boot-starter
14.0.0
com.graphql-java
graphql-java-tools
5.2.4
Next, we need to define our GraphQL schema and resolvers. We can use the graphql-java-tools
library to create our schema from a .graphqls
file and generate our resolvers. Here’s an example schema:
type Book {
id: ID!
title: String!
author: String!
}
type Query {
books: [Book]
}
This defines a Book
type with an id
, title
, and author
field, as well as a Query
type with a books
field that returns an array of Book
objects.
We can generate our resolvers by creating a datafetcher
for each field in our schema. Here’s an example resolver:
@Component
public class BookResolver implements GraphQLResolver {
public String getAuthor(Book book) {
return "J.K. Rowling";
}
}
This resolver returns the author of a book as a hardcoded value for demonstration purposes. We can also define resolvers for our Query
type, which will be used to fetch data for our API.
GraphQL Java를 이용한 스키마와 리졸버 작성 방법
Now that we have our schema and resolvers, we can create a GraphQL endpoint in our Spring Boot application. We can do this by creating a GraphQL
bean and defining our schema and resolvers:
@Configuration
public class GraphQLConfig {
@Bean
public GraphQL graphQL() {
SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(new InputStreamReader(getClass().getResourceAsStream("/schema.graphqls")));
RuntimeWiring runtimeWiring = buildWiring();
SchemaGenerator schemaGenerator = new SchemaGenerator();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
return GraphQL.newGraphQL(graphQLSchema).build();
}
private RuntimeWiring buildWiring() {
return RuntimeWiring.newRuntimeWiring()
.type("Book", typeWiring -> typeWiring.dataFetcher("author", bookResolver::getAuthor))
.type("Query", typeWiring -> typeWiring.dataFetcher("books", queryResolver::getBooks))
.build();
}
}
This creates a GraphQL
bean that uses our schema and resolvers to handle GraphQL queries. We can define our schema using the SchemaParser
and TypeDefinitionRegistry
classes, and our resolvers using the RuntimeWiring
class.
Spring Boot에서 GraphQL API 구현하기 위한 설정 방법
Finally, we need to configure our Spring Boot application to serve GraphQL queries. We can do this by creating a GraphQLController
that handles GraphQL requests:
@RestController
@RequestMapping("/graphql")
public class GraphQLController {
private final GraphQL graphQL;
public GraphQLController(GraphQL graphQL) {
this.graphQL = graphQL;
}
@PostMapping
public ResponseEntity query(@RequestBody String query) {
ExecutionResult executionResult = graphQL.execute(query);
return new ResponseEntity(executionResult, HttpStatus.OK);
}
}
This creates a REST endpoint at /graphql
that accepts GraphQL queries as a POST request. The GraphQL
bean is injected into the controller, and the query
method executes the query and returns the result as a ResponseEntity
.
With these components in place, we now have a fully functional GraphQL API that can be used to fetch data from our application. We can test our API using a tool like GraphiQL or Insomnia, or by sending GraphQL queries directly to our endpoint.
결론
In this article, we have explored how to build a GraphQL API using Spring Boot and GraphQL Java. We started by defining our schema and resolvers using the graphql-java-tools
library, and then created a GraphQL
bean that used our schema and resolvers to handle queries. Finally, we configured our Spring Boot application to serve GraphQL requests using a GraphQLController
. With these components in place, we now have a powerful and flexible API that can be used to fetch data from our application.