Filter by relation predicate?

hi,
ive a relation role for user,
how do i get all users with specific role.name?
for example: role.name=‘admin’ or ‘staff’ ?

users(func: eq(_type, "user") {
    uid
    _type
    name
    first_name
    last_name
    roles {
      uid
      name
    }
})

What if i have more than 2 layers or more deep relationship that i wish to filter users ?

Try something like this:

JSON Mutation

{
  "set": [
    {
      "uid": "_:Admin",
      "_type": "Role",
      "name": "Admin"
    },
    {
     "_type": "user",
     "name": "Jack",
     "first_name": "Jack",
     "last_name": "Nicholson",
     "roles": { "uid": "_:Admin" }
    }
  ]
}

Query

{
  getDefaultUidForAdmin(func: eq(_type, "Role")) @filter(eq(name, "Admin")){
  uid
}

  users(func: eq(_type, "user")) @filter(uid_in(roles, "0x5" ))  
    {
    uid
    _type
    name
    first_name
    last_name
    roles {
      uid
      name
    }
}
}

About uid_in https://docs.dgraph.io/master/query-language/#uid-in

Cheers.

2 Likes

is these 2 separate query?

Yes, are two query blocks. The one above is just to know what UID put in uid_in(roles.

https://docs.dgraph.io/query-language/#multiple-query-blocks

means i need to obtain id first before query the second query

No, but you need to know what UID use tho.

is it possible to query without knowing uid but only knowing name?
can we do it using variable?

and is it possible to pass in multiple id to uid_in ?

For uid_in, no and no. But you can try other complex queries tho.

No wih uid_in, but you can use connectives AND, OR and NOT.

you can

roles as var(func: eq(name, "admin", "stuff"))

users(func: eq(_type, "user")) @cascade {
  roles @filter(uid(roles))
  uid
  name
}

but it will work slow

  1. use different names for edges for users and roles, to not index and lookup all name values but only related to role.
  2. don’t use eq for filtering by type, because it uses index lookup. create edge with typeUser name instead and query using has(typeUser) function
  3. don’t get all users and then filter by role. it’s not sql, you should use graph way instead:
result(func: eq(roleName, "stuff", "admin")) {
  ~roles {
    userName
    uid
  }
}

since you know that roles edge is always connecting user and role, you can use reverse edge to retrieve user by role. you can save users in a var and use in another block.

var(func: eq(roleName, "stuff", "admin")) {
  users as ~roles
}

users(func: uid(users)) {
  uid
  userName
}

if roles edge in your schema is used not only for users, you should modify your schema or use @filter(has(typeUser)) on ~roles edge (this option is worse)

result(func: eq(roleName, "stuff", "admin")) {
  ~roles @filter(has(typeUser)) {
    userName
    uid
  }
}

awesome tips man, thank you :slight_smile:

but looking at this query:

{
roles as var(func: eq(_type, "role") @filter(eq(name, "admin", "superadmin")))

users(func: eq(_type, "users")) @cascade {
...
}
}
  

how do i add in more filter for roles variable, like above , but it does not work

i tried this, no error, but didnt work

roles as var(func: eq(_type, "role")) @filter(eq(name, "admin", "superadmin"))

what did you mean when you say “didnt work”?

The correct syntax for multiple args values is

  • eq(predicate, [val1, val2, ..., valN])
    So: @filter(eq(name, ["admin", "superadmin"])))

Check https://docs.dgraph.io/query-language/#inequality

1 Like

ah sorry, my mistake, i got this working:

roles as var(func: eq(_type, "roles")) @filter(eq(name, ["admin","superadmin"]))

users(func: eq(_type, "users")) @cascade {
  with_role @filter(uid(roles))
  uid
  email
}

but i could have done this instead:

users(func: eq(_type, "users")) @cascade {
  uid
  with_role @filter(eq(name, ["admin","superadmin"])) {
    uid
    name
  }
  email
}

is there anyway to support something like this?

{
  roles1 as var(func: eq(_type, "roles")) @filter(eq(name, "superadmin"))
  users(func: eq(_type, "users")) @filter(uid_in(with_role, roles1)) {
    uid
    email
    with_role {
      uid
      name
    }
  }
}

No for now. As Docs says

uid_in cannot be used at root, it accepts one UID constant as its argument (not a variable).

i did this:

{
  roles1 as var(func: eq(_type, "roles")) @filter(eq(name, "superadmin"))
  u1 as var(func: eq(_type, "users")) @cascade {
    uid
    with_role @filter(uid(roles1)){
      uid
      name
    }
  }
 
  roles2 as var(func: eq(_type, "roles")) @filter(eq(name, "admin"))
  u2 as var(func: eq(_type, "users")) @cascade {
    uid
    with_role @filter(uid(roles2)){
      uid
      name
    }
  }
    
  users(func: eq(_type, "users")) @filter(uid(u1, u2)) {
    uid
    email
    with_role {
      uid
      name
    }
  }
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.