Hi All,
In the previous blog posts, we covered the fundamentals. Now, let’s delve a bit deeper into the topic.
6. Multi-Level Content Hierarchy Query
When you need to fetch both parent and child items, this nested query is the solution. It’s useful for creating full navigation menus that show both main items and sub-pages.
query ContextItemChildren($contextItem: String!, $language: String!) {
contextItem: item(path: $contextItem, language: $language) {
children {
results {
id
PageTitle: field(name: "PageTitle") {
value: jsonValue
}
children {
results {
id
PageTitle: field(name: "PageTitle") {
value: jsonValue
}
}
}
}
}
}
}
When to use:
- Building multi-level navigation menus with dropdowns
- Generating breadcrumb trails with multiple levels
- Building category/subcategory product listings
Example Variables:
{
"contextItem": "/sitecore/content/MyProject/Home",
"language": "en"
}
7. Template-Specific Content Query
In this query, we’re using fragments to access fields specific to templates. This ensures type safety and allows you to build reusable components for different content types.
query GetTypedContent($itemPath: String!, $language: String!) {
item(path: $itemPath, language: $language) {
id
name
template {
id
name
}
... on Page {
pageTitle: field(name: "PageTitle") {
value
}
metaDescription: field(name: "MetaDescription") {
value
}
}
}
}
When to use:
- Building reusable queries for different templates
- Creating generic content components
- Displaying different fields per content type
Example Variables:
{
"itemPath": "/sitecore/content/MyProject/Home/About",
"language": "en"
}
8. Multi-Template Content Handler Query
This advanced query combines multiple template types in one request using GraphQL fragments. It’s useful when you’re building flexible components that need to handle different content types (like BlogPost, ProductPage, etc.).
query GetContentByType($itemPath: String!, $language: String!) {
item(path: $itemPath, language: $language) {
id
name
template {
id
name
}
... on BlogPost {
title: field(name: "Title") {
value
}
publishDate: field(name: "PublishDate") {
value
}
author: field(name: "Author") {
value
}
}
... on ProductPage {
productName: field(name: "ProductName") {
value
}
price: field(name: "Price") {
value
}
sku: field(name: "SKU") {
value
}
}
... on ContactPage {
address: field(name: "Address") {
value
}
phone: field(name: "Phone") {
value
}
email: field(name: "Email") {
value
}
}
}
}
When to use:
- Building universal content components
- Implementing content listings with mixed template types
- Building search results that adapt to content types
Example Variables:
{
"itemPath": "/sitecore/content/MyProject/Blog/MyBlogPost",
"language": "en"
}
9. Component Datasource Query
When components are using separate content items for data (like a text block or image), this query helps retrieve component-specific data by item ID.
query GetComponentDatasource($datasourceId: String!, $language: String!) {
datasource: item(id: $datasourceId, language: $language) {
id
name
template {
id
name
}
... on TextBlock {
heading: field(name: "Heading") {
value
}
content: field(name: "Content") {
value
}
}
... on ImageContent {
image: field(name: "Image") {
value
jsonValue
}
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