5

I am trying to list public repositories in Github using GraphQL as it allows me to choose exactly which information from an Object I want.

Using REST I could list public repositories simply by making requests to https://api.github.com/repositories. This is OK, but the response comes with a bunch of stuff I don't need. So, I was wondering if I could use GraphQL to do the same job.

The problem is, I couldn't find any high level repositories Object I could use to list public repositories using GraphQL. For me it seems I can only use GraphQL to list repositories from organizations or from users. For example, like doing so:

query{
    user(login: "someuser"){
        repositories(first: 50){
            nodes{
                name
            }
            pageInfo{
                hasNextPage
            }
        }
    }
}

So, how can I use (if at all) Github's GraphQL endpoint to list Github's public repositories?

I have also tried something on this line, using search, but I doubt Github has only 54260 repositories as the repositoryCount variable returned me.

query{
    search(query:"name:*", type:REPOSITORY, first:50){
        repositoryCount
        pageInfo{
            endCursor
            startCursor
        }
        edges{
            node{
                ... on Repository{
                    name
                }
            }
        }
    }
}
FTM
  • 1,887
  • 17
  • 34
  • 2
    Learned so much in the last few days about GraphQL that I summarized my endeavor with a post on Medium: https://medium.com/@fabiomolinar/using-githubs-graphql-to-retrieve-a-list-of-repositories-their-commits-and-some-other-stuff-ccbbb4e96d78 – FTM Jan 16 '18 at 20:27

2 Answers2

13

You can use is:public in the search query :

{
  search(query: "is:public", type: REPOSITORY, first: 50) {
    repositoryCount
    pageInfo {
      endCursor
      startCursor
    }
    edges {
      node {
        ... on Repository {
          name
        }
      }
    }
  }
}

Try it in the explorer

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • One more question, would it be possible to order the results by date it was created? – FTM Jan 14 '18 at 09:34
  • @FTM you can't sort by creation date in search query, check [this post](https://stackoverflow.com/a/40764448/2614364). You can only sort by creation date when requesting user repositories in the `repositories` connection, check [this post](https://stackoverflow.com/a/47255971/2614364) – Bertrand Martel Jan 14 '18 at 14:16
  • thank you once again. Final question, where can I find more information about what can I use to form my search query? I tried Github's documentation about its Repository object, but it says nothing about things like "is:public" or any other arguments I could use on the search query. – FTM Jan 14 '18 at 19:54
  • You can find it [here](https://help.github.com/articles/searching-repositories/#search-based-on-whether-a-repository-is-private-or-public) for search options & [here](https://help.github.com/articles/sorting-search-results/) for sorting – Bertrand Martel Jan 14 '18 at 22:20
  • 1
    Bertrand, if you were my neighbor I would definitely pay some beers for you! Thanks again! – FTM Jan 15 '18 at 14:18
0

There is also an inPrivate attribute on the Repository object you can query https://docs.github.com/en/graphql/reference/objects#repository

dancl
  • 689
  • 5
  • 13