Open In App

Handling Data Updates in GraphQL

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

What are GraphQL mutations?

Mutations in GraphQL allow us to update the data in the API server, and with the help of it, we can create, update, or remove the data in the backend database. They are defined in the GraphQL schema, and the users can refer the schema to call the mutation with the required fields to update the data by calling the API in the server. Let’s look at a mutation example.

Example –

In the below example query, the createUser mutation is used to create a new user on the server when the query is hit. It accepts the input data, “name” and “email” and uses the data passed in the arguments to create a new entry for the user the server database.

mutation {
createUser(input: { name: "John Doe", email: "john@example.com") {
id
name
email
}
}

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
}
}

Types of GraphQL Mutations

There are different types of mutations available in GraphQL –

  • Create Mutations: It is used to create a new resource onto the server
  • Delete Mutations: It is used to delete a resource from the database
  • Update Mutations: It is used to update or modify a resource on the server
  • Custom Mutations: We can also define some custom mutations to perform some custom operation

Update Mutations

We can use Update Mutations in GraphQL update or modify a resource on the server. The mutation accepts an input object in its argument, and the object is then used to update the existing data on the server. Let’s look at an example to understand it in more detail

Example of Update Mutations

In this example, we will see a sample GraphQL mutation that updates a user information. The mutation takes an id, and the input object as its arguments, and then update the existing user object in the database with the passed information.

mutation {
updateUser(id: "user_id", input: { name: "GFG" }) {
id
name
}
}

In the above example, the updateUser mutation takes in “id”, and “input” object, and pass in “name” in the object to specify the new value of “name” field. We then request id, and name from the mutation’s response to verify whether the fields have been updated

Conclusion

In this article, we learnt about the GraphQL mutations and subscriptions, and how we can use these two operations to handle data updates in the GraphQL. GraphQL mutations are used when we want to update (add, remove, patch) the data in the server, and subscriptions are used when we want to listen to the real-time data updates in a specific data model in the GraphQL server.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads