Getting the current depth in recurse query

I am trying to get the hops that are needed to reach from node a to node b in a recursive fashion

I know that this is possible to achieve using the shortest path function, but what would be even better is to get the current depth inside a recurse function.

{
  tree(func: eq(name, leaf)) @recurse  {
    name as uid
    ~next {}
    #current_depth
  }
  findtree(func: uid(name)) @filter(eq(dgraph.type, Tree)) {
    name
  }
}

By this way I will be getting the depth at which the Tree is found as well. This tree in this case is root node, but it can be some intermediate node as well based on filters.

Is there a better way of getting the depth or steps from the leaf to the tree in this case.

There are different kinds of nodes before reaching tree in this case, so using a second function to filter them, else a filter could be added on ~next

Just let me know if this even possible? Is there an alternate way to do the same ? I tried writing a function for shortest path, but it accepts only single uids, not lists, what if I want to get the shortest distance between multiple node pairs

Distance between [a,b,c] * [x,y,z]. Distance or shortest path between the cross product of the lists

I saw a similar question getting closed without a response, idle for 30 days, I will keep this ticking for 45 days, then leave it alone .

I didn’t get what you are doing. By “get the current depth” do you mean that the query returns a depth value?

You can try

findtree(func: uid(name)) @filter(eq(dgraph.type, Tree)) {
count(uid)
}

BTW, you don’t need to use {} in the query body.

By current depth I meant the total number of hops (distance) needed to reach a particular parent node.

Which is basically the depth value in the recurse query. Internally depth value will be used in the recursive function. Just wanted to know of that value is available as a variable in Graphql

An equivalent cypher query would look like this.

MATCH p=(d:Tree)-[*]->(c:Code) 
WHERE c.value IN {Codes} WITH d, length(p) as distance 
RETURN ID(d) as id, d.name as name,  distance

It would be great if I can find an equivalent query in Dgraph.

The thing which I wanted to know is how to get the distance value as got in the cypher query

I am not an expert on Cypher. But are you sure this is a recurse query?

It seems to me to be a query that says “Match any Tree entity that has any edge for Code entity”. This does not seem to me to be a recursive query itself in the sense of traverse. I don’t know how Cypher does recurse query tho.

Consider this query below:

{
  var(func: uid(T)){
    g as count(uid)
  }
  stats(){
    F as TotalTraversed : sum(val(g))
    Hops: math(F-1)
  }
  q(func: eq(name, "Root")) @recurse {
    T as uid
    name
    next
  }
}

You should get a response like so:

{
    "stats": [
      {
        "TotalTraversed": 5
      },
      {
        "Hops": 4
      }
    ]
...
 }

This is the Sample

{
   "set": [
      {
         "name": "Root",
         "next": [
            {
               "name": "level_1",
               "next": [
                  {
                     "name": "level_2",
                     "next": [
                        {
                           "name": "level_3",
                           "next": [
                              {
                                 "name": "level_4"
                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

This helps , will try it out and let you know