REST API ๋๋ GraphQL API๋ฅผ ์ฌ์ฉํ์ฌ GitHub์ ๋๋ถ๋ถ์ ๊ฐ์ฒด(์ฌ์ฉ์, ์ด์, ๋์ด์ค๊ธฐ ์์ฒญ ๋ฑ)์ ์ก์ธ์คํ ์ ์์ต๋๋ค. REST API ๋ด์์ ๋ง์ ๊ฐ์ฒด์ ์ ์ญ ๋ ธ๋ ID๋ฅผ ์ฐพ๊ณ GraphQL ์์ ์์ ์ด๋ฌํ ID๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ REST API ๋ฆฌ์์ค์์ GraphQL API ๋ ธ๋ ID ๋ฏธ๋ฆฌ ๋ณด๊ธฐ๋ฅผ ์ฐธ์กฐํ์ธ์.
์ฐธ๊ณ ํญ๋ชฉ
REST์์ ์ ์ญ ๋
ธ๋ ID ํ๋์ ์ด๋ฆ์ node_id
์
๋๋ค. GraphQL์์๋ node
์ธํฐํ์ด์ค์ id
ํ๋์
๋๋ค. GraphQL์์ โ๋
ธ๋โ์ ์๋ฏธ์ ๋ํ ์๋ก์ด ์ค๋ช
์ GraphQL ์๊ฐ์(๋ฅผ) ์ฐธ์กฐํ์ธ์.
์ฌ์ฉํ ์ ์ญ ๋ ธ๋ ID ๋ฐฐ์น
์ ์ญ ๋ ธ๋ ID๋ฅผ ํจ๊ณผ์ ์ผ๋ก ์ฌ์ฉํ๋ ค๋ฉด ๋ค์ ์ธ ๋จ๊ณ๋ฅผ ์ํํ ์ ์์ต๋๋ค.
- ๊ฐ์ฒด์
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๋ก ๋ง์ด๊ทธ๋ ์ด์ ์(๋ฅผ) ์ฐธ์กฐํ์ธ์.