# Lesson 7.2 - Write mutations

Create, update, publish, unpublish, and delete a product entry programmatically using the Hygraph Content API 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.

  **Mutation**

```graphql
mutation MyMutation {
  createProduct(
    data: {
      productName: "My latest product"
      productSlug: "my-latest-product"
      productPrice: 59.99
      productVariant: {
        create: {
          productType: { create: { Clothing: { size: M, color: Green } } }
        }
      }
      relatedProducts: {
        create: {
          title: "Related Products"
          products: { connect: { productSlug: "plaid-shirt" } }
        }
      }
      productCategories: { connect: { slug: "clothes" } }
      productImage: { create: { handle: "", fileName: "" } }
    }
  ) {
    productName
    productPrice
  }
}
```

  
  **Response**

```json
{
  "data": {
    "createProduct": {
      "productName": "My latest product",
      "productPrice": 59.99
    }
  }
}
```


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 editor](https://hygraph.com/images/docs/getting-started/createproduct-mutations-example.png)

## `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.

  **Mutation**

```graphql
mutation MyMutation {
  updateProduct(
    data: { productName: "Updated Product" }
    where: { productSlug: "my-latest-product" }
  ) {
    productSlug
    productName
  }
}
```

  
  **Response**

```json
{
  "data": {
    "updateProduct": {
      "productSlug": "my-latest-product",
      "productName": "Updated Product"
    }
  }
}
```


![updateProduct mutation result](https://hygraph.com/images/docs/getting-started/updateproduct-mutations-example1.png)

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

  **Mutation**

```graphql
mutation MyMutation {
  updateProduct(
    data: { productName: "My black leather shoes" }
    where: { productSlug: "black-leather-shoes" }
  ) {
    productSlug
    productName
  }
}
```

  
  **Response**

```json
{
  "data": {
    "updateProduct": {
      "productSlug": "black-leather-shoes",
      "productName": "My black leather shoes"
    }
  }
}
```


![updateProduct mutation second example](https://hygraph.com/images/docs/getting-started/updateproduct-mutations-example2.png)

## `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 publishing](https://hygraph.com/images/docs/getting-started/publishProduct-mutations-draftmode.png)

  **Mutation**

```graphql
mutation MyMutation {
  publishProduct(where: { productSlug: "my-latest-product" }, to: PUBLISHED) {
    stage
    productName
  }
}
```


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

![Entry in PUBLISHED stage after publishing](https://hygraph.com/images/docs/getting-started/publishProduct-mutations-publishedmode.png)

## `unpublishProduct`

**Optional:**
This step is optional. Skip to `deleteProduct` to continue the lifecycle.

This mutation returns the entry to `DRAFT` only.

  **Mutation**

```graphql
mutation MyMutation {
  unpublishProduct(
    where: { productSlug: "my-latest-product" }
    from: PUBLISHED
  ) {
    stage
    productName
  }
}
```

  
  **Response**

```json
{
  "data": {
    "unpublishProduct": {
      "stage": "DRAFT",
      "productName": "Updated Product"
    }
  }
}
```


![Entry returned to DRAFT after unpublishing](https://hygraph.com/images/docs/getting-started/publishProduct-mutations-draftmode2.png)

## `deleteProduct`

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

  **Mutation**

```graphql
mutation DeleteMutation {
  deleteProduct(where: { productSlug: "my-latest-product" }) {
    productName
  }
}
```

  
  **Response**

```json
{
  "data": {
    "deleteProduct": {
      "productName": "Updated Product"
    }
  }
}
```


## Verify the deletion

Run this query to confirm the entry no longer exists.

  **Query**

```graphql
query CheckDeletion {
  products {
    productName
  }
}
```

  
  **Response**

```json
{
  "data": {
    "products": [
      { "productName": "My black leather shoes" },
      { "productName": "Blue running shoes" },
      { "productName": "Plaid shirt" },
      { "productName": "Necklace" },
      { "productName": "Headband" }
    ]
  }
}
```


**Response varies by content:**
Your response reflects only the product entries you created. If you completed the additional practice in lesson 6.1, more entries will appear. The Black leather shoes entry may show the updated name "My black leather shoes" from the updateProduct mutation above.

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](https://hygraph.com/docs/getting-started/tutorial/tutorial-frontend-overview)

Or, go to the [Tutorial overview](https://hygraph.com/docs/getting-started/tutorial/tutorial-overview) for the full lesson list.
