Subscriptions in GraphQL allow clients to receive real-time updates from the server. This is particularly useful in scenarios where data changes frequently, such as chat applications or live notifications. Subscriptions are defined in the schema similar to queries and mutations, but they are executed differently. Instead of sending a single request and receiving a response, clients establish a long-lived connection with the server to receive updates as they occur.
For example, a subscription to a chat application could be defined as follows:
subscription OnMessageAdded {
messageAdded {
id
content
author
timestamp
}
}