Mutations

Your project endpoint exposes GraphQL mutations you can use to modify the contents of your project. The mutations API allows you to interact with content ouside of the Hygraph UI using GraphQL.

It's not recommended you enable Public API Permissions for mutations, but instead use a Permanent Auth Token for mutating data.

Auto-generated mutationsAnchor

When a new model is added to your project, so are custom GraphQL mutations.

For example, if you created a Product model, these mutations would also be generated inside your GraphQL schema:

  • createProduct
  • updateProduct
  • deleteProduct
  • upsertProduct
  • publishProduct
  • unpublishProduct
  • updateManyProductsConnection
  • deleteManyProductsConnection
  • publishManyProductsConnection
  • unpublishManyProductsConnection

All of these mutations accept input types that are specific to your projects GraphQL schema.

Create entriesAnchor

When creating new content entries, the data argument will have an associated input type that is specific to your content model.

For example, if your project contains the model Product, you will have:

MutationArgumentInput Type
createProductdataProductCreateInput!

The id is a default system field that is automatically generated for all new entries.

Update entriesAnchor

When updating single content entry, you must specify the unique where criteria of which you want to update, as well as the new data.

For example, if your project contains the model Product, you will have:

ArgumentInput Type
whereProductWhereUniqueInput!
dataProductUpdateInput!

You can also update any unique field on your model.

Upsert entriesAnchor

The upsert mutation allows you to create, or update a content entry based on whether the unique where values exist.

For example, if your project contains the model Product, you will have:

ArgumentInput Type
whereProductWhereUniqueInput!
upsertProductUpsertInput!
upsert > createProductCreateInput!
upsert > updateProductUpdateInput!

You must provide both create, and update to the upsert argument.

Delete entriesAnchor

Similar to updating, and upserting entries, you can specify using where the entries you want to delete.

For example, if your project contains the model Product, you will have:

ArgumentInput Type
whereProductWhereUniqueInput!

Nested mutationsAnchor

  • create: Create and relate entries
  • connect: Connect additional existing entries by unique field
  • update: Update the connected entries
  • upsert: Create or update connected entries
  • disconnect: Disconnect connected relations by unique field
  • delete: Delete all connected entries
  • set: Override all connected entries

CreateAnchor

UpdateAnchor

Insert at positionAnchor

When inserting related entries, you can connect entries at a given position. The position of entries reflects that fetching relations.

The position input accepts the following values:

FieldTypeDefinition
beforeIDThe ID of the entry you want to insert before
afterIDThe ID of the entry you want to insert after
startBooleanSet to true if you want to insert at the start
endBooleanSet to true if you want to insert at the end

You must only provide one of these values.

BeforeAnchor

mutation {
updateAuthor(
where: { id: "..." }
data: { posts: { connect: { position: { before: "..." } } } }
) {
id
}
}

AfterAnchor

mutation {
updateAuthor(
where: { id: "..." }
data: { posts: { connect: { position: { after: "..." } } } }
) {
id
}
}

StartAnchor

mutation {
updateAuthor(
where: { id: "..." }
data: { posts: { connect: { position: { start: true } } } }
) {
id
}
}

EndAnchor

mutation {
updateAuthor(
where: { id: "..." }
data: { posts: { connect: { position: { end: true } } } }
) {
id
}
}

Publishing content mutationsAnchor

Hygraph automatically generates publish, and unpublish mutations for each of your content models, including the asset model.

Learn more about publishing and unpublishing content.

Batch mutationsAnchor

Hygraph supports batch mutations that can be applied to "many" entries at once. You may wish to update, or delete many entries at once that fit given criteria.

Batch mutations comply with the Relay connection type specification.

Update manyAnchor

To update many entries at once, you must use the updateMany[Model]Connection mutation. You can use where, or pagination filters to set the criteria you wish to update.

ArgumentInput TypeDescription
whereProductManyWhereInputFiltering criteria for entries you want to update.
dataCreateInput!An object that specifies the data you'd like to update matching entries with.
firstIntSeek forwards from end of result set.
lastIntSeek backwards from start of result set.
skipIntSkip result set by given amount.
beforeIDSeek backwards before specific ID.
afterIDSeeks forwards after specific ID.

If you do not pass any filters, then the first 10 entries will be updated. See Pagination for updating pages.

For example, let's update all products where featured: true, to be featured: false.

Delete manyAnchor

To delete many entries at once, you must use the deleteMany[Model]Connection mutation. You can use where, or pagination filters to set the criteria you wish to delete.

ArgumentInput TypeDescription
whereProductManyWhereInputFiltering criteria for entries you want to delete.
firstIntSeek forwards from end of result set.
lastIntSeek backwards from start of result set.
skipIntSkip result set by given amount.
beforeIDSeek backwards before specific ID.
afterIDSeeks forwards after specific ID.

If you do not pass any filters, then the first 10 entries will be deleted. See Pagination for updating pages.

Publish manyAnchor

Just like you can publish content, you can also batch publish.

ArgumentInput TypeDescription
whereProductManyWhereInputFiltering criteria finding entries.
fromStage = DRAFTThe content stage to find entries from.
to[Stage!]! = [PUBLISHED]The target published content stage.
firstIntSeek forwards from end of result set.
lastIntSeek backwards from start of result set.
skipIntSkip result set by given amount.
beforeIDSeek backwards before specific ID.
afterIDSeeks forwards after specific ID.

For example, we could publish the first 5 products to the PUBLISHED stage.

Unpublish manyAnchor

Just like you can batch publish, you can also batch unpublish.

ArgumentInput TypeDescription
whereProductManyWhereInputFiltering criteria for entries you want to find.
stageStage = DRAFTThe content stage to find entries in.
to[Stage!]! = [PUBLISHED]The target published content stage.
firstIntSeek forwards from end of result set.
lastIntSeek backwards from start of result set.
skipIntSkip result set by given amount.
beforeIDSeek backwards before specific ID.
afterIDSeeks forwards after specific ID.

Localized content mutationsAnchor

Depending on whether or not you have localized fields in your schema, you will be able to mutate each of the localized content entries.

Learn more about mutating localized content.