I need to build a tree structure
eg.
Root
Parent 1
- Child 1.1
- Child 1.2
- Child 1.2.1
- Child 1.2.1.1
Parent 2
- Child 2.1
- Child 2.2
To get the siblings you can use get parent, then their parent children using the following query:
query {
item(path: "/sitecore/content/Demo/Demo/Home/Test", language: "en") {
...siblings
name
}
}
fragment siblings on Item {
parent {
children {
results {
name
}
}
}
}
Currently, it's NOT possible to retrieve all descendants due to GraphQL's limitation on recursive references within fragments, as discussed here: https://github.com/graphql/graphql-spec/issues/929. (Because fragments within fragments could fix this - children within children)
As a workaround, and to ensure good performance, it's better to retrieve a fixed depth (e.g., 1-2-3 levels) of children. This approach helps you avoid exceeding GraphQL's complexity limitations and allows for better control over the performance in your code. This solution is more efficient than how Axes.GetDescendants() worked in the past, which was not optimal.
Example of 3 level of children (I have not included the pagination, so this would return the default 10 items, you can introduce pagination to this, to do this check: https://doc.sitecore.com/xmc/en/developers/xm-cloud/query-examples-for-the-delivery-api.html#get-items-filtered-by-multiple-paths):
query {
item(
path: "/sitecore/content/Demo/Demo/Home"
language: "en"
) {
...descendants
}
}
fragment descendants on Item {
name
firstLevel: children {
results {
name
secondLevel: children {
results {
name
thirdLevel: children {
results {
name
}
}
}
}
}
}
}