Search code examples
graph-databasesgremlinamazon-neptune

Discover all vertices of a type from origin


I'm modeling a permission system with graph-database. This is an example scenario:

graph image for example

An user u1 has a direct permission and also is in a group g1 that has permissions and extends another group.

An user also manages a system s1 and can give the permissions related to s1 to another users.

Basically what I need to do is get all permissions for my u1. I suspected it is a simple query but I'm having a hard time with gremlin.

I have tried combinations of repeat()s, out()s, emit()s, path()s, etc. but couldn't find what I need. All I have tried returns either more nodes than needed (like the group nodes that are in the path of the permission, or the permissions related to the system) or fewer nodes (like not reaching G2)

So to sumarize what I need is:

gremlin> g.V('u1').__misteriousQueryThatShouldntBeThatHard()
==> v[p1]
==> v[p2]
==> v[p3]
==> v[p4]

Appreciate any help.


Solution

  • If the only issue with Kfir's answer from the previous question is to limit the path to certain edge labels you can try:

    g.V('u1')
        .repeat(out('is-in', 'extends', 'has-permission').simplePath())
        .until(hasLabel('Permission'))