Contember offers advanced mutations for data modification. For every entity there are 4 operations available - create, update, delete and upsert. A GraphQL schema for your entity Post will look like this:
type Mutation {
createPost(data: PostCreateInput!): PostCreateResult!
deletePost(by: PostUniqueWhere!): PostDeleteResult!
updatePost(by: PostUniqueWhere!, data: PostUpdateInput!): PostUpdateResult!
upsertPost(by: PostUniqueWhere!, update: PostUpdateInput!, create: PostCreateInput!): PostUpsertResult!
}
Creating records
Using a create mutation you can create new records. Under node field you can fetch inserted record with a generated identifier.
mutation {
createPost(data: {title: "Hello world", publishedAt: "2019-12-20"}) {
ok
node {
id
}
}
}
Updating records
Use an update mutation to change existing record. For this operation, you need an unique identifier of the record. It can be either an ID or any other custom unique field. Using node field, you can fetch updated record.
mutation {
updatePost(
by: {id: "97644abb-0671-486b-9c51-b72b377ec1d9"}
data: {title: "Hello Contember"}
) {
ok
node {
id
}
}
}
Deleting records
Use a delete mutation you can delete records. You also need a unique identifier for this operation. Using node field you can fetch a record before it is actually deleted.
mutation {
deletePost(
by: {id: "97644abb-0671-486b-9c51-b72b377ec1d9"}
) {
ok
node {
title
}
}
}
Upserting records
upsert is a special operation, which updates an existing row or a creates a new one when the requested row does not exist. Beside the unique identifier you must provide two data inputs - one for creating and one for updating existing row.
Using node field, you can fetch updated/created record.
mutation {
upsertPost(
by: {slug: "hello-contember"}
create: {title: "Hello Contember"}
update: {title: "Hello Contember again!"}
) {
ok
node {
id
}
}
}
Mutations on relations
In a single mutation, you can execute nested mutations on relations using this operations:
connect- connects a record you specify by unique identifier.disconnect- disconnects record. For "has many" relations, you have to identify a record using unique identifiercreate- creates a record and connects it automatically after that.update- updates referenced record. For "has many" relations, you have to identify a record using unique identifierdelete- deletes referenced record. For "has many" relations, you have to identify a record using unique identifierupsert- updates given record or creates a new record when there is nothing to update
If you are e.g. connecting a record, which does not exist, the mutation will fail.
mutation {
createPost(data: {
title: "Hello world"
category: {connect: {id: "af86a9a3-349a-412f-b95b-725c4b9061b8"}}
tags: [{create: {name: "graphql"}}]
}) {
ok
}
}
Declaratively setting a collection with set
For "has many" relations (both one has many and many has many) you can declaratively set
the whole collection so that it ends up containing exactly the records you describe, without
having to fetch and disconnect/delete the existing ones yourself.
Each set item is one of the positive operations connect, create, connectOrCreate,
update or upsert (no delete/disconnect). Any record that is currently related but is
not part of the provided list is an orphan and is removed according to orphanStrategy:
disconnect(default) - the orphan stays in the database, only the relation is removed. This is the safest option and is used whenorphanStrategyis omitted.delete- the orphan record is deleted entirely.
On a many has many relation, delete removes the record itself, not just the link, so it also
disappears from every other owner. In the example below, a tag dropped from this post is deleted
for all posts that use it.
Orphans are removed before the listed records are established. That ordering matters for a
unique key that includes the owner - unique(['locale', 'post']), the "replace all translations"
shape - where the old and the new row cannot coexist. A record the list names is never removed
and re-added: which records are orphans is decided up front, from the collection as it was before
the mutation, so a record the list creates cannot be an orphan either.
If deleting an orphan cascades into a record the list names, the mutation fails and rolls back
instead of creating that record anew.
mutation {
updatePost(by: {id: "af86a9a3-349a-412f-b95b-725c4b9061b8"}, data: {
tags: {
orphanStrategy: delete
set: [
{connect: {id: "..."}}
{create: {name: "graphql"}}
]
}
}) {
ok
}
}
set must be the only operation provided for that relation - it cannot be combined with other
per-item operations in the same list. It is currently supported on "has many" relations only.
set has to read the current members to tell which of them became orphans, so it is only present
in the schema for roles that can read the target entity. Likewise orphanStrategy only appears
when the role is allowed to delete the target entity. Where the role's read permission carries a
row-level predicate, members it hides are invisible to the diff and are therefore kept, not
orphaned - set replaces the part of the collection the role can see.
Each item behaves exactly as the same operation does outside set. In particular an update
item addresses an existing member on a one has many relation (its by is completed with the
owner, so a partial composite unique key such as {locale: "cs"} identifies a row), and fails
with NotFoundOrDenied if the record is not a member; on a many has many relation the same
item updates the record and connects it.
On a one has many relation whose owning side is not nullable, an orphan cannot be disconnected -
there is nowhere to put the null. Use orphanStrategy: delete for such relations; the default
disconnect fails with a not-null constraint violation.
Transactions
Each mutation (with its nested mutations) is wrapped into individual transaction, meaning that for query:
mutation {
post1: createPost(data: {
title: "Hello world"
tags: [{create: {name: "graphql"}}]
}) {
ok
}
post2: createPost(data: {
title: "Lorem ipsum"
tags: [{create: {name: "contember"}}]
}) {
ok
}
}
There will be two database transaction.
If you need to wrap it into a single transaction, use transaction mutation:
mutation {
transaction {
post1: createPost(data: {
title: "Hello world"
tags: [{create: {name: "graphql"}}]
}) {
ok
}
post2: createPost(data: {
title: "Lorem ipsum"
tags: [{create: {name: "contember"}}]
}) {
ok
}
ok
}
}
If anything goes wrong, whole transaction is rolled back.
As you can see in the example, you can get the result of mutation (ok field but also errors, validation etc.) on both levels.
Batch operations
Currently there are no mutations for batch updates available. But you can always connect to a PostgreSQL and execute an update using SQL.