Relationship Mutations With Nested Mutations

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.

GraphQL
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:

  1. The new genre "Cli-Fi" is created

  2. Relationships are created between the new genre and the existing "Jurassic" movies

json
{
  "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:

  1. The input object takes the field values for the created Genre node

    GraphQL
        input: {
          name: "Cli-Fi" (1)
        }
  2. The movies field is populated using the connect argument and the "Jurassic" nodes return by a where clause.

    GraphQL
        input: {
          movies: { connect: { where: { node: { title_CONTAINS: "Jurassic" } } } } (2)
        }
  3. The selection set includes the movies relationship field and the title field of the connected Movie nodes.

    GraphQL
        genres {
          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.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?