@SchemaMapping binds a Java method in a Spring controller to one field of one GraphQL type, so Spring for GraphQL calls that method whenever a query selects the field. It is the general-purpose annotation behind resolvers for fields that are not plain properties of the returned object, such as Book.author. The shortcuts @QueryMapping, @MutationMapping and @SubscriptionMapping are specialisations of it for root operation types.
The method receives the parent object (the value already resolved for the type) and returns the field value, which lets you load related data only when a client asks for it.
Spring for GraphQL scans @Controller beans for annotated handler methods and registers each one as a DataFetcher on the matching type and field of your schema. The annotation has two attributes:
| Attribute | Meaning | Default |
|---|---|---|
typeName | GraphQL type that owns the field | Simple name of the parent argument type, or the class-level value |
field (alias value) | Name of the field | Name of the method |
Schema files live under src/main/resources/graphql/ with the .graphqls extension and are picked up automatically by Spring Boot.
Add spring-boot-starter-graphql (plus spring-boot-starter-web or spring-boot-starter-webflux) and write the controller. The Book record has only an authorId, so author needs its own resolver.
@QueryMapping maps the method bookById to Query.bookById. The author method takes a Book parameter, so Spring treats it as the source object of the Book type and passes in the book that was just fetched. If a query never selects author, that method never runs.
Because the type name is inferred from the Book parameter, @SchemaMapping alone is enough when the method name equals the field name:
When one controller handles many fields of the same type, declare typeName once on the class:
Handler methods can declare these arguments:
@Argument for a field argument, with automatic binding to records or classes.@Arguments for all arguments bound to one object.DataFetchingEnvironment, DataFetchingFieldSelectionSet and GraphQLContext for lower-level access.@ContextValue for a value placed in the GraphQL context.java.security.Principal or @AuthenticationPrincipal when Spring Security is present.The return type may be a plain value, Optional, Mono, Flux, CompletableFuture or Callable. Returning Callable runs the work on a separate executor, which is useful for blocking calls.
A per-field resolver runs once for every parent object. For a query that returns 100 books, the author method above would hit the database 100 times. @BatchMapping solves that by receiving all parents at once:
Under the hood this uses a DataLoader, so one batched query replaces many single lookups. Prefer @BatchMapping for any field that triggers I/O and appears inside lists.
Enable GraphiQL in development with spring.graphql.graphiql.enabled=true and open /graphiql. For automated tests, use @GraphQlTest(BookController.class) with GraphQlTester:
@MockitoBean replaces the older @MockBean in Spring Framework 6.2 and later.
@QueryMapping is a shortcut for @SchemaMapping(typeName = "Query"). Use it for root query fields and use @SchemaMapping for fields on any other type.
No. Fields that match a property or record component of the returned object are resolved automatically. Add @SchemaMapping only for fields that need custom logic or extra data.
Yes. Mono, Flux and CompletableFuture are all supported, so the same annotation works in WebFlux and Spring MVC applications.
The field is not found by default. Set field (or value) explicitly, for example @SchemaMapping(typeName = "Book", field = "author").
@SchemaMapping attaches a controller method to a GraphQL type and field, with the type name taken from the source argument, the class, or the annotation and the field name taken from the method unless you override it. Use @QueryMapping, @MutationMapping and @SubscriptionMapping for root types, and @BatchMapping whenever a field resolver would otherwise cause N+1 queries.