Handling Data Updates in GraphQL
Last Updated :
05 Mar, 2024
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: "[email protected]") {
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.
Similar Reads
Error Handling in Mutations in GraphQL
GraphQL is a powerful open-source query language for APIs. It is known for its flexibility and efficiency in fetching data from server endpoints. GraphQL mutations allow us to update the data in the GraphQL server. Oftentimes, the data update might fail because of several reasons, network failure, r
4 min read
Querying Data with GraphQL
GraphQL is an open-source technology that allows us to query only the data that we require, unlike the traditional REST architecture which returns us the entire resources and data with specific endpoints configured for the same. Using GraphQL, we specify using the query the data that we want, and it
7 min read
Python SQLite - Update Data
In this article, we will discuss how we can update data in tables in the SQLite database using Python - sqlite3 module. The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using UPDATE statement as per
7 min read
Input Types in GraphQL Schema
GraphQL is an open-source Query Language for APIs that allows us to query only the data we require, unlike the REST framework which returns the entire data as is configured in its API. With GraphQL, we can maintain a Schema that defines what data we can query from the server, and we can define a pro
5 min read
Mutations in GraphQL
GraphQL is a query language for REST APIs created by Facebook which is designed to provide more efficiency, flexibility, and superiority in data handling. While GraphQL queries are used to retrieve information, mutations are used to perform data operations on the server, similar to HTTP Commands lik
6 min read
Variables in GraphQL
GraphQL is a query language for API (Application Programming Interface). It was developed by Facebook in 2012 after they released it publicly in 2015. Graphql Provides a more efficient, powerful, and flexible alternative to traditional REST APIs. In this article, we will learn about variables in Gra
5 min read
Inline Fragments in GraphQL
GraphQL's flexibility in querying diverse data structures is one of its key strengths. Inline fragments are a powerful feature that allows developers to conditionally query fields based on the type of an object. In this article, We will explore the concept of inline fragments in GraphQL, how they wo
5 min read
Types in GraphQL
GraphQL is a strongly typed query language used as a manipulative language for various APIs. It is also called a query language for APIs. GraphQL also helps to describe our data. GraphQL services can run in any language In this, Types are the fundamental concepts that define various data types prese
4 min read
Type Language in GraphQL
Type language in GraphQL is the fundamental component that distinguishes GraphQL from other query languages. It plays an important role in defining the structure, shape, and behavior of the data that can be queried from a GraphQL API. In this article, we will learn about the concept of the type lang
5 min read
FastAPI - Using GraphQL
FastAPI and GraphQL have become prominent players in the realm of API development, each bringing its unique set of advantages to the table. GraphQL, initially created by Facebook, distinguishes itself with its efficiency and flexibility in data retrieval. In contrast to traditional REST APIs, GraphQ
5 min read