5

The Sitecore Headless GraphQL documentation has some good examples for querying data. However I don't see any for mutations such as updating an item's fields, creating new items, or deleting items.

As an example, say I have an item of template Listing which has a field called description. How do I update the field? I tried this but I think the syntax is wrong.

mutation UpdateListing($path: String, $description String) {
    item(path: $path) {
        id
        ...on Listing {
            description {
                value: $description
            }
        }
    }
}

2 Answers 2

5

The systemContent schema provider built into Sitecore GraphQL does indeed provide mutations to createItem, updateItem, and deleteItem. The Sitecore-provided /sitecore/api/graph/items/master endpoint exposes these, and details can be found in the Schema Docs in the GraphQL IDE accessible at /sitecore/api/graph/items/master/ui.

Note that for these to work successfully, the context user executing them must have appropriate authorizations. If you are using the systemService security configuration on the endpoint (the default for the example), you may need additional configuration for it to authenticate using cookies. If using publicService or otherwise authenticating with an API Key, the Impersonation User configured on the API Key will need appropriate rights.

Note as well that when setting item field values, the mutations accept a value of type FieldValueInput, which expects a JSON value for the field value. The docs on this graph type provide examples of accepted values:

Field type Value
Checkbox "true", "false"
Integer "5"
Number "5.5"
Single-Line Text "'Home'"
Multi-Line Text, Rich Text "'line1\r\nline2'"
Date and Datetime "'2019-08-04T13:33:03.969Z'"
Checklist ["0DD426E8-F61B-447E-8484-A1FF33115963","B3576ED6-05C9-49E7-B12B-918D5B2CF430"]
General Link "{'text':'some test', 'anchor': 'anchor', 'linktype':'internal','queryString':'sc_lang=en','className': 'linkClass', 'targetItem':'110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9', 'target':'target', 'url': 'some url'}"

Note the nested double/single quotes on text values in particular, to make them valid JSON.

Example Queries

Create Item

mutation CreateItem {
  createItem(
    name: "ExampleItem"
    template: "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
    parent: "{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}"
    language: "en"
    fields: [
      { name: "title", value: "'Example Item'" }
      { name: "text", value: "'This is an example item created with GraphQL'" }
    ]
  ) {
    path
  }
}

Update Item

mutation UpdateItem {
  updateItem(
    path: "/sitecore/content/ExampleItem"
    language: "en"
    version: 1
    fields: [{ name: "title", value: "'Example Item with changed title'" }]
  ) {
    ... on SampleItem {
      title {
        value
      }
    }
  }
}

Delete Item

mutation DeleteItem {
  deleteItem(
    path: "/sitecore/content/ExampleItem"
  )
}
-3

GraphQL is a querying "language". You cannot update or create sitecore items using it. What are you trying to do? Creating items in Sitecore from a JSS Front-end seems like a bad idea. I am assuming this is JSS front-end. What is your use case here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.