Graphql query -- how to use a type as filter?

Hi, I use dgraph2.0 with graphql, and I had read about graphql doc
In my pratice, I have a problem of querying that use other type as filter.
For example, I have a schema like this:

type User {
    name: String! @id
    role: Role
}

type Role {
   name: String! @id 
   permission: String!
}

I have some kind of role, like “admin”, “editor”, “visitor”…
Every user have a special role.
So how can I find all user that have a role named “admin”?
I mad a query like this:

query{
    queryUser(filter:{role:{name:{eq:"admin"}}}) {
        name
    }
}

but it returns an error for me: “message”: “Field “role” is not defined by type UserFilter.”

So I use another query like this:

query {
    queryUser() {
        name
        role(filter:{name:{eq:"admin"}}) {
             name
        }
     }
}

But it returns all user for me and some of the use’s role is null, I don’t know how to make query with graphql in this case :frowning: I just want to get the user that have a role named “admin”… not all of the user

Hi, thanks for the question.

At the moment the GraphQL filters don’t have a way of saying something like “I want to start with users, dig into the graph to find some target things (admin, in this case) and then limit the outer result on users by what I found”.

That sort of support will be solved by this issue [GraphQL] Introducing @cascade in the GraphQL API? · Issue #4789 · dgraph-io/dgraph · GitHub.

It won’t be in the 2.0 (now 20.03.0 - see the new version strategy here) release, but will probably land in the betas just after that.

P.S. I can see in your schema how that’s a problem - if Role was linked to all the users that had the role, then you’d be able to go from admin role → users. And that traversal will currently give you what you need.

That could be a work around for now until the new feature is added.

Thanks very much for the reply, got it :slight_smile:

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