Advanced Mutations
1. Bulk Mutations
Bulk mutations allow us to add multiple books at once. Use the following mutation:
mutation {
bulkAddBooks(books: [
{ title: "Brave New World", author: "Aldous Huxley" },
{ title: "The Catcher in the Rye", author: "J.D. Salinger" }
]) {
id
title
author
}
}
2. Nested Mutations
Nested mutations allow us to perform multiple related operations in a single mutation. Use the following mutation to add a book with an author:
mutation {
nestedAddBookWithAuthor(book: { title: "The Hobbit", author: "J.R.R. Tolkien" }, author: "J.R.R. Tolkien") {
id
title
author
}
}
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.