@QueryMapping is the Spring for GraphQL annotation that binds a Java controller method to a field of the root Query type in your GraphQL schema. When a client sends a query for that field, Spring calls the annotated method and returns its result. It replaces manually written DataFetcher registrations with a declarative, Spring MVC style programming model.
It is a shortcut for @SchemaMapping(typeName = "Query"), and it lives in the org.springframework.graphql.data.method.annotation package.
@QueryMapping does@QueryMapping doesA GraphQL schema declares which fields a client can query. Something must supply the data for each field. Without annotations you would implement a DataFetcher for every field and register it with RuntimeWiring. With Spring for GraphQL you write a method on a @Controller class instead, and the framework wires it to the matching schema field at startup.
By default the method name must equal the schema field name. If it does not, set the name explicitly: @QueryMapping("userById"), or use the name attribute.
With Spring Boot, add the GraphQL starter and a web starter:
Spring Boot loads schema files with the .graphqls or .graphql extension from src/main/resources/graphql/ and exposes the endpoint at /graphql. To use the GraphiQL browser tool during development, set spring.graphql.graphiql.enabled=true in application.properties.
Define the schema in src/main/resources/graphql/schema.graphqls:
Then write the controller:
The method user matches the schema field user, and users matches users. The @Argument annotation binds the GraphQL argument id to the Java parameter; without it Spring does not know where the value comes from. Note that the class must be a @Controller; plain @Component classes are not scanned for these mappings.
A client can now send:
and receives only the requested fields:
Annotated methods can accept more than @Argument:
| Parameter | Purpose |
|---|---|
@Argument | A single GraphQL argument, converted to the Java type |
@Arguments | All arguments bound to one object |
DataFetchingEnvironment | Full access to the GraphQL Java environment |
Principal | The authenticated user, when security is configured |
Return types can be a plain object, a collection, Mono or Flux for reactive applications, or CompletableFuture. Spring adapts them for you. Exceptions thrown from the method are turned into GraphQL errors in the response.
@QueryMapping covers the root Query type. Its siblings cover the rest of the schema:
@MutationMapping maps fields of the Mutation type.@SubscriptionMapping maps fields of the Subscription type and returns a Flux.@SchemaMapping maps a field of any type, for example the author field of a Book.@BatchMapping loads a field for many parents at once, which avoids the N+1 problem for nested fields.Use @BatchMapping when a nested field such as Post.author would otherwise trigger one database call per post.
@QueryMapping and @SchemaMapping?@QueryMapping is a shortcut for @SchemaMapping(typeName = "Query"). Use @SchemaMapping when the field belongs to another type.
Usually the method name does not match the schema field, or the class is not a @Controller. Check the names, or set @QueryMapping("fieldName").
@Argument for every parameter?Yes for GraphQL arguments. Parameters without an annotation are only resolved if they are supported types such as DataFetchingEnvironment or Principal.
@QueryMapping without Spring Boot?Yes, Spring for GraphQL works with plain Spring, but Boot's starter handles schema loading and endpoint setup for you.
@QueryMapping connects a field on the GraphQL Query type to a method in a Spring @Controller. Match the method name to the schema field, bind inputs with @Argument, return plain objects or reactive types, and use @BatchMapping for nested fields that would otherwise cause N+1 calls.