GraphQL mutations create and modify objects, similar to a PUT, POST, or DELETE request in REST. Mutation requests are sent to the same endpoint as query requests.
Mutations have the following structure:
- The name of a mutation, such as
- The data to use in the mutation passed as an argument, such as the information for a new customer
- A selection of return fields that should be included in the response, such as the ID of the successfully created Customer object
Mutations require input data, such as the data to create a new object, or the ID of an object to delete. For mutations that might require a substantial data object, the schema provides a dedicated input object type.
For example, the customerCreate mutation requires an
input argument, which accepts a CustomerInput object. The CustomerInput type defines all the fields that can be used to create or modify a customer.
Each mutation provides a set of fields that can be returned in the response. For example, one of the return fields available for the customerCreate mutation is the Customer object that was created by a successful mutation. As with a GraphQL query, you can select the fields on the new object that you want to include in the response.
Each mutation also returns the
userErrors field. The
userErrors field returns information about errors when a mutation fails. You should include the
userErrors field with each mutation to make it easier to troubleshoot failed mutations.
Create a customer
The following mutation uses input objects and return fields to create a new customer and return their ID and displayname.
- GraphQL variables - Learn how to simplify and re-use graphql requests through variables.