Using variables with @filter and @cascade

I’m struggling to use variables with cascade effectively. It seems like @cascade is not applied to variables used in child queries. I would like to see what is the right way to approach this type of problem.

Say I have following schema types

type User {
   user.groups: [Group]
}

type Group {
   group.members: [User]
   group.items: [InventoryItem]
}

type InventoryItem {
  ...
}

Ultimately, for a User A I would like to @filter all his groups which contain User B inside group.members to return group.items for every such group.

My (failed) attempt is doing it with a variable:

var(func: uid(user_A)) @cascade {
   user.groups {
      uids as uid 
      group.members @filter(uid(user_B)) {
         uid
      }
}

items(func: uid(uids)) {
   group.items {
      uid
      ..
   }
}

The problem is that variable uids contains items which do not satisfy @filter too, so variables were selected before the filter happened. I intuitively though using @cascade would prevent this.

Another solution I came up with works, but it uses two variables instead and hence feels bit inefficient:

var(func: uid(user_A)) {
   user.groups {
      uidA as uid
   }
}

var(func: uid(user_B)) {
   ~group.members {
      uidB as uid
   }
}

items(func: uid(uidA)) @filter(uid(uidB)) {
   group.items {
      uid
      ..
   }
}

What would be Dgraph like approach to such problem?

1 Like

Hey @AugustDev,

Your query works, but it can be done with just one variable, like so:

{
  foo(func: uid("0x10")) { # uid of User A
    user.groups @filter(uid(bGroups)) {
      uid
      group.items
    }
  }
  
  var(func: uid("0x13")) { # uid of User B
    ~group.members {
      bGroups as uid
    }
  }
}

Hope this answers your question!

2 Likes