Subscriptions, Errors, and Schema Security
Stream real-time updates over subscriptions and harden the schema with field-level authorization.
Beyond Query and Mutation
Spring for GraphQL supports three root operation types. So far you have used Query (read) and Mutation (write). The third is Subscription — a long-lived stream that pushes data to the client whenever a server-side event occurs.
- Query/Mutation: one request, one response.
- Subscription: one request, many responses over time.
In this lesson you will stream live updates with subscriptions, shape GraphQL errors cleanly, and lock down individual fields with method security.
Declaring a Subscription in the Schema
Subscriptions are declared in the GraphQL schema just like queries. Each subscription field describes a stream of a given type. Below, messageAdded streams a Message for a particular room.
The transport for subscriptions is typically WebSocket (graphql-ws protocol), so make sure your client connects over ws:// rather than plain HTTP POST.
type Subscription {
messageAdded(roomId: ID!): Message!
}
type Message {
id: ID!
roomId: ID!
text: String!
author: String!
}All lessons in this course
- Schema-First Design and Type Mapping
- Data Fetchers and Argument Binding
- Solving N+1 with Batch Loaders
- Subscriptions, Errors, and Schema Security