cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Return relationships-with-properties as Query results

npatel
Node Clone

Hi,

I have a graph network with relationship having properties. So i have modeled them as separate relationship type. Normally cypher would be (u1:User)-[:HAS_ACCESS_TO {role: "readOnly"]-(e1)
and there are separate nodes that define what permissions each role has
(readOnly:Role {permissions: ['READ_A', 'READ_B']}

Now when I query to get user and their roles & permissions together, I had to design schema
something like this

type User {
    id: ID!
    name: String
    has_access_to: [HasAccessTo]
  }

  type HasAccessTo @relation(name: "HAS_ACCESS_TO") {
      from: User
      to: Entity
      role: String
      permissions: [String]
        @cypher(
          statement: "MATCH (u:UserRole) WHERE u.name = this.role RETURN u.permissions"
        )
  }

This approach works when I query something like this

{
 User(id: "u1") {
  has_access_to {
    role
    permissions
    Entity {
      id
    }
  }
 } 
}

But If I have a custom query where I want to return relationships

type  Query {  
    getUserEntities(id: String!, vendorId: Int!): [HasAccessTo]
      @cypher(
        statement: "Match (u:User {id: $id})-[rel:HAS_ACCESS_TO]-(e)-[*0..2]-(v:Vendor {id: $entityId }) return rel"
      )

and query

{
getUserEntities(id: "u1" vendorId: 21989) {
    role 
   permissions
 }
}

I get error

 "message": "Variable `hasAccessTo_relation` not defined (line 1, column 385 (offset: 384))\r\n\"WITH apoc.cypher.runFirstColumn(\"Match (u:User {id: $id})-[rel:HAS_ACCESS_TO]-(e)-[*0..2]-(v:Vendor {id: $vendorId }) return rel\", {offset:$offset, first:$first, id:$id, vendorId:$vendorId}, True) AS x UNWIND x AS `hasAccessTo` RETURN `hasAccessTo` { .role ,permissions: apoc.cypher.runFirstColumn(\"MATCH (u:UserRole) WHERE u.name = this.role RETURN u.permissions\", {this: hasAccessTo_relation}, false)} AS `hasAccessTo` SKIP $offset\"\r\n                                                                                                                                                                                                                                                                                                                                                                                                 ^",

This might not be the best way so any different suggestions are welcome

Thanks.

0 REPLIES 0