# Sub-graph query

**URL:** <https://discuss.dgraph.io/t/sub-graph-query/9895>\
**Category:** GraphQL\
**Tags:** kind:question\
**Created:** [August 29, 2020, 8:59am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895 "2020-08-29T08:59:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soultrans](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/soultrans/32/4919_2.png) [@Soultrans](https://discuss.dgraph.io/u/Soultrans)\
**Post date:** [August 29, 2020, 8:59am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/1 "2020-08-29T08:59:04Z")

</div>

Does Dgraph support sub-graph query?  
For example,I only get part of the information in the graph.There are a lot of nodes I don’t need in the result of my query. I just need to get the nodes I need.  
Graph like this ![1](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/5/5dcee848745dac372f58baac738812c4d4b4e80b.png)  
But there are so many nodes, I just want a subgraph like below  
 ![2](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/a/a82b11ba44c5407f51ca850a6ca65c647b6172cd.png)  
and this  
 ![3](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/0/06db66f8a52daf5225311d2fb62f43c227b37132.png)  
How can I do that?

---

<div class="post-metadata">

**Author:** ![rajas](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rajas/32/4393_2.png) [@rajas](https://discuss.dgraph.io/u/rajas)\
**Post date:** [August 29, 2020, 12:46pm UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/2 "2020-08-29T12:46:43Z")

</div>

Hi @Soultrans,

As I understand looking at the diagram, you want to see only those nodes from level 2 which have at least one outgoing edge.

If that is the case you can use `filter` in the query.  
Suppose every node has predicate `name` and the outgoing edges are represented by `isconnectedto` . You can use something like this:

```auto
{
  find_subgraph(func: uid(<root_uid>)) {
    name 
    isconnectedto @filter(gt(count(isconnectedto), 0)){
      name
      isconnectedto {
        name
      }  
    }
  }
}

```

Note that \<root\_uid\> will have to be replaced by uid of node1.

Simple Example using filter: [https://dgraph.io/docs/tutorial-3/](https://dgraph.io/docs/tutorial-3/)

---

<div class="post-metadata">

**Author:** ![Soultrans](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/soultrans/32/4919_2.png) [@Soultrans](https://discuss.dgraph.io/u/Soultrans)\
**Post date:** [August 31, 2020, 2:06am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/3 "2020-08-31T02:06:17Z")

</div>

I’m sorry，maybe I didn’t make it clear.  
For example,I get a diagram，but I just want an infographic with blue nodes.

```auto
 {
  all(func: eq(department, "principal","sdirector","aleader","teacherA","student")) @recurse(depth: 3) {
    department 
    subordinate_departments
  }
}

```

The results of my query are shown below

 ![1](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/1/122c845f813356da5c9047376380af83294c315e.png)

I want something like this：

 ![2](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/0/05216ddd10dfe89cc84b166646321eb2974f0dae.png)

Or two pictures like this：

 ![3](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/8/82186c8e442f488c72a8b459bc6213e2fff1ae0b.png)  
 ![4](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/5/5273a388e950172eee644bfed7ff22ab9fc329e2.png)

---

<div class="post-metadata">

**Author:** ![chewxy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/chewxy/32/4339_2.png) [@chewxy](https://discuss.dgraph.io/u/chewxy)\
**Post date:** [August 31, 2020, 2:51am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/4 "2020-08-31T02:51:33Z")

</div>

If you know your start and end IDs you can try using [shortest()](https://dgraph.io/docs/query-language/kshortest-path-quries/)

---

<div class="post-metadata">

**Author:** ![Soultrans](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/soultrans/32/4919_2.png) [@Soultrans](https://discuss.dgraph.io/u/Soultrans)\
**Post date:** [August 31, 2020, 3:25am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/5 "2020-08-31T03:25:41Z")

</div>

But I don’t know that 😂  
Now, what I’m going to get is the relationship between the incoming word information

---

<div class="post-metadata">

**Author:** ![chewxy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/chewxy/32/4339_2.png) [@chewxy](https://discuss.dgraph.io/u/chewxy)\
**Post date:** [August 31, 2020, 4:50am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/6 "2020-08-31T04:50:24Z")

</div>

I just realized that the path is quite arbitrary, so `shortest` won’t work. What is the blue dots? Are they of different types?

---

<div class="post-metadata">

**Author:** ![Soultrans](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/soultrans/32/4919_2.png) [@Soultrans](https://discuss.dgraph.io/u/Soultrans)\
**Post date:** [August 31, 2020, 5:45am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/7 "2020-08-31T05:45:22Z")

</div>

I don’t know how to explain it, it looks like the node of the query, I’m not sure.  
For example:

```auto
 {
     all(func: eq(department, “principal”,“sdirector”,“aleader”,“teacherA”,“student”)) @recurse(depth: 3) {
     department
     subordinate_departments
     }
}

```

The predicates of my query are blue in the query result, “principal” is node1

 ![1](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/1/122c845f813356da5c9047376380af83294c315e.png)

---

<div class="post-metadata">

**Author:** ![chewxy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/chewxy/32/4339_2.png) [@chewxy](https://discuss.dgraph.io/u/chewxy)\
**Post date:** [August 31, 2020, 6:46am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/8 "2020-08-31T06:46:02Z")

</div>

Looks like you want a Connected Components thing.

perhaps you can add this:

```auto
 {
     all(func: eq(department, “principal”,“sdirector”,“aleader”,“teacherA”,“student”)) @recurse(depth: 3) {
     department
     subordinate_departments @filter(has(subordinate_departments))
     }
}

```

this is a variant of @rajas’ answer above

---

<div class="post-metadata">

**Author:** ![Soultrans](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/soultrans/32/4919_2.png) [@Soultrans](https://discuss.dgraph.io/u/Soultrans)\
**Post date:** [August 31, 2020, 7:00am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/9 "2020-08-31T07:00:57Z")

</div>

This is a little off from what I expected 😂  
All I want is the information that contains the blue nodes.They are part of the whole.  
Like this,I just want the red part

 ![1](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/4/476f66ce6a6b72bda00db2fbc91855776f641022.png)

---

<div class="post-metadata">

**Author:** ![chewxy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/chewxy/32/4339_2.png) [@chewxy](https://discuss.dgraph.io/u/chewxy)\
**Post date:** [August 31, 2020, 10:35am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/10 "2020-08-31T10:35:14Z")

</div>

Sorry, I meant:

```auto
 {
     all(func: eq(department, “principal”,“sdirector”,“aleader”,“teacherA”,“student”)) @recurse(depth: 3) {
     department
     subordinate_departments {
        department @filter(has(subordinate_departments))
     }
 }
}

```

can you also share what the result would be like?

---

<div class="post-metadata">

**Author:** ![Soultrans](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/soultrans/32/4919_2.png) [@Soultrans](https://discuss.dgraph.io/u/Soultrans)\
**Post date:** [August 31, 2020, 11:07am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/11 "2020-08-31T11:07:32Z")

</div>

That doesn’t seem right.  
 ![1](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/7/71a8586cc7542a4c4a4c0e4f332fbc050d61c605.png)

---

<div class="post-metadata">

**Author:** ![chewxy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/chewxy/32/4339_2.png) [@chewxy](https://discuss.dgraph.io/u/chewxy)\
**Post date:** [August 31, 2020, 11:08am UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/12 "2020-08-31T11:08:14Z")

</div>

Ah, then my DQL-fu ain’t strong enough

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [August 31, 2020, 4:23pm UTC](https://discuss.dgraph.io/t/sub-graph-query/9895/13 "2020-08-31T16:23:53Z")

</div>

When using recurse, you don’t have to specify the nested ones. Just any existing edge/pred.

So the query looks like this:

```auto
 {
     all(func: eq(department, “principal”,“sdirector”,“aleader”,“teacherA”,“student”)) @recurse(depth: 3) {
     department
     subordinate_departments 
     department @filter(has(subordinate_departments))
 }
}

```

Also, to make this query even more performative I would do like:

```auto
 {
     A as var(func: eq(department, “principal”))
     B as var(func: eq(department, “sdirector”))
     C as var(func: eq(department, “aleader”))
     D as var(func: eq(department, “teacherA”))
     E as var(func: eq(department, “student”))

     all(func: uid(A, B, C, D, E)) @recurse(depth: 3) {
        department
        subordinate_departments 
        department @filter(has(subordinate_departments))
    }
}

```
