@MutationMapping marks a controller method as the data fetcher for a field of the GraphQL Mutation type in Spring for GraphQL. It is a shortcut for @SchemaMapping(typeName = "Mutation"), so Spring wires the method to the schema field with the same name and calls it when a client sends that mutation.
Mutations are the write side of GraphQL: they create, update or delete data. The annotation lets you implement them as ordinary annotated controller methods, much like @PostMapping in Spring MVC.
Spring for GraphQL is the Spring project that integrates GraphQL Java with Spring Boot. Instead of registering data fetchers by hand, you annotate methods in a @Controller class. The annotations are in the package org.springframework.graphql.data.method.annotation:
| Annotation | GraphQL operation |
|---|---|
@QueryMapping | Field of the Query type |
@MutationMapping | Field of the Mutation type |
@SubscriptionMapping | Field of the Subscription type |
@SchemaMapping | Any field of any type |
Use @Controller, not @RestController. The response is produced by the GraphQL engine, not by HTTP message conversion.
Start from the schema. By default Spring Boot loads *.graphqls files from src/main/resources/graphql/.
Then implement the mutations. The method name must match the schema field, or you set the field name explicitly with the name attribute.
@Argument binds a GraphQL argument to a method parameter. For input objects, Spring binds the map of values to your class or record. Bean Validation works on the bound object when the controller is @Validated and the parameter is @Valid.
A client sends the mutation to the /graphql endpoint:
Only the fields requested in the selection set are returned, even though the method returns the whole Book.
The GraphQL specification requires top-level mutation fields to run serially, in the order the client wrote them. Query fields may run in parallel, but mutations do not, which makes multi-step changes predictable.
Spring for GraphQL does not open a database transaction for you. Put @Transactional on the service method that does the write, not on the controller, so each mutation commits or rolls back as one unit.
An exception thrown from a @MutationMapping method becomes an entry in the GraphQL errors array. By default Spring hides internal details and reports a generic internal error, which avoids leaking stack traces. For expected failures, such as a duplicate title, map them to meaningful errors with a @GraphQlExceptionHandler method in the controller or in a @ControllerAdvice class, or provide a DataFetcherExceptionResolver.
Mutations change data, so protect them. With Spring Security you can use method security on the controller or service:
Enable it with @EnableMethodSecurity on a configuration class.
@MutationMapping is a specialisation of @SchemaMapping with typeName fixed to Mutation. Use @SchemaMapping when you resolve a field on another type, such as Book.author.
Yes. Controller methods may return plain values, Mono, Flux or CompletableFuture. Spring for GraphQL adapts the result for the GraphQL engine.
The method name does not match the schema field, or the controller is not a Spring bean. Check the spelling, or set the field explicitly with @MutationMapping(name = "...").
No. It belongs to the separate Spring for GraphQL project, added to Spring Boot through spring-boot-starter-graphql.
@MutationMapping connects a controller method to a field of the GraphQL Mutation type. Match the method or name to the schema field, bind inputs with @Argument, validate them, keep transactions in the service layer, and secure write operations like any other endpoint.