Is it possible to dynamically construct a WITH statement? I have to do a graph traversal, but I don't know in advance which collections contain the graph nodes. I have tried to create the statement dynamically, but I keep getting strange errors that don't seem to have anything to do with that. I collect all the collection names from the document handles in the query and I create a comma separated list of collection names that I dynamically inject into the query.
1 Answer
For cluster deployments, it is necessary to declare the possibly involved collections upfront when you use graph traversals. More specifically:
- You need to declare the collection of the start vertex if you provide it as a string literal (
FOR v,e,p 1..1 OUTBOUND "vertex/start" ...) and the collection isn't explicitly declared otherwise. - You need to declare all collections of vertices that edges may point to when using anonymous graphs / collection sets (
... OUTBOUND "vertex/start edgeColl1, edgeColl2as opposed to... OUTBOUND "vertex/start" GRAPH "aNamedGraph") unless they are explicitly declared elsewhere in the query. - You may declare additional collections that you want to dynamically access with the 
DOCUMENT()function. This isn't a requirement but could help to avoid deadlocks. 
Calculating the collections for WITH at runtime isn't possible as that is too late for the query engine. It needs to prepare a cluster query with this knowledge for deterministic locking, which is necessary to avoid deadlocks, and there might also be some setup for traversals that requires knowledge about the involved collections.