GraphQL Mutations
Contember offers advanced mutations for data modification. For every entity there are 3 operations available - create, update and delete. A GraphQL schema for your entity Post will look like this:
#
Creating recordsUsing a create
mutation you can create new records. Under node
field you can fetch inserted record with a generated identifier.
#
Updating recordsUse 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.
#
Deleting recordsUse 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.
#
Upserting recordsupsert
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.
#
Mutations on relationsIn 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.
#
TransactionsEach mutation (with its nested mutations) is wrapped into individual transaction, meaning that for query:
There will be two database transaction.
If you need to wrap it into a single transaction, use transaction
mutation:
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 operationsCurrently there are no mutations for batch updates available. But you can always connect to a PostgreSQL and execute an update using SQL.