Upsert Objects

Introduction

Upserting, which is the method of updating an existing object or creating a new one if it doesn't exist, can be achieved using the update <object type> mutation.

To enable upserting, pass true to the upsert argument on the update query. Upserting works primarily with external IDs, so also be sure to pass the external ID to the update query.

Example

This example shows upserting a Movie. In this case, if a Movie with the external_id movie1 exists, then an update will be run against that object. Otherwise, a new Movie object will be created.

mutation upsertMutation {
  updateMovie(
    external_id: "indiana-jones-and-the-upserted-object", 
    upsert: true, 
    movie: {
      title: "Indiana Jones and the Upserted Object",
      synopsis: "Indiana Jones teams up with a tech genius to prevent a powerful artifact capable of upserting reality—updating or creating events—from falling into the hands of a nefarious tech mogul, embarking on a perilous journey through jungles, temples, and high-tech labs"
  }) {
    uid
    title
    synopsis
  }
}

Result:

{
  "data": {
    "updateMovie": {
      "uid": "01HY0D2HBWJGC3BYCBP84GJPHP",
      "title": "Indiana Jones and the Upserted Object",
      "synopsis": "Indiana Jones teams up with a tech genius to prevent a powerful artifact capable of upserting reality—updating or creating events—from falling into the hands of a nefarious tech mogul, embarking on a perilous journey through jungles, temples, and high-tech labs"
    }
  }
}