0
0
GraphQLquery~5 mins

Subscription resolvers in GraphQL

Choose your learning style9 modes available
Introduction

Subscription resolvers let your app listen for real-time updates from the server. They send new data automatically when something changes.

You want to show live chat messages as they arrive.
You need to update a dashboard with live sensor data.
You want to notify users instantly when new content is published.
You want to track live scores in a sports app.
You want to update stock prices in real-time.
Syntax
GraphQL
subscription SubscriptionName {
  eventName {
    field1
    field2
  }
}
Subscription resolvers use the subscription keyword instead of query or mutation.
They return a stream of data, not just one response.
Examples
This listens for new chat messages and returns their id, content, and sender.
GraphQL
subscription NewMessage {
  messageAdded {
    id
    content
    sender
  }
}
This listens for stock price changes and returns the symbol and new price.
GraphQL
subscription StockPriceUpdate {
  stockPriceChanged {
    symbol
    price
  }
}
Sample Program

This subscription listens for new notifications and returns their id, message, and creation time as they arrive.

GraphQL
subscription NewNotification {
  notificationReceived {
    id
    message
    createdAt
  }
}
OutputSuccess
Important Notes

Subscriptions require a WebSocket or similar connection to keep the data flowing.

They are great for live updates but can use more resources than queries.

Make sure your server supports subscriptions and you handle connection setup in your client.

Summary

Subscription resolvers let your app get live updates automatically.

Use them when you want real-time data like chat, notifications, or live scores.

They use the subscription keyword and keep sending data as events happen.