# Dgraph query to fetch all possible paths between two node types with depth limit

**URL:** <https://discuss.dgraph.io/t/dgraph-query-to-fetch-all-possible-paths-between-two-node-types-with-depth-limit/19997>\
**Category:** GraphQL\
**Tags:** graphql, kind:question, dgraph, pydgraph\
**Created:** [September 9, 2025, 2:28pm UTC](https://discuss.dgraph.io/t/dgraph-query-to-fetch-all-possible-paths-between-two-node-types-with-depth-limit/19997 "2025-09-09T14:28:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rg2609](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rg2609/32/11842_2.png) [@rg2609](https://discuss.dgraph.io/u/rg2609)\
**Post date:** [September 9, 2025, 2:28pm UTC](https://discuss.dgraph.io/t/dgraph-query-to-fetch-all-possible-paths-between-two-node-types-with-depth-limit/19997/1 "2025-09-09T14:28:26Z")

</div>

I’m working with a Dgraph dataset where nodes have different types and are connected by a `RELATED_TO` edge with facets.

Here’s a simplified version of the dataset:

```rdf
_:node_11 <dgraph.type> "Node_Type1" .
_:node_11 <name> "abc1" .
_:node_12 <dgraph.type> "Node_Type1" .
_:node_12 <name> "abc2" .
_:node_13 <dgraph.type> "Node_Type1" .
_:node_13 <name> "abc3" .

_:node_21 <dgraph.type> "Node_Type2" .
_:node_21 <name> "xyz1" .
_:node_22 <dgraph.type> "Node_Type2" .
_:node_22 <name> "xyz2" .
_:node_23 <dgraph.type> "Node_Type2" .
_:node_23 <name> "xyz3" .

_:node_31 <dgraph.type> "Node_Type3" .
_:node_31 <name> "asdf1" .
_:node_32 <dgraph.type> "Node_Type3" .
_:node_32 <name> "asdf2" .
_:node_33 <dgraph.type> "Node_Type3" .
_:node_33 <name> "asdf3" .

# Relationships
_:node_11 <RELATED_TO> _:node_21 (rel_type="CONNECTED") .
_:node_21 <RELATED_TO> _:node_31 (rel_type="CONNECTED") .

_:node_12 <RELATED_TO> _:node_23 (rel_type="CONNECTED") .
_:node_23 <RELATED_TO> _:node_42 (rel_type="CONNECTED") .

_:node_11 <RELATED_TO> _:node_43 (rel_type="CONNECTED") .
_:node_43 <RELATED_TO> _:node_31 (rel_type="CONNECTED") .

_:node_13 <RELATED_TO> _:node_23 (rel_type="CONNECTED") .
_:node_23 <RELATED_TO> _:node_43 (rel_type="CONNECTED") .

```

* * *

### What I want to achieve:

- Fetch **all paths** between nodes of type `Node_Type1` and `Node_Type2`

- Limit search to **depth = 2**

- Include **all possible paths** , not just the shortest one

* * *

### Example Expected Output:

From the above dataset, I would expect something like:

- `node_11 -> node_21 -> node_31`

- `node_11 -> node_43 -> node_31`

* * *

### What I tried:

I tried using the `shortest` query in Dgraph:

```graphql
{
  path as shortest(from: 0x146f52e7, to: 0x14801bbe, depth: 2) {
    RELATED_TO
  }

  path(func: uid(path)) {
    uid
    expand(_all_)
  }
}

```

But this only gives me the shortest path between two nodes, not **all possible paths** of a given depth.

* * *

### Question:

How can I query Dgraph to get **all paths** between two node types (`Node_Type1` → `Node_Type2`) with a depth of 2, instead of just the shortest path?

---

<div class="post-metadata">

**Author:** ![matthewmcneely](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matthewmcneely/32/4774_2.png) [@matthewmcneely](https://discuss.dgraph.io/u/matthewmcneely)\
**Post date:** [September 9, 2025, 7:14pm UTC](https://discuss.dgraph.io/t/dgraph-query-to-fetch-all-possible-paths-between-two-node-types-with-depth-limit/19997/2 "2025-09-09T19:14:54Z")

</div>

Check out the `numpaths` param to `shortest`: [shortest - Dgraph](https://docs.hypermode.com/dgraph/dql/shortest#shortest)

---

<div class="post-metadata">

**Author:** ![rg2609](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rg2609/32/11842_2.png) [@rg2609](https://discuss.dgraph.io/u/rg2609)\
**Post date:** [September 10, 2025, 6:11am UTC](https://discuss.dgraph.io/t/dgraph-query-to-fetch-all-possible-paths-between-two-node-types-with-depth-limit/19997/3 "2025-09-10T06:11:15Z")

</div>

The `numpaths` The parameter will only return the number of paths you specify, but I want to retrieve all available paths. For example, if I set the depth to 3 and there is one node connected at the third level, I might have 2 paths at depth 2 and 1 path at depth 3. In this scenario, I expect the output to include all 3 paths, not just the 2 shortest ones.

---

<div class="post-metadata">

**Author:** ![matthewmcneely](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matthewmcneely/32/4774_2.png) [@matthewmcneely](https://discuss.dgraph.io/u/matthewmcneely)\
**Post date:** [September 15, 2025, 7:48pm UTC](https://discuss.dgraph.io/t/dgraph-query-to-fetch-all-possible-paths-between-two-node-types-with-depth-limit/19997/4 "2025-09-15T19:48:19Z")

</div>

Finally had a chance to dig around in DQL with this. The `shortest` func is doing what it does, returning the shortest path. Here’s an example DQL that lists all the depth 2 paths between the types. It’s not ideal because it’s fixed to three hops, but hopefully it might give you a starting point from which you can work:

```auto
{
  var(func: type(Node_Type1)) {
    s as uid
    RELATED_TO @facets(eq(rel_type, "CONNECTED")) {
      m as uid
      RELATED_TO @facets(eq(rel_type, "CONNECTED")) {
        t as uid
      }
    }
  }

  paths(func: uid(s)) @normalize {
    from_uid: uid
    from_name: name

    RELATED_TO @facets(eq(rel_type, "CONNECTED")) @filter(uid(m)) {
      mid_uid: uid
      mid_name: name

      RELATED_TO @facets(eq(rel_type, "CONNECTED")) @filter(uid(t)) {
        to_uid: uid
        to_name: name
      }
    }
  }
}

```
