-
Notifications
You must be signed in to change notification settings - Fork 2
[React] GraphQL, axios
Kim Hyewon edited this page Jun 14, 2022
·
4 revisions
- ์์ฒญํ๋ data๋ฅผ ์ ์ธ์ ์ผ๋ก ๊ธฐ์ ํ๋ ์ฟผ๋ฆฌ. POST๋ก ์์ฒญํ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ๋ด์ ํ ์๋ต์ผ๋ก ๋๋ ค์ค.
- request์ body๋ถ๋ถ์ ์ด๋ค ์์ฒญ์ ํ๋๊ฐ์ ๋ฐ๋ผ ์์ , ์ญ์ ์์ฒญ๋ ๊ฐ๋ฅ ๋ชจ๋ POST๋ก ์์ฒญํจ.
์ฌ๋ฌ depth์ ์ ๋ณด๋ฅผ ํ๋์ request๋ก ์์ฒญ ๊ฐ๋ฅ
query findRepos($login: String!) {
user(login: $login) { //๋ณ์ $login์ ์์ฒญํ ๋ ๋ด์์ ๋ณด๋
id
login
name
location
avatar_url: avatarUrl //๊ฐ์ ธ์ค๋ data alias๋ ๊ฐ๋ฅ
repositories(first: 100) {
totalCount
nodes {
name
}
}
}
}
GraphQL์ query๋ก ์์ฒญํ ์ ๋ณด๋ง ๋ฐํํ๋ค.
{
"data": {
"user": {
"id": "MDQ6VXNlcjc5NTkyNjA0",
"login": "hyew-kim",
"name": "Kim Hyewon",
"location": null,
"avatar_url": "https://avatars.githubusercontent.com/u/79592604?u=9cf807612ce5121bb2018cb4a85b3ff599357128&v=4",
"repositories": {
"totalCount": 33,
"nodes": [
{
"name": "prgrms-fejs"
},
{
"name": "42_piscine"
},
{
"name": "printf"
}, ...
]}
}
}
}
- Promise ๊ธฐ๋ฐ์ HTTP ๋น๋๊ธฐ ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ, HTTP method๋ชจ๋ ์ง์
- ์ธ์คํด์ค ์ปค์คํ ์ค์ ๊ฐ๋ฅ
- ์์ฒญ ์ทจ์ ๋ฐ ํ์์์ ์ค์ ๊ฐ๋ฅ
body๊ฐ ์๋ data์์ฑ ์ฌ์ฉ
- axios.get(url,[,config])
- axios.post(url[,data[,config]])
- axios.delete(URL,[,config])
- axios.put(url[, data[, config]])
์๋์ผ๋ก JSONํ์์ผ๋ก ๋ณํ๋์ด response๊ฐ ์จ๋ค. status๊ฐ 200์ด๊ณ statusText๊ฐ โOKโ์ด๋ฉด ์ฑ๊ณต์ด๋ค.
{
// `data`๋ ์๋ฒ๊ฐ ์ ๊ณตํ ์๋ต(๋ฐ์ดํฐ)์
๋๋ค.
data: {},
// `status`๋ ์๋ฒ ์๋ต์ HTTP ์ํ ์ฝ๋์
๋๋ค.
status: 200,
// `statusText`๋ ์๋ฒ ์๋ต์ผ๋ก ๋ถํฐ์ HTTP ์ํ ๋ฉ์์ง์
๋๋ค.
statusText: 'OK',
// `headers` ์๋ฒ๊ฐ ์๋ต ํ ํค๋๋ ๋ชจ๋ ํค๋ ์ด๋ฆ์ด ์๋ฌธ์๋ก ์ ๊ณต๋ฉ๋๋ค.
headers: {},
// `config`๋ ์์ฒญ์ ๋ํด `axios`์ ์ค์ ๋ ๊ตฌ์ฑ(config)์
๋๋ค.
config: {},
// `request`๋ ์๋ต์ ์์ฑํ ์์ฒญ์
๋๋ค.
// ๋ธ๋ผ์ฐ์ : XMLHttpRequest ์ธ์คํด์ค
// Node.js: ClientRequest ์ธ์คํด์ค(๋ฆฌ๋๋ ์
)
request: {}
}
- https://yamoo9.github.io/axios/guide/response-schema.html
- https://www.youtube.com/watch?v=oLQeFkhRqzU
- https://inpa.tistory.com/entry/AXIOS-๐-์ค์น-์ฌ์ฉ#axios-vs-fetch [๐จโ๐ป Dev Scroll:ํฐ์คํ ๋ฆฌ]
- https://axios-http.com/kr/docs/interceptors
- https://www.npmjs.com/package/graphql-request#usage