Quick Start Guide: Integrating GraphQL into a Spring Boot Application
This tutorial shows how to quickly set up GraphQL in a Spring Boot web project by creating the project, adding Maven dependencies, defining schema files, implementing providers and resolvers, exposing a GraphQL endpoint, and testing query and mutation operations.
In this article the author, a senior architect, demonstrates how to quickly integrate GraphQL into a Spring Boot web project using a flexible approach different from the official graphql‑java‑kickstart starter.
Quick start : Create a Spring Boot project via Spring Initializr, replace application.properties with application.yml, and add the required Maven dependencies (spring‑boot‑starter‑web, lombok, graphql‑java‑tools, gson, etc.).
Define the GraphQL schema files base.graphqls and item.graphqls that declare queries, mutations, and types.
Implement a GraphQLProvider component that builds the executable schema from the .graphqls files and registers a GraphQL bean:
@Component
public class GraphQLProvider {
private GraphQL graphQL;
@Autowired
private IItemService itemService;
@Bean
public GraphQL graphQL() {
return graphQL;
}
@PostConstruct
public void init() throws IOException {
GraphQLSchema graphQLSchema = SchemaParser.newParser()
.file("graphql/base.graphqls")
.resolvers(new Query(), new Mutation())
.file("graphql/item.graphqls")
.resolvers(new ItemResolver(itemService))
.build().makeExecutableSchema();
this.graphQL = GraphQL.newGraphQL(graphQLSchema).build();
}
}Create an ItemResolver class implementing GraphQLQueryResolver and GraphQLMutationResolver to delegate queries and mutations to a service layer:
public class ItemResolver implements GraphQLQueryResolver, GraphQLMutationResolver {
private IItemService itemService;
public ItemResolver(IItemService itemService) { this.itemService = itemService; }
public ItemList queryItemList() { return itemService.queryItemList(); }
public Item queryById(Long id) { return itemService.queryById(id); }
public Item updateName(Param param) { return itemService.updateName(param); }
}Expose a GraphqlController with a /graphql endpoint that receives a GraphqlRequest, executes the query, and returns the result or errors:
@RestController
@RequestMapping("/graphql")
public class GraphqlController {
@Autowired
private GraphQL graphQL;
@PostMapping
public Object execute(@RequestBody GraphqlRequest request) {
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query(request.getQuery())
.variables(request.getVariables())
.build();
ExecutionResult executionResult = graphQL.execute(executionInput);
List<GraphQLError> errors = executionResult.getErrors();
if (errors != null && !errors.isEmpty()) {
Map<String, Object> result = new HashMap<>();
result.put("errors", errors);
return result;
}
return executionResult.getData();
}
}Run the application and test the three provided operations: queryItemList, queryById, and updateName, as shown by the screenshots.
The article concludes that the basic GraphQL configuration is complete and ready for further business development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
