Is this query possible in the Github GraphQL API?


#1

Hello. I’ve been asked to build a web solution that lists the owners of public repos on Github. I need to collect a list of 100 most popular project owners on Github by follower count. For each owner I need:

  1. The owners avatar (URL)
  2. list of followers for that owner (or first 5 to keep the query smaller)

I’m new to GraphQL, but it seems like the perfect solution for my needs. I only have 5 days to learn GraphQL and code the solution. Step one is to pick between using the Rest API or the GraphQL API. I’m having a hard time finding in the documentation how to return a list of anything. In the GraphQL documentation all their examples have plural query fields like userS or licenseS that return a list of items, but it appears Github is using nodes to get lists.

Can anyone show me the query to get this information? Or point me to some documentation that explains how to return lists of Github items. Thanks, -=Steve


#2

Hi szaske,

I am also very new to it, but this can get you started.
Following query gives you the most starred repos with owner and owner avatar url:

{
  search(first: 10, type: REPOSITORY, query: "stars:>100000") {
    nodes {
      ... on Repository {
        name
        owner {
          id
          login
          avatarUrl
        }
      }
    }
  }
}

Try running it this query in the explorer here - https://developer.github.com/v4/explorer/

May be you have to do a separate query for followers of users ? OR it may be possible to get the followers for users together with this, I am not sure about that right now.

One hint search query, Make your query here : https://github.com/search (you can use advanced search).
Then in the results page, you will see the search query in the search input box on top. This should be the value for ‘query’ in graphQL call.

HTH.
regards,
Sam


#3

Awesome, thank you so much this will definitely get me going in the right direction. Also thanks for the search link, last night I was thinking just as you were, figuring that a search UI could help me figure out what was possible, but I couldn’t actually find the Github search UI. Funny how Github does NOT make it easy to find.

-=Steve


#4

Hi Steve,

UPDATE:

I spent some time exploring more.
Here is everything you need. It shows last 10 latest created repos after a certain date, sorted by updated date.
And gets the first 5 followers for the owner of that repos.

Note:

  1. Organizations do not have followers in github, so top most starred repos all are by ‘organizations’ where you cannot get the followers (you can get stargazers for repo if you want).
  2. If there are less followers then the reponse will be smaller. (if there are no followers, like fo an organization, then the followers will be balnk).
  3. I added the __typename which would tell you if the owner is a ‘USER’ or ‘ORGANIZATION’ (try changing the query to get different results).
{
  search(first: 10, type: REPOSITORY, query: "created:>2018-06-30 sort:updated") {
    nodes {
      ... on Repository {
        name
        owner {
          id
          login
          avatarUrl
          __typename
          ... on User {
            followers(first: 5) {
              nodes {
                name
                avatarUrl
                login
              }
            }
          }
        }
      }
    }
  }
}

#5

UPDATE 2.

Following query gives you top 10 users with most number of followers. And includes the first 5 followers of such users.
It also includes some counts if you are interested in it.

{
  search(first: 10, type: USER, query: "followers:>10000 sort:followers") {
    userCount
    nodes {
      ... on User {
        name
        avatarUrl
        followers(first: 5) {
          totalCount
          nodes {
            ... on User {
              name
              avatarUrl
            }
          }
        }
      }
    }
  }
}

ps: sometimes the explorer shows errors in response. When that happens, just refresh the page and try again. I can assure that above query fetches the results.

@szaske


#6

What is the query to return all projects that have more than 20 contributors?


#7

:wave: @Moara,

I can’t think of a way to do this in either of our APIs. The list of possible query arguments for the search field in GraphQL is listed here, but none of those give you access to sort by the number of contributors.