Assume I have a GraphQL Schema like this:
Schema {
user($id: ID) {
id: ID
name: String
}
}
I know how to query for one user by his ID, and I could use aliases to query for several users like so:
GetSomeMore {
john: user(id: "123") {
name
}
mary: user(id: "321") {
name
}
}
However, assume I just have a list of user IDs, and would like to query for the names of each one of them. How would I write a query like that?
The approaches I can come up with is ether dynamically generating a query with aliases like user1
, user2
, etc., or implementing a users(ids: [ID])
edge on the server. But I feel like there should be a better solution. Is there?
(PS: my code examples might have syntax errors, I'm writing it more as pseudocode, to get my point across.)
You could implement that functionality as part of your schema, for example with a
id_in: [String]
argument to theusers
query.At Graphcool, we use the
filter
argument to expose functionality like this. For example, your query could be done like this:results in
To read more about the
filter
argument check the documentation.