The spring-boot-starter-graphql dependency adds Spring for GraphQL and GraphQL Java to a Spring Boot application and auto-configures a GraphQL endpoint. You describe your API in a .graphqls schema file and map schema fields to Java methods with annotations such as @QueryMapping, so you do not write the HTTP and execution plumbing yourself.
The starter does not include a web server. Add spring-boot-starter-web (Spring MVC) or spring-boot-starter-webflux next to it.
| Component | Role |
|---|---|
| Spring for GraphQL | Annotated controllers, data fetcher registration, transport support |
| GraphQL Java | The engine that parses, validates and executes queries |
| Auto-configuration | Schema loading, /graphql HTTP endpoint, optional GraphiQL and WebSocket |
Spring Boot loads schema files from classpath:graphql/**/ with the extensions .graphqls or .gqls. Create src/main/resources/graphql/schema.graphqls:
Because this is a schema-first approach, the schema is the contract and the Java code implements it.
A controller class is a normal Spring bean. Each method is bound to a schema field by name, or explicitly through the annotation.
The author method runs only when a client selects the author field, which is how GraphQL avoids over-fetching.
If a query returns 100 books, the author method above runs 100 times. Replace it with a batch method that loads all authors in one call:
Spring for GraphQL wires this through a DataLoader for you.
GraphiQL, the in-browser explorer, is disabled by default; enable it for development only. Then send a query:
@GraphQlTest loads a slice with your controllers, and GraphQlTester runs queries against them.@PreAuthorize work on controller methods.INTERNAL_ERROR classification. Register a DataFetcherExceptionResolver to map your own exceptions to clearer errors.It needs a web stack for HTTP transport. Choose Spring MVC or WebFlux. Without one, you can still run queries programmatically through GraphQlService.
In classpath:graphql/**/ for .graphqls and .gqls files. Change it with spring.graphql.schema.locations.
Spring for GraphQL is schema-first. You can still generate a schema from code with other libraries and register the result, but the annotated-controller model assumes a schema exists.
Disable it there. It is a development tool, and exposing schema exploration publicly is usually undesirable.
spring-boot-starter-graphql gives you a ready GraphQL endpoint backed by GraphQL Java. Write a .graphqls schema, bind fields with @QueryMapping, @MutationMapping and @SchemaMapping, use @BatchMapping to avoid N+1 queries, and pair the starter with a web starter and Spring Security.