Redirect relationships

The APOC library contains procedures that can be used to redirect relationships to target nodes.

Procedures for redirecting relationships

Qualified Name Type

apoc.refactor.from(rel RELATIONSHIP, newNode NODE) - redirects the given RELATIONSHIP to the given start NODE.

Procedure

apoc.refactor.to(rel RELATIONSHIP, endNode NODE) - redirects the given RELATIONSHIP to the given end NODE.

Procedure

Examples

The below examples will further explain this procedure.

Redirect Target Node

The following creates Foo and Bar nodes that are connected by a FOOBAR relationship, as well as a solitary Antony node:
CREATE (f:Foo {value: "Foo"})-[rel:FOOBAR {a:1}]->(b:Bar {value: "Bar"})
CREATE (p:Person {name:'Antony'})
RETURN *
apoc.refactor.to.dataset
The following will change the target node of the FOOBAR relationship from the Bar node to the Antony node:
MATCH (f:Foo)-[rel:FOOBAR {a:1}]->(b:Bar)
MATCH (p:Person {name:'Antony'})
CALL apoc.refactor.to(rel, p)
YIELD input, output
RETURN input, output

If the above query is run, it will result in the following graph:

apoc.refactor.to

Redirect Source Node

The following creates Foo2 and Bar2 nodes that are connected by a FOOBAR2 relationship, as well as a solitary David node:
CREATE (f:Foo2 {value: "Foo2"})-[rel:FOOBAR2 {a:1}]->(b:Bar2 {value: "Bar2"})
CREATE (p:Person {name:'David'})
RETURN *
apoc.refactor.from.dataset
The following will change the source node of the FOOBAR2 relationship from the Foo node to the David node:
MATCH (f:Foo2)-[rel:FOOBAR2 {a:1}]->(b:Bar2)
MATCH (p:Person {name:'David'})
CALL apoc.refactor.from(rel, p)
YIELD input, output
RETURN input, output

If the above query is run, it will result in the following graph:

apoc.refactor.from