GitHub GraphQL API で指定した組織の情報を(ここでは github
organization)の情報を取得には、organization クエリ を使用します。
organization
クエリには、login
パラメーターで組織名を渡します。
organization
クエリが返す Organization オブジェクト を参照すると、そこに所属する メンバーの一覧、チームの一覧、リポジトリの一覧 などを取得することができます。
ある組織に所属するユーザー情報を取得する (Organization.membersWithRole)
Organization
オブジェクトの membersWithRole
フィールドは OrganizationMemberConnection オブジェクトを保持しており、この nodes
を参照することで、組織に所属するメンバーの一覧を取得できます。
query {
organization(login: "github") {
name
description
url
membersWithRole(first: 3) {
nodes {
login
name
email
updatedAt
}
}
}
}
{
"data": {
"organization": {
"name": "GitHub",
"description": "How people build software.",
"url": "https://github.com/github",
"membersWithRole": {
"nodes": [
{
"login": "mtodd",
"name": "Matt Todd",
"email": "xxx@example.com",
"updatedAt": "2022-05-28T16:13:01Z"
},
{
"login": "jonmagic",
"name": "Jonathan Hoyt",
"email": "xxx@example.com",
"updatedAt": "2022-06-07T18:27:30Z"
},
{
"login": "mislav",
"name": "Mislav Marohnić",
"email": "xxx@example.com",
"updatedAt": "2022-04-07T18:34:52Z"
}
]
}
}
}
}
ある組織のリポジトリの一覧を取得する (Organization.repositories)
Organization
オブジェクトの repositories
フィールドを参照することで、その組織に作成されたリポジトリの一覧を取得することができます。
query {
organization(login: "github") {
name
description
url
repositories(first: 3) {
nodes {
name
url
}
}
}
}
{
"data": {
"organization": {
"name": "GitHub",
"description": "How people build software.",
"url": "https://github.com/github",
"repositories": {
"nodes": [
{
"name": "media",
"url": "https://github.com/github/media"
},
{
"name": "albino",
"url": "https://github.com/github/albino"
},
{
"name": "hubahuba",
"url": "https://github.com/github/hubahuba"
}
]
}
}
}
}
取得される各リポジトリの情報は、Repository オブジェクト として表現されています。
ここでは、組織内のリポジトリのリストを取得する方法を示しましたが、組織名(ユーザー名)とリポジトリ名があらかじめ分かっている場合は、repository
クエリを使って、直接そのリポジトリの情報を取得することができます。
関連記事
- GitHub GraphQL クエリ例: イシュー情報を取得する (search)
- GitHub GraphQL クエリ例: リポジトリの情報を取得する (repository)
- GitHub の GraphQL API Explorer の使い方
- Node.js で GitHub GraphQL API を使用する (@octokit/graphql)
- GitHub GraphQL のスキーマ情報を取得する
- GitHub GraphQL API の呼び出し回数制限 (rate limit) の情報を取得する
- Apollo Client で GitHub GraphQL API を使う (Node & React)