Returning relationship attributes depending on type

Hi all,

I got a basic problem for which I couldn't find a solution in previous posts. I want to return relationships from a known start node. However, the attributes that must be returned for each relationship depend on the relationship type.This is because different attributes exist for different relationship types.

For example, when a relationship x is matched, x.a must be returned, only when relationship y is matched, I want to return y.b.

Ofcourse, I can do this in different match queries like:

MATCH (n:nodeType{id: 123})-[x:RELATIONSHIP_X]-()
MATCH (n:nodeType{id: 123})-[y:RELATIONSHIP_Y]-()

Only this seems rather naive and will result in the execution of different queries. Therefore, I was looking to do this in one query and came up with the following:

MATCH (n:nodeType {id: 123})
WITH n, x.a as x_a
WITH x_a, y.b as y_b
RETURN x_a, y_b

This result works, only if both the relationships exist for this node. If not, nothing will be returned at all. Also, this will result in different db.hits which is computational inefficient (right?).

Therefore, I'm still looking for a way to do this in one query which has one db hit. My questions are:

  • Is this possible in Cypher? Perhaps there is a APOC procedure for this?
  • Another solution I see is to just return all edges from a node and format the response afterwards. For example this can be done in a driver. Is this the way to go?
  • Or is this more of a data modelling problem? In that case I should remodel my data such that by returning all edges only the attributes of interest are returned?

This should be possible by combining a wildcard or list of allowed relationship types ([r:TYPE_A|TYPE_B]) with apoc.when to conditionally return the properties you want, based on type(r)

1 Like