LoginSignup
2
1

More than 5 years have passed since last update.

GithubのGraphQL APIでリポジトリの作成日付を範囲指定してスター数を取得する

Last updated at Posted at 2019-03-31

はじめに

Github APIを使ってWebアプリ作成中ですが、REST APIよりGraphQL APIの方が良さそうだと思って作り始めて躓いたので備忘録を兼ねてメモします。

やりたい事

各言語別にリポジトリのスター数が多い順に表示する。また、その際に作成日付で範囲指定する。例えば、2019年1月1日以降に作成されたレポジトリの中でスター数が多いものを取得という感じです。

githubの検索画面でやると下記の入力で表示できるものです。
github search.jpg

結論

github v4 explorerの実行結果です。

{
  search(query: "language:python created:>=2019-01-01", type: REPOSITORY, first: 3) {
    edges {
      node {
        ... on Repository {
          nameWithOwner
          createdAt
          stargazers{
            totalCount
          }
        }
      }
    }
  }
}

上記クエリーでやると、下記の結果となりました。

{
  "data": {
    "search": {
      "edges": [
        {
          "node": {
            "nameWithOwner": "CriseLYJ/awesome-python-login-model",
            "createdAt": "2019-01-20T10:43:13Z",
            "stargazers": {
              "totalCount": 5948
            }
          }
        },
        {
          "node": {
            "nameWithOwner": "NVlabs/stylegan",
            "createdAt": "2019-02-04T15:33:58Z",
            "stargazers": {
              "totalCount": 5753
            }
          }
        },
        {
          "node": {
            "nameWithOwner": "openai/gpt-2",
            "createdAt": "2019-02-11T04:21:59Z",
            "stargazers": {
              "totalCount": 4859
            }
          }
        }
      ]
    }
  }
}

下記記載のものが2019年以降に作られたpythonリポジトリの中でスター数が多い上位3つです。(2019年4月1日現在)

  1. CriseLYJ/awesome-python-login-model
  2. NVlabs/stylegan
  3. openai/gpt-2

「created:>=2019-01-01」をどこに入れれば良いか分からずハマりましたが、普通に「query:」の後に書けば良かったのですね。スター数はstargazersのtotalCountで取得できます。

もちろん作成日付を指定しなければ、全リポジトリの中でスター数順に取得できます。

{
  "data": {
    "search": {
      "edges": [
        {
          "node": {
            "nameWithOwner": "vinta/awesome-python",
            "createdAt": "2014-06-27T21:00:06Z",
            "stargazers": {
              "totalCount": 65023
            }
          }
        },
        {
          "node": {
            "nameWithOwner": "donnemartin/system-design-primer",
            "createdAt": "2017-02-26T16:15:28Z",
            "stargazers": {
              "totalCount": 60574
            }
          }
        },
        {
          "node": {
            "nameWithOwner": "toddmotto/public-apis",
            "createdAt": "2016-03-20T23:49:42Z",
            "stargazers": {
              "totalCount": 54435
            }
          }
        }
      ]
    }
  }
}
  1. vinta/awesome-python
  2. donnemartin/system-design-primer
  3. toddmotto/public-apis

languageをgolangに変えて2019年以降に作られたリポジトリを取得してみます。

{
  "data": {
    "search": {
      "edges": [
        {
          "node": {
            "nameWithOwner": "eranyanay/1m-go-websockets",
            "createdAt": "2019-02-10T13:41:43Z",
            "stargazers": {
              "totalCount": 3361
            }
          }
        },
        {
          "node": {
            "nameWithOwner": "vlang-io/V",
            "createdAt": "2019-02-08T02:57:06Z",
            "stargazers": {
              "totalCount": 1768
            }
          }
        },
        {
          "node": {
            "nameWithOwner": "alash3al/sqler",
            "createdAt": "2019-01-07T21:38:36Z",
            "stargazers": {
              "totalCount": 1743
            }
          }
        }
      ]
    }
  }
}
  1. eranyanay/1m-go-websockets
  2. vlang-io/V
  3. alash3al/sqler

うまく取得できているようですね。GithubのGraphQL APIの日付の範囲指定でググってもあまり出て来なかったのですが、こちらのstackoverflowをヒントに解決できました。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1