GitHubã®ã»ãšãã©ã®ãªããžã§ã¯ãïŒãŠãŒã¶ãIssueããã«ãªã¯ãšã¹ããªã©ïŒã«ã¯ãREST APIã䜿ã£ãŠããGraphQL APIã䜿ã£ãŠãã¢ã¯ã»ã¹ã§ããŸãã REST API å ããå€ãã®ãªããžã§ã¯ãã®ã°ããŒãã« ããŒã ID ãæ€çŽ¢ããGraphQL æäœã§ãããã® ID ã䜿ããŸãã 詳现ã«ã€ããŠã¯ããREST API ãªãœãŒã¹ã§ã® GraphQL API ããŒã ID ã®ãã¬ãã¥ãŒããåç §ããŠãã ããã
ã¡ã¢
REST ã§ã¯ãã°ããŒãã« ããŒã ID ãã£ãŒã«ãã« node_id
ãšããååãä»ããããŸãã GraphQL ã§ã¯ããã㯠node
ã€ã³ã¿ãŒãã§ã€ã¹äžã® id
ãã£ãŒã«ãã«ãªããŸãã GraphQL ã§ã® "ããŒã" ã®æå³ãããäžåºŠç¢ºèªããã«ã¯ããGraphQLã®ç޹ä»ããåç
§ããŠãã ããã
ã°ããŒãã«ããŒãIDãå©çšãã
ã°ããŒãã«ããŒãIDãå¹ççã«å©çšããã«ã¯ã以äžã®3ã€ã®ã¹ããããèžãã§ãã ããã
- ãªããžã§ã¯ãã®
node_id
ãè¿ã REST ãšã³ããã€ã³ããåŒã³åºããŸãã - GraphQLã§ã®ãã®ãªããžã§ã¯ãã®åãèŠã€ããŸãã
- ãã®IDãšåã䜿ããGraphQLã§ãã€ã¬ã¯ãã«ããŒãã®ã«ãã¯ã¢ãããè¡ããŸãã
äŸãèŠãŠãããŸãããã
1. ãªããžã§ã¯ãã®ããŒã ID ãè¿ã REST ãšã³ããã€ã³ããåŒã³åºã
èªèšŒããããŠãŒã¶ãŒãèŠæ±ããå Žå:
curl -i --header "Authorization: Bearer YOUR-TOKEN" https://api.github.com/user
èªèšŒããããŠãŒã¶ãŒã® node_id
ãå«ãã¬ã¹ãã³ã¹ãè¿ãããŸãã
{
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false,
"name": "monalisa octocat",
"company": "GitHub",
"blog": "https://github.com/blog",
"location": "San Francisco",
"email": "octocat@github.com",
"hireable": false,
"bio": "There once was...",
"public_repos": 2,
"public_gists": 1,
"followers": 20,
"following": 0,
"created_at": "2008-01-14T04:33:35Z",
"updated_at": "2008-01-14T04:33:35Z",
"private_gists": 81,
"total_private_repos": 100,
"owned_private_repos": 100,
"disk_usage": 10000,
"collaborators": 8,
"two_factor_authentication": true,
"plan": {
"name": "Medium",
"space": 400,
"private_repos": 20,
"collaborators": 0
},
"node_id": "MDQ6VXNlcjU4MzIzMQ=="
}
2. GraphQL ã§ã®ãªããžã§ã¯ãã®çš®é¡ãèŠã€ãã
ãã®äŸã§ã¯ãnode_id
å€ã¯ MDQ6VXNlcjU4MzIzMQ==
ã§ãã ãã®å€ã䜿ã£ãŠãåããªããžã§ã¯ããGraphQLã§ã¯ãšãªã§ããŸãã
ããããæåã«ãªããžã§ã¯ãã® çš®é¡ ãææ¡ããå¿ èŠããããŸãã ã·ã³ãã«ãªGraphQLã¯ãšãªã§ããã®åã調ã¹ãããšãã§ããŸãã
query {
node(id:"MDQ6VXNlcjU4MzIzMQ==") {
__typename
}
}
ãã®ã¯ãšãªã®çš®é¡âããŒãã ID ã§èŠã€ããâã¯ã"ãã€ã¬ã¯ã ããŒã ã«ãã¯ã¢ãã" ãšåŒã°ããŠããŸãã
ãã®ã¯ãšãªãå®è¡ãããšã__typename
㯠User
ã«ãªããŸãã
3. GraphQL ã§ãã€ã¬ã¯ã ããŒã ã«ãã¯ã¢ãããè¡ã
çš®é¡ã確èªããããã€ã³ã©ã€ã³ ãã©ã°ã¡ã³ãã䜿ã£ãŠããã® ID ã«ãã£ãŠãªããžã§ã¯ãã«ã¢ã¯ã»ã¹ãã远å ã®ããŒã¿ãè¿ãããšãã§ããŸãã ãã®äŸã§ã¯ããã¡ãã§ã¯ãšãªãå®è¡ãã User
ã®ãã£ãŒã«ããå®çŸ©ããŠããŸãã
query {
node(id:"MDQ6VXNlcjU4MzIzMQ==") {
... on User {
name
login
}
}
}
ãã®çš®ã®ã¯ãšãªã¯ããªããžã§ã¯ããã°ããŒãã«ããŒãIDã§ã«ãã¯ã¢ããããæšæºçãªã¢ãããŒãã§ãã
ç§»è¡ã«ãããã°ããŒãã«ããŒãIDã®å©çš
REST API ãŸã㯠GraphQL API ã䜿çšããã€ã³ãã°ã¬ãŒã·ã§ã³ãæ§ç¯ããå ŽåãAPI ããŒãžã§ã³éã«ããã£ãŠãªããžã§ã¯ããç°¡åã«åç §ã§ããããã«ãã°ããŒãã«ããŒã ID ãä¿æãããšè¯ãã§ãããã REST ãš GraphQL ã®éã®ç§»è¡åŠçã®è©³çްã«ã€ããŠã¯ããRESTããGraphQLãžã®ç§»è¡ããåç §ããŠãã ããã