Apollo Client

https://www.apollographql.com/docs/react/

GraphQL ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ์ปฌ๊ณผ ์›๊ฒฉ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

๋ฐ์ดํ„ฐ๋ฅผ ํŽ˜์น˜, ์บ์‹œ, ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์‹œ ์ž๋™์œผ๋กœ UI ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.

ํ•ต์‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ@apollo/client์— React๊ฐ€ ์ ‘๋ชฉ๋˜์–ด ์žˆ๋‹ค.

๊ธฐ๋ณธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

ref. https://www.apollographql.com/docs/react/get-started/

1. ์„ค์น˜

npm install @apollo/client graphql

  • @apollo/client: Apollo Client ์„ธํŒ…์‹œ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋กœ, in-memory ์บ์‹œ, ๋กœ์ปฌ ์ƒํƒœ ๊ด€๋ฆฌ, ์—๋Ÿฌ ์ฒ˜๋ฆฌ, ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํฌํ•จํ•œ๋‹ค.

  • graphql: GraphQL ์ฟผ๋ฆฌ ํŒŒ์‹ฑ์„ ๋‹ด๋‹นํ•˜๋Š” ํŒจํ‚ค์ง€

2. ApolloClient ์ดˆ๊ธฐํ™”

2-1.ํ•„์š”ํ•œ ๋ชจ๋“ˆ ์ถ”๊ฐ€

import {
  ApolloClient,
  InMemoryCache,
  ApolloProvider,
  useQuery,
  gql
} from "@apollo/client";

2-2. ApolloClient ์ดˆ๊ธฐํ™”

uri, cache ์†์„ฑ์„ ๊ฐ€์ง„ ์„ค์ • ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

const client = new ApolloClient({
  uri: 'https://48p1r2roz4.sse.codesandbox.io',
  cache: new InMemoryCache()
});
  • uri GraphQL ์„œ๋ฒ„ URL

  • cache InMemoryCache ์ธ์Šคํ„ด์Šค, Apollo Client๊ฐ€ ์ฟผ๋ฆฌ ํŽ˜์น˜ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ์บ์‹ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

์ฐธ๊ณ : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ ์˜ˆ์‹œ

client
  .query({
    query: gql`
      query GetRates {
        rates(currency: "USD") {
          currency
        }
      }
    `
  })
  // ์‘๋‹ต์— data ์†์„ฑ ์™ธ, loading ์™€ neworkStatus ์†์„ฑ ํ™•์ธ ๊ฐ€๋Šฅ
  .then(result => console.log(result));

3. ๋ฆฌ์•กํŠธ ์—ฐ๋™ํ•˜๊ธฐ

์ฟผ๋ฆฌ๋ฅผ ์—ฐ๋™ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ž๋™์œผ๋กœ UI๋ฅผ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

ApolloProvider(๋ฆฌ์•กํŠธ Context.Provider ์™€ ์œ ์‚ฌ) ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฆฌ์•กํŠธ app๋ฅผ ๊ฐ์‹ธ Apollo Client Context ์•ˆ์— ๋‘”๋‹ค.

GraphQL data ๊ฐ€ ํ•„์š”ํ•œ ์ปดํฌ๋„ŒํŠธ ์ตœ ์ƒ์œ„์— ๋‘์–ด ์–ด๋””์„œ๋“  ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

import React from 'react';
import { render } from 'react-dom';
import {
  ApolloClient,
  InMemoryCache,
  ApolloProvider,
  useQuery,
  gql
} from "@apollo/client";

const client = new ApolloClient({
  uri: 'https://48p1r2roz4.sse.codesandbox.io',
  cache: new InMemoryCache()
});

function App() {
  return (
    <div>
      <h2>My first Apollo app ๐Ÿš€</h2>
    </div>
  );
}

render(
  <ApolloProvider client={client}>    <App />  </ApolloProvider>,  document.getElementById('root'),
);

4. useQuery ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

useQuery: UI ์™€ GraphQL ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฆฌ์•กํŠธ ํ›…

// query ์ •
const EXCHANGE_RATES = gql`
  query GetExchangeRates {
    rates(currency: "USD") {
      currency
      rate
    }
  }
`;
// useQuery ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
// ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋  ๋•Œ๋งˆ๋‹ค useQuery๊ฐ€ ์‹คํ–‰๋˜๊ณ  ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ UI๊ฐ€ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ๋œ๋‹ค.
// ์‘๋‹ต ๊ฐ์ฒด๋Š” loading, error ์ƒํƒœ์™€ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ๊ฐ’์ธ data ์†์„ฑ์„ ๊ฐ€์ง„๋‹ค.
function ExchangeRates() {
  const { loading, error, data } = useQuery(EXCHANGE_RATES);
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error :(</p>;

  return data.rates.map(({ currency, rate }) => (
    <div key={currency}>
      <p>
        {currency}: {rate}
      </p>
    </div>
  ));
}

Last updated