Skip to main content

GraphQL mutations

Note

The REST Admin API is a legacy API as of October 1, 2024. All apps and integrations should be built with the GraphQL Admin API. For details and migration steps, visit our migration guide.

Legacy

The REST Admin API is a legacy API as of October 1, 2024. All apps and integrations should be built with the GraphQL Admin API. For details and migration steps, visit our migration guide.

GraphQL mutations create and modify objects, similar to a PUT, POST, or DELETE request in REST. However, unlike REST, GraphQL mutations are sent to a single endpoint and use the POST HTTP method.

For a full list of available mutations, refer to the relevant API's reference.


GraphQL mutations have the following structure:

  • The mutation operation name. This is the keyword that starts a mutation operation in a GraphQL request.
  • The mutation field name, such as customerCreate. This is the specific mutation to perform, as defined in the GraphQL schema.
  • The input data to use in the mutation, such as the information for a new customer. This is the data that the mutation needs to perform its operation. It's passed as an argument to the mutation field.
  • A selection of return fields that should be included in the response, such as the ID of a successfully created Customer object. These are the pieces of data that you want the mutation to return. In GraphQL, you specify exactly what data you want returned.

Mutation structure

mutation {
mutationName(arg: "Data") {
return fields
}
}

Mutation structure with data

mutation {
customerCreate(input: { firstName: "Ayumu", lastName: "Hirano", email: "ayumu@example.com" }) {
customer {
id
}
}
}

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 input object. The CustomerInput type defines all the fields that can be used to create or modify a customer.

Mutation input

mutation {
customerCreate(
input: {
firstName: "Ayumu",
lastName: "Hirano",
email: "ayumu@example.com"
}
)
{
...
}
}

Each mutation provides a set of fields that can be returned in the response. For example, one of the return fields that's available for the customerCreate mutation is the Customer object that was created by a successful mutation. Like GraphQL queries, you can select the fields on the new object that you want to include in the response.

Each mutation can also return 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. Learn more about error handling in GraphQL.

Mutation return fields

mutation {
customerCreate(
input: {
...
}
)
{
customer {
id
displayName
}
userErrors {
field
message
}
}
}

Anchor to Example: Create a customerExample: Create a customer

The following mutation uses input objects and return fields to create a new customer and return their id and displayName:

POST /admin/api/{api_version}/graphql.json

GraphQL mutation

mutation {
customerCreate(
input: {
firstName: "Ayumu",
lastName: "Hirano",
email: "ayumu@example.com"
}
)
{
customer {
id
displayName
}
userErrors {
field
message
}
}
}

JSON response

{
"data": {
"customerCreate": {
"customer": {
"id": "gid:\/\/shopify\/Customer\/1310036885526",
"displayName": "Ayumu Hirano"
},
"userErrors": []
}
}
...
}

Learn how to write reusable requests using variables.


Was this page helpful?