i want create a new edge between two nodes using the python driver for neo4j.
def cypher_orchestration(tx, start_identifier: str, end_identifier:str, edge_type: str):
start_node = node_match(tx, start_identifier).single()
end_node = node_match(tx, end_identifier).single()
edge = create_edge(tx, start_node, edge_type, end_node)
I created the function "node_match" and can successfully receive the searched nodes. But now i have problems with creating a edge between both nodes.
What i did, is creating the "create_edge" function which looks like this:
from neo4j.graph import Node
def create_edge(tx, start_node: Node, edge_type: str, end_node: Node):
result = tx.run(f"""
CALL apoc.merge.relationship($start_node, $edge_type, {{}}, $end_node, {{}}) yield rel
return rel
""", start_node=start_node, edge_type=edge_type, end_node=end_node)
return result
Unfortunately i don't understand, how to pass a node which was queried before as parameter to a query. How can i pass a Node Object to neo4j? Is this even possible?
I also know, that i could match both nodes again in my "create edge" function but i believe (and hope) that there must be a more... more elegant way. If someone knows how to archieve this or knows a similar way, i would be happy to learn from you :)
Thanks for your help! Greetings, ottonormal
You cannot pass node or relationship objects as query parameters.
In the case of apoc.merge.relationship
, the start and end node are defined within the Cypher query itself. It looks like:
MATCH (start:SomeLabel {some: "property"})
MATCH (end:SomeOtherLabel {some-other: "property"})
CALL apoc.merge.relationship(start, "SOME_REL_TYPE", {}, {}, end, {}) YIELD rel
RETURN rel