Ask a Question

Update Mutations in GraphQL

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 {} or an empty set{} doesn’t have any effect on the update mutation.
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.