graphql-request
- GraphQL์ ์ฌ์ฉํ์ฌ ์๋ฒ์ ํต์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ฐ๋จํ JavaScript ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- GraphQL API๋ฅผ ํธ์ถํ๊ธฐ ์ํด ์ฌ์ฉ
โ ํ์ํ ๊ฒ
- headers - token, Content-Type
- query
- variables
- endpoint ⇒API URI
GraphQLClient
- GraphQL ์๋ฒ์ HTTP ์์ฒญ์ ๋ณด๋ด๊ณ ์๋ต์ ๋ฐ๊ธฐ ์ํด ์ฌ์ฉ
- . GraphQLClient๋ฅผ ์ฌ์ฉํ์ฌ GraphQL API ์๋ํฌ์ธํธ์ HTTP ์์ฒญ์ ๋ณด๋ผ์ ์์
- ์ด ํด๋์ค๋ ์ฌ๋ฌ ๊ฐ์ง ๋ฉ์๋๋ฅผ ์ ๊ณต. ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ๋ฉ์๋๋ request๋ฉ์๋
request
- request๋ฉ์๋๋ GraphQL ์ฟผ๋ฆฌ๋ฅผ ์คํํ๊ณ , ์๋ฒ๋ก๋ถํฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋ ๋ฐ ์ฌ์ฉ
- GraphQL API ์๋ํฌ์ธํธ URL๊ณผ GraphQL ์ฟผ๋ฆฌ๋ฅผ ์ธ์๋ก ๋ฐ์
- ์ด ๋ฉ์๋๋ Promise๋ฅผ ๋ฐํํ๋ฉฐ, ์ด Promise๊ฐ ์ฒ๋ฆฌ๋ ๋ ์๋ฒ๋ก๋ถํฐ์ ์๋ต ๋ฐ์ดํฐ๋ฅผ ๋ฐํ
GraphQLClient๋ฅผ ์ฌ์ฉํ์ฌ ์์ฒญ์ ๋ณด๋ด๊ธฐ
import { GraphQLClient } from 'graphql-request'
const client = new GraphQLClient('<https://api.example.com/graphql>') // endpoint ์ง์
const query = `{
allUsers {
id
name
}
}`
client.request(query).then(data => console.log(data))
- GraphQLClient ์ธ์คํด์ค ์์ฑ
- ์์ฒญํ GraphQL API ์๋ํฌ์ธํธ URL ์ค์
- GraphQLClient์ request ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ฟผ๋ฆฌ ์คํ ๋ฐ ๊ฒฐ๊ณผ ์์
GraphQLClient๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์์ง๋ง request๋ง์ ์ฌ์ฉํด์๋ ๊ฐ๋ฅ!
import { request } from 'graphql-request'
request({
url: endpoint, //์์ฒญํ URL(Endpoint)
document: query, // backend์ ์ ์๋ query๋ฌธ
variables: variables, // query์์ ๋ณํ๋ ๊ฐ - queryvariables
requestHeaders: headers, // header
}).then((data) => console.log(data))
'Development > API' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[API] GraphQL ๊ด๋ จ dependency | Apollo Client, graphql-request (0) | 2023.06.15 |
---|
๋๊ธ