Filter parent-child edges

I’m evaluating Dgraph for my use case, but I hit this scenario: I have some edges in a parent-child hierarchy (up to depth 4). How can I find all leaves that have an ancestor (at any level) matching a predicate?

This is how I search in parents only:

{
  nodes(func: eq(type, "MyType")) @cascade {
    name
    child.of @filter(eq(name, "Foo bar"))
  }
}

Its a bit hard to understand what you are doing here, could you give some more information. Do you want to find all leave nodes which have a particular predicate as parent? @recurse usually works for these type of queries.

I’m dealing with biology, so I have a taxonomy like Animals → Mammals → Primates → Gorilla, chimpanzees, Bonobo…
I want to query the dataset for all Primates or Mammals and so on.

I suppose you want all Primates which have an animal ancestor or something? Try something like the below.

{
  var(func: eq(type, "Animal")) @cascade @recurse {
    mammals as child.of @filter(eq(type, "Mammal"))
  }

  me(func: uid(mammals)) {
    name
  }
}
1 Like