Returning Nth degree related nodes

Reposting to try and start fresh with a clearer question:

I have Category nodes, which can be nested infinitely - along with Product nodes, which belong to one or more Categories.

I’d like to return a list of root Categories (those with no parents) - and with each of these a list of all Products belonging to:

  1. that root Category or
  2. any child Category connected directly or indirectly (2nd level subcategory) to that root Category.

Using recurse isn’t working for me here, because each category is returned with only the products directly related to it, and I’d like to return Nth degree related Products.

Does that make sense? Any help would be amazing!

Check if this example works for you.


 rootCategories(func: type(Category)) @filter(NOT has(~IncomingCategory)) {
    CD as childCategory
# "not has Incoming Category" means that this will fetch only "root category" or "no parents"
 B as var(func: uid(CD)) @recurse { 
    C as someConnection #more childs?
#this will expand in the "child" from the "root" starting from the 2nd level.

  listOfChilds(func: uid(B, C)) { # added C just in case
# This above will list, kind of "flattened", all child found.

This will return a list of root categories. And also a list of children starting from the 2nd level in a flattened way.

listofChilds here returns the children, but in this query the root is returned separately in rootCategories - how would I return a list with root Categories at the root, and child products listed under each?

@MichelDiz I’ve tried as best I can, but can’t figure this out - I’m thinking it might be better for me to stick with Postgres :confused:

Well, in Dgraph you can’t return different responses in the same response. That’s why I have shared 2 queries. You can’t merge them. Also, we don’t have a @recurse directive for subqueries. Only Cascade and normalize were supported. I think we have a ticket for recursing but seems like there’s no demand from the community and this gets forgotten.

Another way to make it “work” would be the loop(foreach). But it is in the roadmap still.

It is up to you. I particularly gave up completely on improving myself in SQL. When I found Dgraph I realized that I didn’t need anything else. Even with the limitations at the time. I saw that anything was possible both for the future(new features) or capable of being solved on my side (with my code). SQL to me is archaic. And look, I’m 30 years old.

I wouldn’t give up that easy. But maybe this is because here in Brazil we have the motto “never give up, never surrender”. However I traded SQL for DQL U.U

@MichelDiz Ok, I’ll give it one more honest try :slight_smile:
Do you know if theres a DB management tool / browser that can be used with Slash GraphQL - similar to:

for relational dbs?

Nope, we have Ratel only and the Slash GUI there.