@MutationMapping is the Spring for GraphQL annotation that binds a Java method to a field of the GraphQL Mutation type. When a client sends a mutation operation, Spring GraphQL calls the annotated method, passes the arguments from the query, and returns the result as the mutation's response.
It is a shortcut for @SchemaMapping(typeName = "Mutation"). By default the method name must match the schema field name, or you can set it with name or value.
GraphQL separates reads (Query) from writes (Mutation). Spring for GraphQL discovers annotated handler methods on any Spring bean that is annotated with @Controller, and registers each one as a data fetcher for the matching field. The sibling annotations are @QueryMapping and @SubscriptionMapping.
Use @Controller, not @RestController: GraphQL responses are produced by the GraphQL engine, not by HTTP message converters.
Schema files live under src/main/resources/graphql/ and end with .graphqls. Add the starter spring-boot-starter-graphql (plus spring-boot-starter-web or WebFlux) to enable it.
The schema above is written in the GraphQL schema language, so it belongs in a .graphqls file and not in Java code. The controller:
A client calls it like this:
Method parameters are bound from the mutation's arguments with @Argument. Spring converts the argument map into your type: a scalar, a record or a class for an input type, or a list. The parameter name must match the schema argument name, which works without extra setup when you compile with -parameters (Spring Boot's Maven and Gradle plugins do this by default). Otherwise set it explicitly with @Argument(name = "id").
For many arguments you can also bind the whole map with @Arguments, or take a DataFetchingEnvironment when you need the selected fields.
Prefer a single input type for non-trivial mutations. It keeps the schema stable when you add fields, and it maps neatly onto a Java record.
A mutation method can return a plain value, Mono, Flux, CompletableFuture or a Callable. In a WebFlux application, return Mono<Product> and keep the pipeline reactive.
With Spring MVC on Java 21 or later, you can enable virtual threads with spring.threads.virtual.enabled=true and keep writing blocking code.
Mutations change state, so apply the normal Spring tools:
@Valid to an @Argument parameter and Bean Validation annotations to the input class or record.@Transactional on the service that the controller calls, not on the controller.@PreAuthorize on the mutation method, or on the service, with Spring Security method security enabled.DataFetcherExceptionResolver (or extend DataFetcherExceptionResolverAdapter) to turn it into a GraphQL error with a stable code.Use @GraphQlTest with GraphQlTester for a slice test of the controller.
They map to different root types. @QueryMapping handles Query fields, which should not change data. @MutationMapping handles Mutation fields, which are executed serially and are meant for writes.
By default yes. If you want a different method name, use @MutationMapping("addProduct") or @MutationMapping(name = "addProduct").
Use @Controller. The GraphQL engine writes the response, so @RestController adds nothing and can confuse readers.
Throw an exception and resolve it with a DataFetcherExceptionResolver, or return a result type in the schema (for example a union of success and error types) for expected business errors.
@MutationMapping ties a Java method on a @Controller bean to a field of the GraphQL Mutation type. Combine it with @Argument for inputs, return plain or reactive values, add validation and security as for any Spring service, and cover it with @GraphQlTest.