Implementing a Mutation

Mutations are defined in the schema just as queries, but with the concept of the Mutation type. In the above example, we have three mutations: These are addBook, updateBook and deleteBook operations and all of them work with books table.

Add a Book:

To add a new book, use the following mutation:

mutation {
addBook(title: "The Great Gatsby", author: "F. Scott Fitzgerald") {
id
title
author
}
}

Output

This mutation invokes the addBook resolver function, which generates a new book id, creates a new book object with the new id and the provided fields, inserts the new book to the end of the books array and returns the new book.

Update a Book:

Here is a mutation that will help to update an existing book:

mutation {
updateBook(id: 1, title: "Nineteen Eighty-Four") {
id
title
author
}
}

Output:

This mutation executes the updateBook resolver function, which accepts the id, and compares the new title and/or author fields to the book object found by its id, then returns the updated book.

Delete a book:

Use the following mutation to delete a book:

mutation {
deleteBook(id: 2) {
id
title
author
}
}

Output:

This mutation invokes the deleteBook resolver function, which takes the book based on the id, removes that particular book from the books array, and returns A product containing the deleted book.

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 like POST, PUT, PATCH, and DELETE in REST APIs. In this article, We will learn about the Mutations in GraphQL in detail.

Similar Reads

Mutations in GraphQL

Mutations in GraphQL are embedded in the schema and are used to write data to the server. Every mutation can have an argument to take in the desired data to be modified and every mutation can also have a return value of data. Before moving on, let’s take a step-by-step approach to how to set the mutation with Express. js and Express-GraphQL....

Prerequisites

Node. js and npm: GraphQL runs on node.js, so you need to have Node.js and npm which is Node Package Manager installed on your computer. You can collect them from the Node js official website. 2. Basic Knowledge of JavaScript: It is important to have a basic understanding of JavaScript and its operations because all your GraphQL server as well as the queries using the APIs will be written in JavaScript. 3. Basic Understanding of GraphQL: It will be helpful to have prior knowledge of GraphQL concepts such as schema, queries, and resolvers....

Step-by-Step Example

1. Set Up the Project...

Implementing a Mutation

Mutations are defined in the schema just as queries, but with the concept of the Mutation type. In the above example, we have three mutations: These are addBook, updateBook and deleteBook operations and all of them work with books table....

Working with Variables in Mutations

GraphQL supports the use of variables in mutations, which makes them more flexible and reusable. Here’s how we can use variables with the addBook mutation....

Advanced Mutations

1. Bulk Mutations...

Conclusion

Overall, mutations in GraphQL are important and serve the purpose of changing server data. They enable clients to carry out the CRUD operations in the shortest time possible. In this article, we have learned about how to perform various data manipulation responsibilities through defining mutations and installing a simple Express server. This example shows how to eventually work with basic mutations to create a good start for creating more complex GraphQL applications. Using variables in mutations allows for better flexibility and code reuse by making your API calls all the more effective....