I have a hierarchical parent/child structure that I wish to return as a tree. I can use apoc.convert.toTree to return the tree and that works great. Since my graph's parent/child structure is somewhat like a file folder structure, to simplify, I'll just use folders as an example. The following example returns a tree with the folders and sub-folders nested under one parent folder.
MATCH (parent:Folder)
WHERE id(parent) = {parentId}
OPTIONAL MATCH childPath= (child:Folder)-[:CHILD*]->(endFolder:Folder)
WHERE (parent)-[:CHILD]->(child)
WITH childPath, endFolder
ORDER BY endFolder.name
WITH collect(childPath) as paths
CALL apoc.convert.toTree(paths) YIELD value
RETURN value
The part that is stumping me, is returning the 'creator' of each folder to return into the tree because each folder in the path has the relationship (:Folder)-[:CREATED_BY]->(:Person). Since the path could be unlimited length, I find all the sub folders with the [:CHILD*] relationship, which allows me to run a MATCH on the start and end of the path but this does not allow me to run a MATCH on ALL the nodes. Then I still need to turn the data back into a path to return the nested tree.
I feel like I want to collapse the Folder/Person relationship down to a virtual nodes, recreate the folder relationships as virtual relationships and then generate the hierarchical paths, but once I create a virtual node/relationships, it doesn't seem like I can do a MATCH off of it to generate a path. Is there a different approach that anyone else has taken to 'branch out' another relationship of my path?