Sitecore GraphQL Mutations: A Complete Guide

 

Introduction

While queries handle data retrieval in GraphQL, mutations are responsible for creating, updating, and deleting content in Sitecore. Let's explore how to effectively use Mutations in Sitecore.

1. Create Mutation:

mutation CreateItem {

  createItem(

    name: "New Item to be Created"

    template: "{4330C04F-5CAA-4E93-A2B2-634900395E51}"

    parent: "{C081F940-3F38-4E10-8F43-B80AA63BC1BA}"

    language: "en"

    fields: [

      { name: "title", value: "'New Item Created with Mutations'" }

      {

        name: "subtitle"

        value: "'This is Sitecore New Item Created with Mutations'"

      }

    ]

  ) {

    path

    id

  }

}

In this example:

  • createItem is a mutation function that creates a new item.
  • name is the name of the new item.
  • parentId is the ID of the parent item under which this new item will be created (you can get the parentId of an existing item in the Sitecore content tree).
  • templateId is the template ID for the new item, which defines the fields and layout of the item.

2. Update item

mutation UpdateItem {

  updateItem(

    path: "/sitecore/content/Home/MyProject/MutationsItem/Cretaed MutationsItem"

    language: "en"

    version: 1

    fields: [

 { name: "title", value: "'Item updated'" }

 { name: "subtitle", value: "'This is Sitecore Item updated with Mutations UpdateItem'" }

 ]

  ) { 

 id 

 name 

 fields { 

 name 

 value 

  } 

  }

  }


In this mutation:

  • path is the path of the item you want to update. we can pass itemid as well instead of path.
  • fields is an array of field names and their new values.
  • The response includes the updated item’s id, name, and the updated field values.

13. Delete item

mutation DeleteItem {

  deleteItem(

    path: "/sitecore/content/Home/MyProject/MutationsItem/Item updated"

  ) 

{ success }

}

In this mutation:

  • path is the path of the item you want to delete, we can pass itemid as well instead of path
  • The response includes a success flag to indicate whether the deletion was successful.

Comments

Popular posts from this blog

Setting Up Sitecore Headless with Next.js in Disconnected Mode

Translating Sitecore items’ field values into different language with PowerShell and Translate API

Create Sitecore Ribbon Button using Sitecore PowerShell Extensions