apoc.refactor.invert
Syntax |
|
||
Description |
Inverts the direction of the given |
||
Input arguments |
Name |
Type |
Description |
|
|
The relationship to reverse. |
|
|
|
|
|
Return arguments |
Name |
Type |
Description |
|
|
The internal id of the original entity. |
|
|
|
The copied entity. |
|
|
|
Any error that occurred during the copy process. |
Usage Examples
The examples in this section are based on the following sample graph:
CREATE (mark:Person {name: "Mark", city: "London"})
CREATE (jennifer:Person {name: "Jennifer", city: "St Louis"})
CREATE (mark)-[:FOLLOWS]->(jennifer);
The following inverts the FOLLOWS
relationship, making Jennifer the start node using APOC and Cypher:
MATCH ()-[rel:FOLLOWS]->()
CALL apoc.refactor.invert(rel, { failOnErrors = true })
YIELD input, output
RETURN input, output;
MATCH (startNode)-[rel:FOLLOWS]->(endNode)
CALL (*) {
WITH id(rel) AS oldId, properties(rel) AS relProps, type(rel) AS relType
DELETE rel
MERGE (startNode)<-[newRel:$(relType)]-(endNode)
SET newRel = relProps
RETURN oldId AS oldId, newRel AS newRel
}
RETURN oldId, newRel;
input | output |
---|---|
14 |
[:FOLLOWS] |
We can list all the Person
nodes by running the following query:
MATCH path = ()-[rel:FOLLOWS]->()
RETURN path;
path |
---|
(:Person {name: "Jennifer", city: "St Louis"})-[:FOLLOWS]→(:Person {name: "Mark", city: "London"}) |
APOC is not inverting the type; instead, it adds a new relationship with the desired direction and deletes the original. |