Hygraph
Docs

#Lesson 7.2 - Write mutations

In this lesson, you will use mutations to create, update, publish, unpublish, and delete a product entry. The mutations follow a complete content lifecycle: create it, change it, publish it, unpublish it, remove it. After each destructive mutation, you will run a quick query to confirm the change took effect.

Running a verification query after a mutation is not just a tutorial step. It is the correct practice for any destructive or state-changing API operation.

Hygraph automatically generates five mutations for each model when it is created. For the Product model, those are createProduct, updateProduct, publishProduct, unpublishProduct, and deleteProduct. They are used here in lifecycle order.

#createProduct

This mutation creates a new product entry with a name, slug, price, product variant, related product, category assignment, and a placeholder image asset.

The mutation uses nested create blocks to configure the product variant and related products inline. The products connect uses plaid-shirt, one of the five core products from lesson 6.1. The productCategories field uses connect to link to an existing category entry rather than creating a new one.

Open the Content editor and navigate to the Product model. The new entry appears there in the DRAFT stage.

createProduct mutation result in the content editorcreateProduct mutation result in the content editor

#updateProduct

This mutation changes the productName of the entry created above. The where clause uses productSlug as the unique identifier because it was configured to be unique in lesson 1.2.

updateProduct mutation resultupdateProduct mutation result

The updateProduct mutation works on any entry, not only newly created ones. To demonstrate, update the Black leather shoes entry:

updateProduct mutation second exampleupdateProduct mutation second example

#publishProduct

The entry created above is in DRAFT. This mutation promotes it to PUBLISHED so it is visible when querying the PUBLISHED stage.

Entry in DRAFT stage before publishingEntry in DRAFT stage before publishing

Open the content editor to confirm the entry is now published.

Entry in PUBLISHED stage after publishingEntry in PUBLISHED stage after publishing

#unpublishProduct

This mutation returns the entry to DRAFT only.

Entry returned to DRAFT after unpublishingEntry returned to DRAFT after unpublishing

#deleteProduct

This mutation permanently deletes the entry. Deletion cannot be undone.

#Verify the deletion

Run this query to confirm the entry no longer exists.

The deleted entry does not appear. This verify-after-mutate pattern applies to any mutation in any project. Run a query immediately after a destructive operation to confirm the result.

The project now has a queryable API, published content, and a confirmed remote source connection. The next step is connecting a frontend to display this content in a storefront.

#What's next

Lesson 8 - Frontend

Or, go to the Tutorial overview for the full lesson list.