What are GraphQL subscriptions?
With the help of subscriptions, the users can get real-time updates on the data changes in the server. This is useful in case of features that require real-time data handling like trading sites, or live chats, push notifications, etc. They are akin to a pub-sub model where the users subscribe to a particular event in the server model, and receive updates when there is a change in the specified model.
Example –
In the below example, we will create a newMessage subscription that will subsribe to the new messages that enter in the chat, and whenever a message is entered, the subscription returns the user the id, content, sender, and timestamp of the message.
subscription {
newMessage {
id
content
sender
timestamp
}
}
Handling Data Updates in GraphQL
GraphQL is an open source query language for the APIs that allows us to query and fetch only the data we require on the client side, and also shows us the detailed schema of all the fields supported in the API. It helps saving a lot network bandwidth with just sending and receiving the fields that we want, and with its strongly typed nature, we can easily identify all the queries and mutations supported by the server. In this article, we will learn about handling data updates in GraphQL.