I have a graph which has a hierarchy of categories in it (similar to product categories on a shopping site, e.g., Clothing --> Mens --> Shirts --> Short Sleeve --> ...). I have a few use cases where I need to retrieve the entire hierarchy as a tree (nested ruby and/or javascript objects in this case). The only solution I've been able to come up with is to use NODES()
to retrieve each unique path and then transform it into nested objects in the client. The performance of this is obviously a problem.
MATCH (store:Store),
(store)-[:hasCategory]->(category:Category),
categories=(category)-[:narrower*0..]->(:Category)
WHERE store.name = {name}
RETURN store, NODES(categories) AS categories
This returns result rows that are basically paths like:
[store, [category1, narrower_category1, narrower_category2, ...]]
What's the proper way to handle this in cypher without numerous trips back to the server or a massive fetch of data like the above query?