Coding Ref

GraphQL Mutations

What is GraphQL mutation?

In GraphQL, a mutation is a type of operation that allows you to create, update, or delete data on the server. It is similar to a CREATE, UPDATE, or DELETE operation in a traditional database, and is used to modify the data in a GraphQL server.

Mutations are defined in the schema using the mutation keyword, followed by the name of the mutation and the fields it accepts as arguments.

For example, the following schema defines a mutation called createProduct that accepts the name, price, and isAvailable fields as arguments:

type Mutation {
  createProduct(name: String!, price: Float!, isAvailable: Boolean!): Product
}

Once a mutation has been defined in the schema, you can use it in a GraphQL query to create, update, or delete data on the server. For example, the following query uses the createProduct mutation to create a new product with the given name, price, and isAvailable values:

mutation {
  createProduct(name: "Product A", price: 10.99, isAvailable: true) {
    id
  }
}

This query will create a new product with the given name, price, and isAvailable values and return the id of the new product.

When would you use mutation in GraphQL?

You would use a mutation in GraphQL when you need to create, update, or delete data on the server.

For example, you might use a mutation to add a new product to a product catalog, update the price of an existing product, or delete a product that is no longer available.

Mutations are typically used when you need to make a change to the data on the server. They are similar to CREATE, UPDATE, or DELETE operations in a traditional database, and are used to modify the data in a GraphQL server.

Here are some examples of when you might use a mutation in GraphQL:

  • Adding a new user to a user database
  • Updating the email address of an existing user
  • Deleting a user who is no longer active
  • Creating a new product in a product catalog
  • Updating the price of an existing product
  • Deleting a product that is no longer available

These are just a few examples of when you might use a mutation in GraphQL. In general, you would use a mutation any time - you need to make a change to the data on the server.

What should GraphQL mutations return?

After a mutation has been performed, the server should return a response indicating the status of the operation, along with any updated data.

GraphQL mutation schema

A GraphQL mutation schema specifies the types of mutations that can be performed on the server, along with the input arguments and return types for each mutation.

Here is an example of a simple mutation schema:

type Mutation {
  createUser(name: String!, email: String!): User
  updateUser(id: ID!, name: String, email: String): User
  deleteUser(id: ID!): Boolean
}

In this example, the Mutation type defines three mutations: createUser, updateUser, and deleteUser. Each mutation specifies its input arguments and return type. For example, the createUser mutation takes a name and an email as input, and it returns a User object.

Conclusion

GraphQL mutations are a powerful way to modify data on the server. They allow you to create, update, or delete