0
votes

I have a Neo4j database with many disconnected subgraphs (by design). Each subgraph has a "organization" node as it's root/start node. The intention is to have each organization only be able to query their own subgraph.

I have a webapp in front of the graph that provides the ID of the organization the user is part of. All nodes for the organization has a relationship with the organization node.

What is the best way to enforce query on a specific subgraph if you know the root/start node?

I have been playing with this example and it works but I would like to know if there is a better way of doing this:

MATCH (org:Organization {org_id: 1}) WITH org
MATCH p=(user:User)-[:SITTING_IN]->(room:Room)
WITH p, user, room
WHERE (user)-[:MEMBER_OF]->(org) AND (room)-[:MEMBER_OF]->(org)
RETURN p

This can quickly become cumbersome when the number of nodes grows with more complicated queries, and you have to remember to check them all in the WHERE clause..

1

1 Answers

0
votes

If all org graphs are mutually exclusive (no way to get from one to another through traversals), than you only need to check nodes that where NOT found through a traversal (in this case, 'user', but user can be found through a traversal of org. So if you start at org, and find everything as a traversal down from there, you can't enter any other graphs). Doing additional checks of what should then be always true just puts more work on Cypher for pointless validations.

Since each graph is mutually exclusive, traversal direction doesn't matter.