@SubscriptionMapping is the Spring for GraphQL annotation that binds a controller method to a field of the GraphQL Subscription type. The method must return a reactive Publisher, usually a Flux, and every item it emits is pushed to the subscribed client as a separate result. It is how a Spring Boot application streams live data, such as notifications or price updates, over GraphQL.
Despite older tutorials, GraphQL subscriptions do not use STOMP. Spring GraphQL serves them over its own WebSocket transport (the graphql-transport-ws protocol) and, in current versions, also supports Server-Sent Events.
Spring for GraphQL maps controller methods to schema fields with annotations: @QueryMapping, @MutationMapping and @SubscriptionMapping. The annotation is a shortcut for @SchemaMapping(typeName = "Subscription"), and the method name is used as the field name unless you set name or value.
The package is org.springframework.graphql.data.method.annotation.SubscriptionMapping, and the controller is a normal @Controller bean. Unlike queries, a subscription method returns a stream. Each element of the stream becomes one message sent to the client until the stream completes or the client cancels.
Subscriptions are declared on the Subscription root type in a .graphqls file under src/main/resources/graphql/.
You need the GraphQL starter and a WebSocket-capable web stack. With Spring MVC, add spring-boot-starter-graphql and spring-boot-starter-websocket. With WebFlux, spring-boot-starter-graphql and spring-boot-starter-webflux are enough.
Setting spring.graphql.websocket.path is what turns on the WebSocket endpoint, because it is not exposed by default. Do not add @EnableWebSocketMessageBroker or a STOMP configuration: that is a different feature and is not used here.
A subscription method returns Flux<T> where T matches the schema type. This controller exposes a timer and a message feed.
messageReceived returns the sink as a Flux, so every sendMessage mutation is pushed to all active subscribers. tick shows that subscription fields take arguments like any other field.
A Mono is allowed, but it emits one item and completes, so it behaves like a query. Use Flux for real streams. In a larger system, replace the in-memory sink with a broker such as Kafka or Redis, otherwise subscribers on a different instance will not receive events.
Open GraphiQL at /graphiql and run:
In another tab, run mutation { sendMessage(text: "hello") { id } }. The first tab receives the new message. For automated tests, use GraphQlTester with a WebSocket transport, or WebSocketGraphQlTester, and verify the stream with Reactor's StepVerifier.
connection_init message. Spring GraphQL provides interceptors (WebSocketGraphQlInterceptor) to read connection parameters, and Spring Security can protect the endpoint.spring.graphql.websocket.keep-alive, so dead connections are detected.doOnCancel or doFinally to release resources tied to the stream.onBackpressureBuffer with a size limit or onBackpressureDrop, so a slow client cannot grow memory without bound.No. It uses the GraphQL over WebSocket protocol, set up through spring.graphql.websocket.path. Server-Sent Events are supported in current versions as an alternative transport.
Yes, but it emits a single value and finishes. Use Flux for continuing updates.
An in-memory Sinks object only exists in one JVM. Distribute events with a shared broker so every instance can feed its own subscribers.
Declare them on the schema field and read them with @Argument in the method, as in the tick example.
@SubscriptionMapping connects a controller method to a field on the GraphQL Subscription type and requires a Publisher return value, typically Flux. Enable the WebSocket endpoint with spring.graphql.websocket.path, declare the field in the schema, and feed the stream from a sink or a message broker. Skip STOMP, handle cancellation and backpressure, and secure both the connection and the data.