Friday, October 17, 2025

Part 1: Common GraphQL Queries for Sitecore XM Cloud: A Developer’s Guide — Basic Queries to Level Up Your Skills

 Introduction

As Sitecore shifts toward headless architecture with XM Cloud, combining it with GraphQL unlocks a world of possibilities for building fast, flexible experiences. GraphQL’s ability to request exactly the data you need is an ideal match for modern frameworks like React, Next.js, and other front-end technologies.

In this blog, we’ll explore some of the most essential GraphQL queries that Sitecore developers use daily. We’ll start simple, focusing on fetching single items, and move to more advanced patterns for handling lists, navigation, and multilingual content. These queries have been production-tested, ensuring that they’re ready to be used in your own projects.

1. Get Item by Path Query

This is one of the simplest queries in Sitecore GraphQL. It’s useful when you know the exact content tree path of an item and want to retrieve its data.

query GetItemByPath($path: String!, $language: String!) {
item(path: $path, language: $language) {
id
name
displayName
path
template {
id
name
}
fields {
name
value
}
}
}

When to use:

  • Building navigation menus
  • Fetching page content by route
  • Loading specific content items

Example Variables:

{
"path": "/sitecore/content/MyProject/Home",
"language": "en"
}

2. Get Item by ID Query

If you have an item GUID, using an ID-based query is more efficient than a path-based query. Since GUIDs are unique across the entire Sitecore instance, this method ensures you’re targeting the exact item.

query GetItemById($id: String!, $language: String!) {
item(id: $id, language: $language) {
id
name
displayName
path
fields {
name
value
jsonValue
}
}
}

When to use:

  • Retrieving datasource items for components
  • Following field references

Example Variables:

{
"id": "{11111111-1111-1111-1111-111111111111}",
"language": "en"
}

3. Basic Field Values Query

Accessing individual fields is essential in Sitecore. Here’s a basic example of how to fetch various field types.

query GetItemFields($itemPath: String!, $language: String!) {
item(path: $itemPath, language: $language) {
id
name
fields {
name
value
definition {type}
}
title: field(name: "Title") {
value
definition {type}
}
description: field(name: "Description") {
value
definition {type}
}
}
}

When to use:

  • Retrieving fields like “Title” and “Description”
  • Displaying text fields

Example Variables:

{
"itemPath": "/sitecore/content/MyProject/Home",
"language": "en"
}

4. Structured Field Data Query

This query is useful for fields with more complex structures, such as images, links, or rich text content.

query GetStructuredFields($itemPath: String!, $language: String!) {
item(path: $itemPath, language: $language) {
heroImage: field(name: "HeroImage") {
value
jsonValue
}
ctaLink: field(name: "CTALink") {
value
jsonValue
}
bodyContent: field(name: "BodyContent") {
value
}
}
}

When to use:

  • Building components that need image dimensions and alt text
  • Creating buttons/links with proper href, text, and target attributes
  • Displaying rich text content with HTML formatting

Example Variables:

{
"itemPath": "/sitecore/content/MyProject/Home",
"language": "en"
}

5. Child Items Navigation Query

This query fetches all child items of a specified parent and is commonly used for building navigation menus, content listings, or displaying child pages under a parent.

query ContextItemChildren($contextItem: String!, $language: String!) {
contextItem: item(path: $contextItem, language: $language) {
children{
results{
id
PageTitle: field(name: "PageTitle") {
value: jsonValue
}
}
}
}
}

When to use:

  • Creating breadcrumb navigation
  • Generating sitemaps or content hierarchies
  • Building folder-based content listings

Example Variables:

{
"contextItem": "/sitecore/content/MyProject/Home",
"language": "en"
}

I hope you enjoy this Sitecore blog. Stay tuned for more Sitecore related articles.

Till that happy Sitecoring :)

Please leave your comments or share this article if it’s useful for you.

No comments:

Post a Comment