Delete Mutations - Graphql

Delete Mutations allows you to delete objects of a particular 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 delete mutation. Delete Mutations takes filter as an input to select specific objects and returns the state of the objects before deletion.

deleteAuthor(filter: AuthorFilter!): DeleteAuthorPayload
type DeleteAuthorPayload {
	author(filter: AuthorFilter, order: AuthorOrder, first: Int, offset: Int): [Author]
	msg: String
	numUids: Int
}

Example: Delete mutation using variables

mutation deleteAuthor($filter: AuthorFilter!) {
  deleteAuthor(filter: $filter) {
    msg
    author {
      name
      dob
    }
  }
}

Variables:

{ "filter":
  { "name": { "eq": "A.N. Author" } }
}

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/delete/
type Author {
    ...
    posts: [Post] @hasInverse(field: author)
}

type Post {
    ...
    author: Author
}

How can I perform deletion mutation on Post with filter variable like author: {usename: “david”}

1 Like

At present, it is not possible to filter a type by its objects fields. Filtering is supported only on scalar fields.
So, a direct deletion like this is not possible at present.

But, there are discussions about supporting it which you might want to take a look: Proposal Nested Object Filters for GraphQL rewritten as var blocks in DQL