Kerpol
Kerpol

Reputation: 57

What;s a correct way to use APOC.when in that query?

I need to select different nodes in dependence of rel_type var. So it'll be ideal for me if returning a node from APOC.when is possible. As alternative way it's ok for returning an ID of matched node. How can I solve that task by one of these ways?

Legal_Entity and Natural_Person is a classes of nodes interested us;

hid_party - a parameter that each nodes has. Used as unique ID;

rel_type may be 'LEGAL' or 'PHYSICAL'. Depending on this parameter, different nodes should be selected.

Example:

match (legal:Legal_Entity {hid_party : '157456674'})
with legal,
     '422741957' as second_hid,
     'LEGAL' as rel_type

     CALL apoc.when(
        'LEGAL' = 'LEGAL',
        'match (second:Legal_Entity {hid_party : second_hid}) return second as second_node',
        'match (second:Natural_Person {hid_party : second_hid}) return second as second_node',
        {second_hid:second_hid}
        ) YIELD value

return value.second_node

Upvotes: 0

Views: 247

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

Your query should work, but maybe we're missing how you pass the rel type as parameter.

Simple example :

Stub graph

CREATE (:LegalEntity {id: 123})
CREATE (:NaturalPerson {id: 456})

Then set a dummy parameter in the browser

:param relType => 'LEGAL'

Verify the list of parameters available for a query

:params

// result
{
  "relType": "LEGAL"
}

Then example of using apoc.when depending on the parameter

CALL apoc.when($relType = 'LEGAL', 
'MATCH (n:LegalEntity) RETURN n',
'MATCH (n:NaturalPerson) RETURN n',
{}
)
YIELD value
RETURN value.n AS n

Returns the expected LegalEntity node

╒══════════╕
│"n"       │
╞══════════╡
│{"id":123}│
└──────────┘

Change the parameter to something else

:param relType => 'OtherValue'

Run the same query, result is different

╒══════════╕
│"n"       │
╞══════════╡
│{"id":456}│
└──────────┘

Upvotes: 1

Related Questions