Skip to main content

SitecoreAI · Tutorial

Querying Sitecore XMC Content with GraphQL

Use the GraphQL Playground to see exactly what a template returns, from a single item to a full navigation tree.

GavAusWeb2 min read

Goal: stop guessing at field shapes and query Sitecore XMC content directly. The fastest way to learn what a template actually returns is the built-in GraphQL Playground, running locally at https://xmcloudcm.localhost/sitecore/api/graph/edge/ide.

Authenticate the PlaygroundLink to this section

You need an API key. Find one in the content tree at /sitecore/system/Settings/Services/API Keys/..., or grab the value already in your app's .env.local (SITECORE_API_KEY / NEXT_PUBLIC_SITECORE_API_KEY). Paste it into the Playground's HTTP Headers panel as sc_apikey:

HTTP headersjson
{ "sc_apikey": "YOUR_API_KEY" }

Query children or a single itemLink to this section

To find multiple child items under a path, query children › results. To find a single item, drop children and results and query the fields directly on item.

graphql
# Multiple items (children)
query {
  item(path: "/sitecore/content/your-site-name/Home/some/path", language: "en") {
    children {
      results {
        name
        id
        data: field(name: "Data") {
          value: jsonValue
        }
      }
    }
  }
}

# Single item
query {
  item(path: "/sitecore/content/your-site-name/Home/some/path", language: "en") {
    name
    id
    data: field(name: "Data") {
      value: jsonValue
    }
  }
}

The Docs panel on the right of the Playground is worth exploring on day one — it shows every available query, type, and argument so you're not reverse-engineering the schema by trial and error.

Query a navigation treeLink to this section

For real component work you’ll often need more than a flat item — navigation is the classic case. This fragment queries _BasePage fields recursively across nested children so you can build a full nav tree in one round trip:

navigation.graphqlgraphql
fragment navigationFields on Item {
  ... on _BasePage {
    metaTitle { value }
    navigationTitle { value }
    navigationFilter { jsonValue }
  }
  url { path }
}

query ($site: String!, $language: String!) {
  layout(site: $site, routePath: "/", language: $language) {
    item {
      ...navigationFields
      children(hasLayout: true) {
        results {
          ...navigationFields
          children(hasLayout: true) {
            results {
              ...navigationFields
            }
          }
        }
      }
    }
  }
}
Query variablesjson
{
  "site": "your-site-name",
  "language": "en"
}

Inherited fieldsLink to this section

If a template's fields aren't inheriting the way you expect, check its Standard Values' Inheritance tab in the Content Editor. That's how you find that, say, a Redirect or Bounce Page template also inherits from a shared _Base template — which tells you to add an inline fragment like ... on Redirect { redirect { jsonValue } } to pull that data through GraphQL too.