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
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
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
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
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
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
Operation Name in GraphQL In GraphQL, the operation name is a label that we attach to requests sent to servers which acts as an indication of code complexity. This allows us to define queries, mutations, and subscriptions to improve code readability and organize in particular when it comes to large projects making it easier
6 min read
GraphQL Validation GraphQL validation is an important step in ensuring the integrity and correctness of GraphQL queries and mutations. It involves checking the syntax, structure, and semantics of GraphQL documents to identify any errors or inconsistencies before executing them against a GraphQL server. In this article
4 min read
JQuery Integration in GraphQL jQuery integration in GraphQL refers to the practice of using jQuery, a popular JavaScript library that interacts with GraphQL servers. This integration simplifies the process of sending and receiving data asynchronously, which is crucial for building dynamic web applications. In this article, We wi
5 min read