apoc.refactor.invert

Details

Syntax

apoc.refactor.invert(rel, config) :: (input, output, error)

Description

Inverts the direction of the given RELATIONSHIP.

Input arguments

Name

Type

Description

rel

RELATIONSHIP

The relationship to reverse.

config

MAP

{ failOnErrors = false :: BOOLEAN }. Note that while { failOnErrors = true } is recommended to ensure transactional consistency and prevent a partial application of changes, it is not the default since this would risk breaking existing implementations. Introduced in APOC 2025.01

Return arguments

Name

Type

Description

input

INTEGER

The internal id of the original entity.

output

RELATIONSHIP

The copied entity.

error

STRING

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:

apoc.refactor.invert
MATCH ()-[rel:FOLLOWS]->()
CALL apoc.refactor.invert(rel, { failOnErrors = true })
YIELD input, output
RETURN input, output;
Using Cypher
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;
Results
input output

14

[:FOLLOWS]

We can list all the Person nodes by running the following query:

MATCH path = ()-[rel:FOLLOWS]->()
RETURN path;
Results
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.