Mutations Overview
Mutations can be used to insert, update, or delete data. Dgraph automatically generates GraphQL mutation for each type that you define in your schema
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
Mutations allow you to modify server-side data, and it also returns an object based on the operation performed. It can be used to insert, update, or delete data. Dgraph automatically generates GraphQL mutations for each type that you define in your schema. The mutation field returns an object type that allows you to query for nested fields. This can be useful for fetching an object’s new state after an add/update, or to get the old state of an object before a delete.
The following mutations would be generated from the schema.
Input objects
Mutations require input data, such as the data, to create a new object or an object’s ID to delete. Dgraph auto-generates the input object type for every type in the schema.
Return fields
Each mutation provides a set of fields that can be returned in the response. Dgraph auto-generates the return payload object type for every type in the schema.
Multiple fields in mutations
A mutation can contain multiple fields, just like a query. While query fields
are executed in parallel, mutation fields run in series, one after the other.
This means that if we send two updateAuthor
mutations in one request, the
first is guaranteed to finish before the second begins. This ensures that we
don’t end up with a race condition with ourselves. If one of the mutations is
aborted due error like transaction conflict, we continue performing the next
mutations.
Example: mutation on multiple types
Variables:
Union mutations
Mutations can be used to add a node to a union
field in a type.
For the following schema,
This is the mutation for adding members
to the Home
type:
Vector embedding mutations
For types with vector embeddings Dgraph automatically generates the add mutation. For this example of add mutation we use the following schema.
Note: the embeddings are generated outside of Dgraph using any suitable machine learning model.
Examples
You can refer to the following link for more examples.
Was this page helpful?