Reputation: 197
Trying to find documentation or figure out how to replicate GitHub's v3 API Get all users endpoint via there v4 graphql API.
It's easy enough to query just about anything for a specific user but how can I retrieve a payload listing all users similarly to the v3 API payload?
Can someone point me to the correct documentation or even better provide an example that returns a list of users?
Upvotes: 3
Views: 4425
Reputation: 838
As far as I can tell there is no equivalent to "Get all users" in the v4 api, however, there is a hacky way that you can get close to it using a nodes query.
First you need to be able to generate node ID's to iterate over. Looking at mojombo
(the first user by database ID) you can see how node ID's are derived.
$ curl https://api.github.com/users/mojombo
{
"login": "mojombo",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
...
}
$ echo -n "MDQ6VXNlcjE=" | base64 -d
04:User1
It is the string 04:User
followed by the users id
(database ID).
Knowing this we can now generate a nodes query for 100 users at a time. NOTE: Not all database ID's are users, some are organisations or deleted users, and as such you will get a lot of NOT_FOUND
errors.
query($ids:[ID!]!) {
nodes(ids:$ids) {
... on User {
login
}
}
}
variables {
"ids": ["MDQ6VXNlcjE=", "MDQ6VXNlcjI=", ...]
}
If you aren't limited to only using the v4 api but still want to take advantage of improved performance of the v4 api then there is a slightly less hacky alternative, which is to use the v3 api to get users 100 at a time then use the returned node_id
field to perform a bulk v4 query across all 100 users using the above technique. Using this method gives far fewer NOT_FOUND
errors and hence better utilises your rate limit.
To give an idea of the performance improvement you can get using the v4 api and this technique, the task I was performing went from taking an estimated ~474 days using the v3 api to less than 5 days to using this method.
Upvotes: 8
Reputation: 141
The search query works well for this. You can query based on specific fields, and it will return a list of users. This is usually good if you are looking for one specific user. If you are looking for a large list of users, the search api isn't what you want, since it is optimized for finding one value based on inputs. See an example below:
{
search(query: "location:toronto language:Go", type: USER, first: 10) {
userCount
edges {
node {
... on User {
login
name
location
email
company
}
}
}
}
}
Upvotes: 3