Mutations

Unlike queries, where the operation type is assumed (you can omit the word query), you need to specify mutation {} when writing a mutation.

Multiple mutations are generated per content model. We'll use the example of a product model with quantity integer and name string fields.

The generated mutations would be:

  • createProduct: Create a single product.
  • updateProduct: Update a single product.
  • deleteProduct: Delete a single product.
  • upsertProduct: Update or Create a single product.
  • publishProduct: Copy a single product to a content stage.
  • unpublishProduct: Remove a single (copy of) product from a content stage.
  • updateManyProducts: Update multiple products.
  • deleteManyProducts: Delete multiple products.
  • publishManyProducts: Copy multiple products to a content stage.
  • unpublishManyProducts: Remove multiple products (copies of) from a content stage.

Single Entry Mutations

createProduct

Single entry create mutation takes one input:

  • data: CreateInput! A required object type specifying the data you'd like to create.

Mutation

mutation {
createProduct(data: {
quantity: 100
}) {
id
}
}

Id's are generated for you.

updateProduct

Single entry update mutation takes two inputs:

  • where: WhereUniqueInput! A required object type specifying a field with a unique constraint (like id).
  • data: CreateInput! A required object type specifying the data you'd like to update.

Mutation

mutation {
updateProduct(where: {
id: "123xyz"
}, data: {
quantity: 50
}) {
id
}
}

deleteProduct

Single entry delete mutation takes one input:

  • where: WhereUniqueInput! A required object type specifying a field with a unique constraint (like id).

Mutation

mutation {
deleteProduct(where: {
id: "123xyz"
}) {
id
}
}

upsertProduct

Single upsert mutation takes two inputs:

  • where: WhereUniqueInput! A required object type specifying a field with a unique constraint (like id)
  • upsert: UpsertInput! A required object type specifying the behavior for both a create operation and an update operation. When the filter provided by where returns no entries, a new one will be created, if one is found, it will be updated. If you pass an id to the where filter, and an entry is not found, a system-generated id will be created, not the one provided.

Mutation

mutation {
upsertProduct(where: {
id: "123xyz"
}, upsert: {
create: {
quantity: 75
}
update: {
quantity: 75
}
}) {
id
}
}

publishProduct

Single publish mutation takes two inputs:

  • where: WhereUniqueInput! A required object type specifying a field with a unique constraint (like id).
  • to: [Stage!] A system Stage enumeration of the stage you want to publish to. Defaults to PUBLISHED.

Mutation

mutation {
publishProduct(
where: {
id: "123xyz"
}, to: PUBLISHED) {
id
}
}

Implicit Stage Mutation

mutation {
publishProduct(
where: {
id: "123xyz"
}) {
id
}
}

unpublishProduct

Single unpublish mutation takes two inputs:

  • where: WhereUniqueInput! A required object type specifying a field with a unique constraint (like id)
  • from: [Stage!] A system Stage enumeration of the stage you want to unpublish from.

Mutation

mutation {
unpublishProduct(
where: {
id: "123xyz"
}, from: PUBLISHED) {
id
}
}

Multiple Entry Mutations

If you don't pass a where filter to a many mutation - it will affect every entry of that type.

Many mutations are not yet stable and may experience moderate modifications before the product exits beta.

updateManyProducts

Multiple entry update mutation takes two inputs:

  • where: ManyWhereInput An object type specifying one or more fields.
  • data: CreateInput! A required object type specifying the data you'd like to update across all matching entries.

Mutation

mutation {
updateManyProducts(where: {
_search: 'ACME'
}, data: {
quantity: 100
}) {
count
}
}

Implicit Where Mutation

mutation {
updateManyProducts(
data: {
quantity: 100
}) {
count
}
}

deleteManyProducts

Multiple entry delete mutation takes one input:

  • where: ManyWhereInput An object type specifying one or more fields.

Mutation

mutation {
deleteManyProducts(where: {
_search: "Hammer"
}) {
count
}
}

Implicit Where Mutation

mutation {
deleteManyProducts {
count
}
}

publishManyProducts

Multiple publish mutation takes two inputs:

  • where: ManyWhereInput An object type specifying one or more fields.
  • to: [Stage!] A system Stage enumeration of the stage you want to publish to. Defaults to PUBLISHED.

Mutation

mutation {
publishManyProducts(
where: {
_search: "ACME"
}, to: PUBLISHED) {
count
}
}

Implicit Stage Mutation

mutation {
publishManyProducts(
where: {
_search: "ACME"
}) {
count
}
}

Implicit Where and Stage Mutation

mutation {
publishManyProducts {
count
}
}

unpublishProduct

Multiple unpublish mutation takes two inputs:

  • where: WhereInput An object type specifying one or more fields.
  • from: [Stage!]! A system Stage enumeration of the stage you want to unpublish from.

Mutation

mutation {
unpublishManyProducts(
where: {
_search: "ACME"
}, to: DRAFT) {
count
}
}

Implicit Stage Mutation

mutation {
unpublishManyProducts(
where: {
_search: "ACME"
}) {
count
}
}

Implicit Where and Stage Mutation

mutation {
unpublishManyProducts {
count
}
}

This site uses cookies to provide you with a better user experience. For more information, refer to our Privacy Policy