Get relationships of a specific profile

26 Views Asked by At

I'm attempting to get all friend requests of a specific profile. The cypher query that I have is the following:

.run(
  'MATCH (p:Profile {uuid: $profileUuid})' +
    ' OPTIONAL MATCH (p)-[friendRequests:FRIEND_REQUEST]->(u)' +
    ' OPTIONAL MATCH (e)<-[reverseFriendRequest:FRIEND_REQUEST]-(l)' +
    ' return friendRequests',
  {
    profileUuid,
  })

Where profileUuid is the uuid of the profile that im attempting to retrieve all FRIEND_REQUEST relationships that are ongoing or incoming from/to it. But the above query is returning all FRIEND_REQUEST relationships for all nodes. Do I have to add a where somewhere? If yes isnt that what the first MATCH is supposed to be doing?

1

There are 1 best solutions below

0
Graphileon On BEST ANSWER

In the second OPTIONAL match, you should replace (e) by (p). Since the (l) and (e) are unbound, that's why you get everything.

An alternative would be something like this:

MATCH (p:Profile {uuid: $profileUuid})
RETURN [(p)-[fr:FRIEND_REQUEST]-() | fr] AS allFriendRequests

or

MATCH (p:Profile {uuid: $profileUuid})
RETURN [(p)-[fr:FRIEND_REQUEST]->() | fr] AS outgoingFriendRequests,
       [(p)<-[fr:FRIEND_REQUEST]-() | fr] AS incomingFriendRequests