apoc.refactor.setType

Details

Syntax

apoc.refactor.setType(rel, newType) :: (input, output, error)

Description

Changes the type of the given RELATIONSHIP.

Input arguments

Name

Type

Description

rel

RELATIONSHIP

The relationship to change the type of.

newType

STRING

The new type for the relationship.

Return arguments

Name

Type

Description

input

INTEGER

The id of the given relationship.

output

RELATIONSHIP

The id of the new relationship with the updated type.

error

STRING

The message if an error occurred.

Refactoring nodes using Cypher

Node labels and relationship types can be referenced dynamically in Cypher without using APOC.

Cypher syntax for creating, matching and merging labels and types dynamically
CREATE (n1:$(label))-[r:$(type)]->(n2:$(label))
MERGE (n1:$(label))-[r:$(type)]->(n2:$(label))
MATCH (n1:$(label))-[r:$(type)]->(n2:$(label))

The dynamically calculated type must evaluate to a STRING or LIST<STRING>. For more information, see the Cypher Manual → CREATE, MERGE, MATCH.

Usage Examples

The examples in this section are based on the following graph:

CREATE (f:Foo)-[rel:FOOBAR]->(b:Bar);

The following queries change the relationship type from FOOBAR to NEW-TYPE using APOC and Cypher:

apoc.refactor.setType
MATCH (f:Foo)-[rel:FOOBAR]->(b:Bar)
CALL apoc.refactor.setType(rel, 'NEW-TYPE')
YIELD input, output
RETURN input, output;
Using Cypher
MATCH (f:Foo)-[rel:FOOBAR]->(b:Bar)
CREATE (f)-[r:`NEW-TYPE`]->(b)
SET r = properties(rel)
DELETE rel
RETURN id(r), type(r)

If we execute this query, it will result in the following output:

Results
input output

30

[:`NEW-TYPE`]

And the graph now looks like this:

apoc.refactor.setType2

APOC is not inverting the type; instead, it adds a new relationship with the desired direction and deletes the original.