apoc.create.relationship

Details

Syntax

apoc.create.relationship(from, relType, props, to) :: (rel)

Description

Creates a RELATIONSHIP with the given dynamic relationship type.

Input arguments

Name

Type

Description

from

NODE

The node from which the outgoing relationship will start.

relType

STRING

The type to assign to the new relationship.

props

MAP

The properties to assign to the new relationship.

to

NODE

The node to which the incoming relationship will be connected.

Return arguments

Name

Type

Description

rel

RELATIONSHIP

The created relationship.

Creating a relationship with dynamic types using Cypher

Relationship types can be referenced dynamically in Cypher without using APOC.

Cypher syntax for creating a type dynamically
CREATE ()-[r:$(type)]->()

The dynamically calculated type must evaluate to a STRING or LIST<STRING>. For more information, see the Cypher Manual → CREATE nodes and relationships using dynamic node labels and relationship types.

Usage Examples

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

CREATE (p:Person {name: "Tom Hanks"})
CREATE (m:Movie {title:"You've Got Mail"});

The example below demonstrates how to use APOC and Cypher to create a relationship by passing in its type and properties dynamically using parameters:

The following creates relType and properties parameters:

:param relType =>  ("ACTED_IN");
:param properties => ({roles: ["Joe Fox"]});
apoc.create.relationship
MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
CALL apoc.create.relationship(p, $relType, $properties, m)
YIELD rel
RETURN rel;
CREATE clause
MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
CREATE (p)-[rel:$($relType) $properties]->(m)
RETURN rel;
Results
rel

[:ACTED_IN {roles: ["Joe Fox"]}]