Setting Up Spring for GraphQL (spring-graphql)
Fast recall
Quick revision
Core answer
Whether you've actually built a Spring for GraphQL service — schema-first resolvers with `@Controller` + `@QueryMapping`/`@MutationMapping`/`@SchemaMapping`, WebSocket subscription support, and GraphiQL dev tooling.
Using the deprecated graphql-java-tools or Netflix DGS tutorial verbatim on a Spring 6/Boot 3 project instead of the official `spring-boot-starter-graphql`. Or wiring custom servlet endpoints instead of letting the starter auto-configure `/graphql`.
Explain `RuntimeWiringConfigurer` for scalars/directives, `@BatchMapping` for DataLoader-backed resolvers, testing with `GraphQlTester`, and how Spring for GraphQL integrates with Spring Security for per-field authorization.
Interview-ready guide
Interview answer
Setting up Spring for GraphQL is three pieces: dependency, schema files, and resolver beans. The official Spring project is simpler to integrate than older libraries and provides native testing support.
Dependency and auto-configuration:
- Add
spring-boot-starter-graphqland a web starter topom.xml; Boot auto-configures the GraphQL engine and a/graphqlHTTP endpoint with zero additional wiring or factory beans - Drop schema files into
src/main/resources/graphql/with a.graphqlsextension; Boot discovers and merges them automatically, so you can split schemas by domain —book.graphqls,author.graphqls— and they compose into one schema
Resolver conventions:
- Resolvers are plain
@Controllerbeans:@QueryMappinghandles reads,@MutationMappinghandles writes,@SchemaMappingresolves nested fields likeBook.author - Method names match schema field names by convention, so there's almost no boilerplate; use
@SchemaMapping(typeName, field)explicitly only when names diverge
What to wire from day one:
- Enable GraphiQL in dev profiles so the team can explore the schema before any client code exists — it's a built-in IDE for your API
- Wire DataLoader for any
@SchemaMappingthat resolves to a related entity immediately, not after the N+1 problem surfaces in production; retrofitting DataLoader into existing resolvers is significantly harder
Why Spring for GraphQL over alternatives:
- Integrates natively with
@GraphQlTestslice tests, Spring Security filters, and Actuator; older libraries like DGS work but lack this cohesion with the Spring ecosystem
I recommend enabling GraphiQL in dev and setting up DataLoader for nested fields on the first day of development — both are much easier to start with than to add retroactively.
Setting Up Spring for GraphQL (spring-graphql)
Direct Answer
Add the spring-boot-starter-GraphQL dependency, define your schema in src/main/resources/graphql/*.graphqls files, annotate a Spring bean with @Controller and map resolver methods using @QueryMapping, @MutationMapping, and @SubscriptionMapping. Spring Boot auto-configures the GraphQL engine and exposes it at /graphql.
Common Mistakes & Pitfalls
Using the deprecated graphql-java-tools or Netflix DGS tutorial verbatim on a Spring 6/Boot 3 project instead of the official spring-boot-starter-graphql. Or wiring custom servlet endpoints instead of letting the starter auto-configure /graphql.
Check your understanding
Answer these without looking back at the article.
- How do you handle input types vs scalar arguments in @Argument?
- What is the difference between @SchemaMapping and @QueryMapping?
- How do you enable subscriptions over WebSocket with spring-graphql?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.