Update Mutations allows you to update existing objects of a particular type. It allows to filter nodes and, set and remove any field belonging to a type.
We use the following schema to demonstrate some examples.
Schema:
type Author {
id: ID!
name: String! @search(by: [hash])
dob: DateTime
posts: [Post]
}
type Post {
postID: ID!
title: String! @search(by: [term, fulltext])
text: String @search(by: [fulltext, term])
datePublished: DateTime
}
Dgraph automatically generates input and return type in the schema for the update mutation. Update mutation takes filter as an input to select specific objects. You can specify set and remove operation on fields belonging to the filtered objects. It returns the state of the objects after updation.
updatePost(input: UpdatePostInput!): UpdatePostPayload
input UpdatePostInput {
filter: PostFilter!
set: PostPatch
remove: PostPatch
}
type UpdatePostPayload {
post(filter: PostFilter, order: PostOrder, first: Int, offset: Int): [Post]
numUids: Int
}
Example: Update set mutation using variables
mutation updatePost($patch: UpdatePostInput!) {
updatePost(input: $patch) {
post {
postID
title
text
}
}
}
Variables:
{ "patch":
{ "filter": {
"postID": ["0x123", "0x124"]
},
"set": {
"text": "updated text"
}
}
}
Example: Update remove mutation using variables
mutation updatePost($patch: UpdatePostInput!) {
updatePost(input: $patch) {
post {
postID
title
text
}
}
}
Variables:
{ "patch":
{ "filter": {
"postID": ["0x123", "0x124"]
},
"remove": {
"text": "delete this text"
}
}
}
Examples
You can refer to the following link for more examples.
This is a companion discussion topic for the original entry at https://dgraph.io/docs/graphql/mutations/update/