I have a tree structure like a folder structure so with a project with nested project without a depth limit, each node has access rights on them.
Here is my graph:
Here is my query:
MATCH (a:Account {name: "bob"})-[r:VIEWER | EDITOR]->(c:Project)
MATCH (c)<-[:IS_PARENT*]-(p)
WHERE (p)<-[:VIEWER | EDITOR]-(a)
WITH TYPE(r) as relation, p, collect(distinct c) AS children
RETURN {name: p.name, Children: [c in children | {name: c.name, access:relation}]}
Here is my result:
{
"name": "project test",
"Children": [
{
"access": "VIEWER",
"name": "cohort"
},
{
"access": "VIEWER",
"name": "experience"
}
]
}
{
"name": "project test",
"Children": [
{
"access": "EDITOR",
"name": "protocol"
},
{
"access": "EDITOR",
"name": "nested child"
}
]
}
{
"name": "cohort",
"Children": [
{
"access": "EDITOR",
"name": "nested child"
}
]
}
And this is what I want to get:
{
name: "Project 1",
access: "VIEWER",
children: [
{
name: "cohort",
access: "VIEWER",
children: [
{
name: "nested",
access: "EDITOR",
},
]
},
{
name: "protocol",
access: "EDITOR",
},
{
name: "expererience",
access: "VIEWER",
}
]
}
My problem is that the result is split in two results, and nested child
isn't nested in cohort
.
An other thing that is tricky is that I don't want to get a node if I don't have a relation with it.
For example here I removed the relation between bob
and cohort
:
So I must not get cohort
in my result, like this:
{
name: "Project 1",
access: "VIEWER",
children: [
{
name: "nested child",
access: "EDITOR",
},
{
name: "protocol",
access: "EDITOR",
},
{
name: "expererience",
access: "VIEWER",
}
]
}
Here is my data if you want to try:
MERGE (project:Project:RootProject {name: "project test"})
MERGE (child1:Project {name: "cohort"})
MERGE (child2:Project {name: "protocol"})
MERGE (child3:Project {name: "experience"})
MERGE (child4:Project {name: "nested child"})
MERGE (project)-[:IS_PARENT]->(child1)
MERGE (project)-[:IS_PARENT]->(child2)
MERGE (project)-[:IS_PARENT]->(child3)
MERGE (child1)-[:IS_PARENT]->(child4)
MERGE (bob:Account {name: "bob"})
MERGE (bob)-[:EDITOR]->(child4)
MERGE (bob)-[:EDITOR]->(child2)
MERGE (bob)-[:VIEWER]->(child3)
MERGE (bob)-[:VIEWER]->(child1)
MERGE (bob)-[:VIEWER]->(project)
I have tried a lot of things but I never get a good result.