add better upsert example to docs (#1930)

This commit is contained in:
Rikin Kachhia 2019-04-03 15:05:51 +05:30 committed by GitHub
parent f147f6d6a6
commit 29d6e85e42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,44 +33,53 @@ upsert request in case of conflicts.
Update selected columns on conflict Update selected columns on conflict
----------------------------------- -----------------------------------
Insert a new object in the author table or, if the primary key constraint, ``author_pkey``, is violated, update the columns Insert a new object in the ``article`` table or, if the primary key constraint, ``article_pkey``, is violated, update
specified in ``update_columns``: the columns specified in ``update_columns``:
.. graphiql:: .. graphiql::
:view_only: :view_only:
:query: :query:
mutation upsert_author { mutation upsert_article {
insert_author( insert_article (
objects: [ objects: [
{name: "John", id: 10} {
id: 2,
title: "ex quis mattis",
content: "Pellentesque lobortis quam non leo faucibus efficitur",
published_on: "2018-10-12"
}
], ],
on_conflict: { on_conflict: {
constraint: author_pkey, constraint: article_pkey,
update_columns: [name] update_columns: [title, content]
} }
) { ) {
affected_rows returning {
returning{
id id
name title
content
published_on
} }
} }
} }
:response: :response:
{ {
"data": { "data": {
"insert_author": { "insert_article": {
"affected_rows": 1,
"returning": [ "returning": [
{ {
"name": "John", "id": 2,
"id": 10 "title": "ex quis mattis",
} "content": "Pellentesque lobortis quam non leo faucibus efficitur",
] "published_on": "2018-06-10"
}
]
} }
} }
} }
The ``published_on`` column is left unchanged as it wasn't present in ``update_columns``.
Ignore request on conflict Ignore request on conflict
-------------------------- --------------------------
If ``update_columns`` is an **empty array** then GraphQL Engine ignore changes on conflict. Insert a new object into If ``update_columns`` is an **empty array** then GraphQL Engine ignore changes on conflict. Insert a new object into
@ -101,7 +110,7 @@ the author table or, if the unique constraint, ``author_name_key``, is violated,
} }
} }
In this case, the insert mutation is ignored because there is a conflict. In this case, the insert mutation is ignored because there is a conflict and ``update_columns`` is empty.
Upsert in nested mutations Upsert in nested mutations