Neo4j streams cypher template with dynamic label

Is there a way to dynamically fetch node/relationship label from events in cypher template? Preferably not using apoc.

Something like:

WITH event.label as nodeLabel
MERGE (n:nodeLable {id: event.id})
...

The above query creates nodes with "nodeLabel" and not the actual label value in the event.

@ajalali

see apoc.merge.node - APOC Extended Documentation

Hey @dana_canzano
Thanks. It works for nodes, but not for relationships. Since apoc.merge.relationship needs both source and destination nodes as input, so I'm looking for a way to match on source and destination nodes. I want something to be able to match on source/dest nodes like:

with event.sourceLabel as sourceLabel, event.destLabel as destLabel, event
MATCH (n:sourceLabel {id: event.sourceNodeId})
MATCH (m:destLabel {id: event.destNodeId})
CALL apoc.merge.relationship (
     n,
     event.label,
     event.props,
     m
)

which doesn't work

Using @dana_canzano’s suggestion, you can do the following:


WITH event.sourceLabel as sourceLabel, event.destLabel as destLabel, event

CALL apoc.merge.node([sourceLabel], {id: event.sourceNodeId}, {}, {}) yield node
WITH node as n, destLabel, event

CALL apoc.merge.node([destLabel], {id: event.destNodeId}, {}, {}) yield node
WITH node as m, n, event

CALL apoc.merge.relationship (
     n,
     event.label,
     event.props,
     m
)

1 Like