0

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?

  • What I attempted to do just now is edit the 3rd 'OPTIONAL MATCH' specifically '(e)' to '(p)'. This resulted in the query only returning a single 'friendRequests' record with no fields in it which is correct since that profile does not have any incoming or outgoing friend requests. But this confuses me as im only returning the friendRequests variable in the second 'OPTIONAL MATCH'. So the third match isnt supposed to affect the result? – Frisbetarian-Support Palestine Aug 21 '22 at 09:48

1 Answers1

1

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
Graphileon
  • 5,275
  • 3
  • 17
  • 31