14. Mutations
1m

Mutation: A GraphQL operation that modifies data.

To write a mutation, we start with the keyword mutation, then what we want to name the mutation. Inside the curly braces, we add the entry point and the fields for the mutation.

mutation AddSpacecat($name: String!) {
addSpacecat(name: $name) {
code
success
message
spacecat {
id
name
}
}
}

The useMutation hook

Options: The GraphQL query as the first argument, and an options object as the second argument.

Result: A tuple with a mutate function in the first position and an object representing the mutation result in the second position.

Code-along: Build the LikeTrack mutation in Sandbox

mutation LikeTrack($trackId: ID!) {
likeTrack(trackId: $trackId) {
success
track {
id
numberOfLikes
}
}
}

In the Variables panel:

{"trackId": "c_0"}
Previous
Next