UPDATE
I have played around a bit now and I’m actually really confused now. The documentation states:
The upsert block allows performing queries and mutations in a single request. The upsert block contains one query block and one or more than one mutation blocks. Variables defined in the query block can be used in the mutation blocks using the
uidandvalfunction.
However, the documentation also says that the value variable is only defined in the context of the same UID:
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.
This led me to alter the schema to test this out.
type Course {
id: ID!
title: String!
chapters: [Chapter!]!
chapterCount: Int
}
type Chapter {
id: ID!
title: String!
sequence: Int
}
and the new upsert block
{
"query": "{ qTest(func: uid(0x1)) { u as uid c as count(Course.chapters) } }",
"mutations": [
{
"set": {
"uid": "uid(u)",
"Course.chapterCount": "val(c)",
"Course.chapters": [
{
"dgraph.type": "Chapter",
"Chapter.title": "Test Chapter",
"Chapter.sequence": "val(c)"
}
]
}
}
]
}
This results in a Course.chapterCount: 2, which is exactly the result I was expecting for Chapter.sequence! So does this mean I cannot use val for nodes with a different UID?
This would be a disaster and basically makes the upsert mutation useless.