PASSWORD RESET

Your destination for complete Tech news

What are mutations in GraphQL?

280 0
< 1 min read

In GraphQL, a mutation is a type of operation that allows clients to make changes to the data on the server. Mutations are typically used to create, update, or delete data in a database. They are similar to the “write” operations in a traditional REST API. Unlike queries, which only retrieve data, mutations change the state of the data on the server. They are defined in the schema and can take arguments, like fields or variables, to specify the data to be changed.

Here is an example of a mutation in GraphQL that creates a new user in a database:

mutation {
  createUser(
    name: "John Smith",
    email: "[email protected]"
  ) {
    id
    name
    email
  }
}

This mutation defines a createUser operation that takes two arguments, name and email, and returns the id, name, and email of the created user. In this example, the mutation is making a change to the data on the server by creating a new user in the database.

Another example is updating a user:

mutation {
  updateUser(
    id: "1",
    name: "Jane Smith",
    email: "[email protected]"
  ) {
    id
    name
    email
  }
}

Here, the updateUser operation takes an id argument to identify the user to be updated and two other arguments, name and email, as the new value of the user.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.