2018-09-11 14:11:24 +03:00
|
|
|
Multiple mutations in a request
|
|
|
|
===============================
|
|
|
|
If multiple mutations are part of the same request, they are executed **sequentially**. If any of the mutations fail,
|
2018-10-10 09:32:03 +03:00
|
|
|
all the executed mutations will be rolled back (i.e. all the mutations are run as a **transaction**).
|
2018-09-11 14:11:24 +03:00
|
|
|
|
2018-11-19 13:43:17 +03:00
|
|
|
Run multiple top level mutations transactionally
|
|
|
|
------------------------------------------------
|
|
|
|
**Example:** Delete all ``article`` objects written by an author and update the ``author`` object
|
2018-09-11 14:11:24 +03:00
|
|
|
|
|
|
|
.. graphiql::
|
|
|
|
:view_only:
|
|
|
|
:query:
|
2018-11-19 13:43:17 +03:00
|
|
|
mutation reset_author {
|
|
|
|
delete_article (
|
|
|
|
where: {author_id: {_eq: 6}}
|
|
|
|
) {
|
|
|
|
affected_rows
|
|
|
|
}
|
|
|
|
update_author (
|
|
|
|
where: {id: {_eq: 6}}
|
|
|
|
_set: {name: "Cory"}
|
2018-09-11 14:11:24 +03:00
|
|
|
) {
|
|
|
|
returning {
|
|
|
|
id
|
|
|
|
name
|
2018-11-19 13:43:17 +03:00
|
|
|
articles {
|
|
|
|
id
|
|
|
|
title
|
|
|
|
}
|
2018-09-11 14:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-19 13:43:17 +03:00
|
|
|
}
|
|
|
|
:response:
|
|
|
|
{
|
|
|
|
"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
|
|
|
|
|
|
|
|
.. graphiql::
|
|
|
|
:view_only:
|
|
|
|
:query:
|
|
|
|
mutation insert_article {
|
2018-09-11 14:11:24 +03:00
|
|
|
insert_article(
|
|
|
|
objects: [
|
2018-11-19 13:43:17 +03:00
|
|
|
{
|
|
|
|
id: 21,
|
|
|
|
title: "Article 1",
|
|
|
|
content: "Sample article content",
|
|
|
|
author: {
|
|
|
|
data: {
|
|
|
|
id: 11,
|
|
|
|
name: "Cory"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 14:11:24 +03:00
|
|
|
]
|
|
|
|
) {
|
2018-11-19 13:43:17 +03:00
|
|
|
affected_rows
|
2018-09-11 14:11:24 +03:00
|
|
|
returning {
|
|
|
|
id
|
|
|
|
title
|
2018-11-19 13:43:17 +03:00
|
|
|
author {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
2018-09-11 14:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:response:
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"insert_article": {
|
2018-11-19 13:43:17 +03:00
|
|
|
"affected_rows": 2,
|
2018-09-11 14:11:24 +03:00
|
|
|
"returning": [
|
|
|
|
{
|
2018-11-19 13:43:17 +03:00
|
|
|
"id": 21,
|
|
|
|
"title": "Article 1",
|
|
|
|
"author": {
|
|
|
|
"id": 11,
|
|
|
|
"name": "Cory"
|
|
|
|
}
|
2018-09-11 14:11:24 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 13:43:17 +03:00
|
|
|
}
|