Count cannot be assigned to a value variable

I have not tested the code, by my understanding is that all vars are actually maps, and val(c) is actually a map lookup using a particular key: specifically, the uid of the entity defined in the enclosing scope.

This is documented, if not prominently, at https://dgraph.io/docs/query-language/value-variables/ where it reads " Value variables are a map from the UIDs of the enclosing block to the corresponding values." This is why val(c) could also be used in a repeating structure if the query matched many Courses - c is not one value, but a map with one value per uid.

So because val(c) is inside a block defining a new Chapter, I expect the uid of the new node is used to access the c var (the map) and it gets undefined because that map only has an entry for 0x01 (the uid of the matching Course vs the new Chapter).

A possible workaround is here: DQL conditional upsert with nested query value variables

It appears that near the end there is an upsert using RDF where specific uids from the query portion are used. In your case, there is a new node, so you may need to see if blank nodes can work here (as it won’t be possible to get the uid of a new chapter that does not exist yet in the query portion).

Again, I have not replicated the scenario, so I may be incorrect.