Imagine a schema for a golf course. I have a course which contains multiple holes, holes contain tees, and tees contain pins (in this case pins are called baskets). A hole can have multiple tee and pin locations so the distance from tee to pin is stored on a facet. Here’s an example query:
{
q(func: eq(CourseName, "Cool Golf Course")) {
CourseName
CourseLocation
Hole @filter(eq(HoleNumber, "1")) {
HoleNumber
Tee @filter(eq(TeeColor, "Red")) {
TeeColor
Basket @filter(eq(BasketDesignation, "A")) @facets(distance) {
BasketDesignation
}
}
}
}
}
Which returns the following:
"data": {
"q": [
{
"CourseName": "Cool Golf Course",
"Hole": [
{
"HoleNumber": "1",
"Tee": [
{
"TeeColor": "Red",
"Basket": [
{
"BasketDesignation": "A",
"Basket|distance": 68
}
]
}
]
}
]
}
]
}
What I want to do is a query like this which will give me the total course distance by adding up all of the distance facet values in a query:
{
q(func: eq(CourseName, "Cool Golf Course")) {
CourseName
CourseLocation
Hole {
HoleNumber
Tee @filter(eq(TeeColor, "Red")) {
TeeColor
Basket @filter(eq(BasketDesignation, "A")) @facets(d as distance) {
BasketDesignation
}
}
}
Distance: sum(val(d))
}
}
…but I cannot aggregate the values higher than one block in the query as it fails with a “Check the levels” error.
This query works:
{
var(func: eq(CourseName, "Cool Golf Course")) {
CourseName
CourseLocation
Hole {
HoleNumber
Tee @filter(eq(TeeColor, "Red")) {
TeeColor
Basket @filter(eq(BasketDesignation, "A")) @facets(d as distance) {
BasketDesignation
}
}
}
}
q() {
sum(val(d))
}
}
…and I could go on and pass every little bit of information from the var block to the query results but that would be tedious. What I want is the value from sum(val(d))
where d is the distance of every basket returned by the query at the “root” of the course node.
How do I aggregate a set of values several levels removed from the block the value is returned in?