Multiple mutations in a request¶
Table of contents
Execution¶
If multiple mutations are part of the same request, they are executed sequentially in a single transaction. If any of the mutations fail, all the executed mutations will be rolled back.
Run multiple top level mutations in the same request¶
Example: Delete all article objects written by an author and update the author object:
mutation reset_author {
  delete_article (
    where: {author_id: {_eq: 6}}
  ) {
    affected_rows
  }
  update_author (
    where: {id: {_eq: 6}}
    _set: {name: "Cory"}
  ) {
    returning {
      id
      name
      articles {
        id
        title
      }
    }
  }
}
mutation reset_author {
  delete_article (
    where: {author_id: {_eq: 6}}
  ) {
    affected_rows
  }
  update_author (
    where: {id: {_eq: 6}}
    _set: {name: "Cory"}
  ) {
    returning {
      id
      name
      articles {
        id
        title
      }
    }
  }
}
{
    "data": {
      "delete_article": {
        "affected_rows": 2
      },
      "update_author": {
        "returning": [
          {
            "id": 6,
            "name": "Cory",
            "articles": []
          }
        ]
      }
    }
  }
Insert an object and a nested object in the same mutation¶
If you are trying to insert multiple objects which have relationships between them, you can use nested inserts.
Example: Insert a new article object with its author and return the inserted article object with its author
in the response:
mutation insert_article {
  insert_article(
    objects: [
      {
        title: "Article 1",
        content: "Sample article content",
        author: {
          data: {
            name: "Cory"
          }
        }
      }
    ]
  ) {
    affected_rows
    returning {
      id
      title
      author {
        id
        name
      }
    }
  }
}
mutation insert_article {
  insert_article(
    objects: [
      {
        title: "Article 1",
        content: "Sample article content",
        author: {
          data: {
            name: "Cory"
          }
        }
      }
    ]
  ) {
    affected_rows
    returning {
      id
      title
      author {
        id
        name
      }
    }
  }
}
{
  "data": {
    "insert_article": {
      "affected_rows": 2,
      "returning": [
        {
            "id": 21,
            "title": "Article 1",
            "author": {
              "id": 11,
              "name": "Cory"
            }
        }
      ]
    }
  }
}
