1. GraphQL
React Query์ fetching ๋ฉ์ปค๋์ฆ์ Promise์ ๋ํด ๋ถ๊ฐ์ง๋ก ์ (agnostically)์ผ๋ก ๊ตฌ์ถ๋์ด ์๊ธฐ ๋๋ฌธ์, GraphQL์ ํฌํจํ ์ด๋ค ๋น๋๊ธฐ ๋ฐ์ดํฐ fetching์ด๋ ์์ ํ(literally, ๋ง ๊ทธ๋๋ก) ์ฌ์ฉ๊ฐ๋ฅํ๋ค!
cf. ๋ถ๊ฐ์ง๋ก
- ์๋ ๋ถ, ์ณ์ ๊ฐ, ์ ์ง, ๋ ผํ ๋ ผ(๋ก )
- ์ผ๋ฐ์ ์ผ๋ก ์ด๋ค ๋ช ์ ์ ๋ํด ์ ์ ์๋ค๊ณ ์ฃผ์ฅํ๋ ์ ์ฅ์ ์๋ฏธ
- ์ฌ๊ธฐ์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ(fetching)๋ฅผ ๊ฐ์ ธ์ฌ์ง ๋ชปํ ์ง์ ๋น๋๊ธฐ์ ์๋ฏธ๋ก ํด์ํ๋ฉด ๋จ
React Query๋ ํ์คํ๋ ์บ์ฑ์ ์ง์ํ์ง ์์์ ๋ช ์ฌํด๋ผ. ๋๋ค์ ์ฌ์ฉ์๋ ์ค์ ๋ก ์ ๊ทํ๋ ์บ์๋, ๊ทธ๋ค์ด ์๊ฐํ ๋งํผ ์บ์์ ์ฅ์ ์ด ํ์์๋ค. ๋งค์ด ๋๋ฌธ ์ํฉ๋ฐ์ํ ์ ์์ผ๋ฏ๋ก, ๋จผ์ react-queryํ์๊ฒ ํ์ธํ์ฌ ํ์ํ ์ ํ์ธ์ง ํ์ธํด๋ผ!
2. ํ์ ์์ ์ฑ๊ณผ ์ฝ๋ ์์ฑ(Generation)
React Query๋ (,graphql-request^5 ๋ฐ graphQL Code Generator๊ฐ ์กฐํฉ๋) ๋ชจ๋ ํ์ ์ GraphQL ๋์์ ์ ๊ณตํ๋ค:
1import request from 'graphql-request'2import { useQuery } from '@tanstack/react-query'34import { graphql } from './gql/gql'56const allFilmsWithVariablesQueryDocument = graphql(/* GraphQL */ `7query allFilmsWithVariablesQuery($first: Int!) {8allFilms(first: $first) {9edges {10node {11id12title13}14}15}16}17`)1819function App() {20// `data` is fully typed!21const { data } = useQuery({22queryKey: ['films'],23queryFn: async () =>24request(25'https://swapi-graphql.netlify.app/.netlify/functions/index',26allFilmsWithVariablesQueryDocument,27// variables are type-checked too!28{ first: 10 },29),30})31// ...32}
- ๋ฆฌํฌ์งํ ๋ฆฌ์์ ์ ์ฒด ์์ ๋ฅผ ๋ณผ ์ ์๋ค.
- GraphQL Code Generator ๋ฌธ์