You are looking at the docs for the unreleased master branch of Dgraph. The latest version is v20.11.
Report Issue Edit Page

Update Mutations

Update mutations let you update existing objects of a particular type. With update mutations, you can filter nodes and set or 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 types in the schema for the update mutation. Update mutations take filter as an input to select specific objects. You can specify set and remove operations on fields belonging to the filtered objects. It returns the state of the objects after updating.

Note Executing an empty remove {} statement is equivalent to remove * and will delete everything for the predicate in the specified filter.
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
}

Set

For example, an 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"
    }
  }
}

Remove

For example an 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.

Continue the conversation on Discuss.