How to delete with cascade?

Here is my schema:

type Contact {
  id: ID
  name: String!
  relationshipsTo: [Relationship] @hasInverse(field: of)
  relationshipsOf: [Relationship] @hasInverse(field: to)
}
type Relationship {
  id: ID
  name: String @search(by: [hash])
  of: Contact
  to: Contact
  meta: [RelationshipMeta]
}
type RelationshipMeta {
  id: ID
  name: String!
  value: String!
}

I have existing contacts, that I do not want to delete, I have added a bunch of relationships and want to delete all of the relationships and meta that match a name.

mutation {
  deleteRelationship(filter: {name:"Child"}) {
    numUids
  }
}

This works, but how can I also delete the associated subgraph meta in a single mutation.

In SQL world this would be called a cascade delete. It is important though that I only want to cascade delete to the meta edge and not the to, or of edges

I also need this.

Cascading deletes are not possible through a single GraphQL mutation but could be achieved through a DQL query for for your use-case.

Query:

{
  relations as me(func: eq(dgraph.type, "Relationship")) @filter(eq(name, "Child")) {
    rm as meta
  }
}

DeleteJSON:

[{
  "uid": "uid(relations)"
},
{
  "uid": "uid(rm)"
}]
2 Likes

Although this use-case might be solved using DQL, let’s continue this discussion here: