In the previous lesson, you learned how to create, update, and delete single nodes.
In this lesson, you will connect nodes using relationships.
Connecting Nodes
Let’s return to the example of adding the "Cli-Fi" genre node. This query will use the movies relationship in Genre and connect any "Jurassic" movie.
mutation MyMutation {
createGenres(
input: {
name: "Cli-Fi"
movies: { connect: { where: { node: { title: {contains: "Jurassic" } } } } }
}
) {
genres {
name
movies {
title
}
}
}
}Run the query and observe the following in the response:
-
The new genre "Cli-Fi" is created
-
Relationships are created between the new genre and the existing "Jurassic" movies
{
"data": {
"createGenres": {
"genres": [
{
"name": "Cli-Fi",
"movies": [
{ "title": "Lost World: Jurassic Park, The" },
{ "title": "Jurassic Park III" },
{ "title": "Jurassic Park" },
{ "title": "Jurassic World" }
]
}
]
}
}
}There are three key points to identify in this query:
-
The
inputobject takes the field values for the createdGenrenodeGraphQLinput: { name: "Cli-Fi" (1) } -
The
moviesfield is populated using theconnectargument and the "Jurassic" nodes return by awhereclause.GraphQLinput: { movies: { connect: { where: { node: { title {contains: "Jurassic" } } } } } (2) } -
The selection set includes the
moviesrelationship field and thetitlefield of the connectedMovienodes.GraphQLgenres { name movies { title } }
Check Your Understanding
Creating Relationships
True or False - A mutation operation can create relationships in a Neo4j database using the Neo4j GraphQL Library.
Choose the correct answer.
-
✓ True
-
❏ False
Hint
The result of a query can be used as input to a mutation operation.
Solution
The statement is True - the connect argument can be used to create relationships in a mutation operation.
Summary
This lesson covered nested mutation operations with the Neo4j GraphQL Library and the Neo4j GraphQL Toolbox. In the next lesson you will see how to add custom logic to our GraphQL API using Cypher and how to use GraphQL schema directives to configure the GraphQL API generated by the Neo4j GraphQL Library.