Create Related Objects

The create option in a relationship input allows you to create new objects and their relationships in a single mutation.

In the “parent” input object (whether in a createObject or updateObject mutation) where the object has relationships defined to other objects, a relationships field should be available. This object will include a list of objects that relationships can be created to, and within the relationship input the create input allows you to create a new object to be related to.

📘

It is also possible to create children of children in the same request.

The body of the create object can either be a single object or a list of objects.

Creating A Single Related Object

In this example a new season, with the title "A new season", is created and a new episode, with the title "A new episode", is created and a relationship between the season and the episode is created.

mutation createSeason {
  createSeason(season: {
    title: "A new season", 
    relationships: {
      episodes: {
        create: {
          title: "A new episode"
        }
      }
    }
  }
  ) 
  {
    uid
    episodes {
      uid
    }
  }
}

Creating Multiple Related Objects

In this example a new season, with the title "A new season", is created and two new episodes, one with the title "A new episode" and a second with the title "Another new episode", and a relationships between the season and the new episodes is created.

mutation createSeason {
  createSeason(season: {
    title: "A new season", 
    relationships: {
      episodes: {
        create: [
          {
            title: "A new episode"
          },
          {
            title: "Another new episode"
          }
        ]
      }
    }
  ) 
  {
    uid
    episodes {
      uid
    }
  }
}

Creating Nested Relationships

In this example a new brand, with the title "A new brand" is created, a new season, with the title "A new season" is created and a new episode, with the title "A new episode", is created.
The following relationships are then created:

  • Brand to season
  • Season to episode
mutation createBrand {
  createBrand(brand: {
    title: "A new brand", 
    relationships: {
      seasons: {
        create: {
          title: "A new season"
          relationships: {
            episodes: {
              create: {
                title: "A new episode"
              }
            }
          }
        }
      }
    }
  ) 
  {
    uid
    seasons {
      uid
      episodes {
        uid
      }
    }
  }
}