arijitAD commented :
https://dgraph.io/docs/query-language/#query-variables
From docs.
Value variables store scalar values. Value variables are a map from the UIDs of the enclosing block to the corresponding values. It therefore only makes sense to use the values from a value variable in a context that matches the same UIDs - if used in a block matching different UIDs the value variable is undefined.
The query is fixed by replacing func: uid(totalLikes) in query.
The following seems to work.
Data:
{
"set": [
{
"uid": "_:lesson1",
"dgraph.type": "Lesson",
"Lesson.reviews": [{ "uid": "_:review1" }, { "uid": "_:review2" }]
},
{
"uid": "_:review1",
"dgraph.type": "Review",
"Lesson.liked": true
},
{
"uid": "_:review2",
"dgraph.type": "Review",
"Lesson.liked": true
},
{
"uid": "_:lesson2",
"dgraph.type": "Lesson",
"Lesson.reviews": [{ "uid": "_:review3" }, { "uid": "_:review4" }]
},
{
"uid": "_:review3",
"dgraph.type": "Review",
"Lesson.liked": true
},
{
"uid": "_:review4",
"dgraph.type": "Review",
"Lesson.liked": false
},
{
"uid": "_:lesson3",
"dgraph.type": "Lesson",
"Lesson.reviews": [{ "uid": "_:review5" }]
},
{
"uid": "_:review5",
"dgraph.type": "Review",
"Lesson.liked": false
},
{
"uid": "_:lesson4",
"dgraph.type": "Lesson"
},
{
"uid": "_:lesson5",
"dgraph.type": "Lesson"
}
]
}
and I have used this query.
{
var(func: type(Lesson)) {
uid
Lesson.reviews @filter(eq(Lesson.liked, true)) {
uid
}
totalLikes as count(Lesson.reviews)
}
q(func: uid(totalLikes), orderdesc: val(totalLikes)) {
uid
totalLikes: val(totalLikes)
}
}