Query based type node and filter its nested value

Hi i have the following json

json { "uid": "_:business1", "dgraph.type": "Business", "name": "Test Business", "hasProducts": [ { "uid": "_:product1", "dgraph.type": "Product", "name": "Product 1", "size": "size 1", "isSoldBy": {"uid": "_:business1"}, "decription": "This is product desc" } ] }

Now if i want to query Products related to a given Business uid how do I do that ?

Hi @harshadbhatia,

You can do something like:

{
  business_products(func: uid(<uid-of-business>)) {
    hasProducts {
       name
    }
  }
}
1 Like

Hi, yes how would this perform against if we were to get Product and then filter ?

Getting all products and then filtering would be slower (and less efficient)
So, your query would be something like:

{
  business_products(func: eq(dgraph.type, Product)) @cascade {
    business: ~ hasProducts @filter(...) {
    } 
  }
}

The filtering would happen after all the product ids are fetched and then cascade would remove the irrelevant products. So, this would take one network call to fetch all the product uids, another network call for getting their business (~hasProducts) and then filtering would happen. So 2 network calls followed by filtering would get us the relevant product ids.
On the other hand, the query I shared earlier, a single network call would fetch all the relevant product ids.
If this answers your questions, please mark this resolved.