Help with recursion

What I want to do

Perform a recursive query with a second level structure

What I did

Here’s the query I have:

{
      query(func: uid($a)) {
        uid
        created
        leaf (first: 1) {
          stem
        }
        tree {
          uid
          created
          grape (first: 1) {
            num
          }
          branches {  # multiple
            uid
            created
            leaf (first: 1) {
               stem
            }
            tree {
               uid
               created
               grape (first: 1) {
                   num
               }
               branches etc.
            }
          }
        }
      }
    }

I want to replace “branches” with some form of a recursive query such that instead of referring to branches, I could instead point to a function. How would I go about doing that. I tried using the recurse keyword, but that doesn’t seem to like the multi-depth structure of this query. Also pagination asks for a sorting method, but given this sort of query, how would I paginate based on depth? The uid in the filter is what I call the “root node” where all nodes are linked to that specific node. Would I need to create a depth attribute?

Not sure if I get it. Something like this?

{
      Root(func: uid($a)) {
        uid
        created
        leaf (first: 1) {
          stem
        }
        tree {
          uid
          created
          grape (first: 1) {
            num
          }
          BRC as branches
        }
      }


   rq(func: uid(BRC)) @recurse {
        uid
        created
        leaf (first: 1)
        stem
        tree
        grape (first: 1)
        num
      }
   }

Thanks for the response; the recurse function doesn’t work as expected. rq returns a list that is out of order.

The structure is extremely recursive and follows like this:

{
  query(func: uid($a)) {
    uid
    created
    leaf (first: 1) {
      stem
    }
    tree {
      uid
      created
      grape (first: 1) {
        num
      }
      branches {
        uid
        created
        leaf (first: 1) {
          stem
        }
        tree {
          uid
          created
          grape (first: 1) {
            num
          }
          branches {
            uid
            created
            leaf (first: 1) {
              stem
            }
            tree {
              uid
              created
              grape (first: 1) {
                num
              }
              branches {
                uid
                created
                leaf (first: 1) {
                  stem
                }
                tree {
                  uid
                  created
                  grape (first: 1) {
                    num
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Right now, rq is returning things out of order, as in sometimes the child branch or tree may be incorrect, and some “tree” are missing.

You can try add sorting https://dgraph.io/docs/query-language/sorting/ but the problem is that Recurse isn’t a predictable query.

 rq(func: uid(BRC), orderasc: created) @recurse {
        uid
        created
        leaf (first: 1, orderasc: created)
        stem
        tree
        grape (first: 1, orderasc: created)
        num
      }

I didn’t really mean “out of order” in terms of sorting with a created field. I meant that there are missing nodes and sometimes the returned branch and tree edges (in rq) are not real edges.

If there are missing nodes, add more edges in order to the recurse expand them. I havent added cuz I dont know your context. But with this example you can go from there.

1 Like

The last thing to add in the rq is “branches” otherwise I wouldn’t have gotten full recursion. Thanks!