To make this easier, I’ll begin with this mutation example. So imagine the following set up:
We have a bunch of parents which may or may not have a collection of items, each of which have an amount.
{
"set": [
{
"uid": "_:first",
"isParent": true,
"name": "first",
"items": [
{ "amount": 10 },
{ "amount": 2 }
]
},
{
"uid": "_:second",
"isParent": true,
"name": "second",
"items": [
{ "amount": 5 },
{ "amount": 3 }
]
},
{
"uid": "_:third",
"name": "third",
"isParent": true
}
]
}
Notice the third node has no “items” attached to it.
Now with this query:
{
parents(func: has(isParent)) {
uid
name
items {
itemAmount as amount
}
total: sum(val(itemAmount))
}
}
I get the expected results:
{
"data": {
"parents": [
{
"uid": "0xc5b9",
"name": "third"
},
{
"uid": "0xc5ba",
"name": "first",
"items": [
{
"amount": 10,
"uid": "0xc5bb"
},
{
"amount": 2,
"uid": "0xc5bc"
}
],
"total": 12
},
{
"uid": "0xc5bd",
"name": "second",
"items": [
{
"amount": 5,
"uid": "0xc5be"
},
{
"amount": 3,
"uid": "0xc5bf"
}
],
"total": 8
}
]
}
The problem arises if some parents are added to the database, but they do not have any “items” attached to them. So if on a fresh database we only add the following:
{
"set": [
{
"uid": "_:first",
"isParent": true,
"name": "first"
}
]
}
Then the above query causes this error:
"Wrong use of var() with [{itemAmount 2}]."
So my question is this: how can I still use that query regardless of whether or not any nodes have the “items” edge, and if not simply not display a value for “total” (as is the case for the items-less node “third” in the very first example)?
The thing is that if a single node is added with an “items” edge, then the query works fine, but I can’t guarantee there will always be a node with that edge.