Implementing GraphQL mutations
Most services need to modify data as well as query it. GraphQL calls these mutations. A mutation has three related components:
- The mutation itself, which defines the change that will be made to the graph. It should be named using a verb, a noun, and use camel casing, for example,
addProduct
. - The input is the input for a mutation, and it should have the same name as the mutation with a suffix of
Input
, for example,AddProductInput
. Although there is only one input, it is an object graph, so it can be as complex as you need. - The payload is the returned document for a mutation, and it should have the same name as the mutation with a suffix of
Payload
, for example,AddProductPayload
. Although there is only one payload, it is an object graph, so it can be as complex as you need.
Adding mutations to the GraphQL service
Let’s define mutations for adding, and later, we will define some to update and delete products...