Querying Graphs with Gremlin

Tags: , , ,



Please help me with the query on Gremlin lang

I have a graph with 2 types of vertices: User and Group. I need to find friends of ‘U1’. If users have edges ( member or invite ) to ‘Group A’ need to flag them like the below result.

[Graph image](https://ibb.co/D9VPKw4)

Expected result : [ { U2: ‘Member’}, { U3: ‘Invited’ }, { U4: ‘Member’} ]

Answer

g.V().has('User', 'name', 'U1')
  .out('friend')
  .as('friends')
  .bothE('invited', 'member', 'friend')
  .where(or(inV().has('Group', 'name', 'G1'), outV().has('User', 'name', 'U1')))
  .group()
  .by(select('friends').values('name'))
  .by(label().fold())


Source: stackoverflow