Apollo client ์ •๋ฆฌ

robertoยท2022๋…„ 12์›” 14์ผ
1
post-thumbnail

๐Ÿ’โ€โ™‚๏ธ Apollo Client guild ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๊ณ  ์ •๋ฆฌ ํ•œ๊ธ€์ž…๋‹ˆ๋‹ค * ๋ชจ๋“  ์ด๋ฏธ์ง€,์˜ˆ์ œ ์ถœ์ฒ˜๋Š” ๊ณต์‹๋ฌธ์„œ์ž…๋‹ˆ๋‹ค (์ง€์† ์ˆ˜์ •์ค‘)
:https://www.apollographql.com/docs/react/data/queries


Apollo Client ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด์„  graphQl ์‚ฌ์šฉ๋ฒ•์ด ์„ ์ˆ˜ ๋˜์–ด์•ผ ํ•œ๋‹ค

graphQl ์‚ฌ์šฉ๋ฒ•: https://graphql.org/learn/queries/


1. ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

Apollo client ์‚ฌ์šฉํ•ด ์„œ๋ฒ„๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ๋ด…์‹œ๋‹ค

์‚ฌ์šฉ

apollo client ์˜ API ์ธ useQuery ํ›…์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

ReactQuery , swr ๊ณผ ๋น„์Šทํ•œ ์–‘์ƒ์„ ๋„๋Š”๊ฒŒ ํŠน์ง•์ด๋‹ค


์บ์‹ฑ (์ค‘์š”)

๊ทธ๋กœ ๊ทธ๋Ÿด๊ฒƒ์ด ์บ์‹ฑ๋„ ํ•ด์ค€๋‹ค

Apollo ํด๋ผ์ด์–ธํŠธ์˜ ์บ์‹ฑ

์ด๋Ÿฐ ํ˜•ํƒœ๋กœ ์ €์žฅ๋œ๋‹ค

flat ํ…Œ์ด๋ธ” (cash๋‹ด์€ํ…Œ์ด๋ธ”) ์— ์ค‘์ฒฉ ๋ฐ์ดํ„ฐ๋ฅผ InMemoryCache์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์ „์— ์บ์‹œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ •๊ทœํ™” ํ•ด์•ผ ํ•œ๋‹ค.
์ •๊ทœํ™” ์ €์žฅ๊ณผ์ •์€ ๋งˆ์น˜ ์•„๋ž˜์˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๊ตฌ์„ฑ๊ณผ ๋น„์Šทํ•˜๋‹ค

Apollo ํด๋ผ์ด์–ธํŠธ ์บ์‹œ ๊ตฌ์„ฑ

์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๊ณ 
const cache = new InMemoryCache({
			addTypename:...,
            resultCaching:...,
            typePolicies:{
            	... ,
                ... ,
            }) 
์˜ต์…˜๋“ค์€ ์•„๋ž˜ ์™€ ๊ฐ™๋‹ค

์บ์‹œ ID ์ง€์ •

 typePolicies: {
    AuthRootQuery: { keyFields: [] },
    HospitalRootQuery: { keyFields: [] },

์ •๊ทœํ™”

Apollo Client ์บ์‹œ๊ฐ€ ์ฟผ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •๋“ค์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
1.๊ฐ์ฒด๋ฅผ ํ™•์ธํ•œ๋‹ค. (Identify objects)
2.์บ์‹œ ID๋ฅผ ๋งŒ๋“ ๋‹ค. (Generate cache IDs)
3.๊ฐ์ฒด์˜ ํ•„๋“œ๋ฅผ ๋ ˆํผ๋Ÿฐ์Šค๋กœ ๋Œ€์ฒดํ•œ๋‹ค. (Replace object field with references)
4.์ •๊ทœํ™”๋œ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•œ๋‹ค.

์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ตœ์‹  ํ™•์ธ

๋‘๊ฐ€ ์ง€ ๋ฐฉ์‹ polling ๊ณผ refetching ์„ ์‚ฌ์šฉํ•œ๋‹ค

polling

์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ€๋ฆฌ์„ธ์ปจ๋“œ๋ฅผ ์ค€๋‹ค

์œ„์™€ ๊ฐ™์€ ํ›„ํฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ต์…˜์œผ๋กœ ์‚ฌ์šฉํ• ์ˆ˜์žˆ๋‹ค

refetching

:๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๊ธฐ

onClick ์ด๋ฒคํŠธ์‹œ๋งˆ๋‹ค refetch ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ณด๋ฅผ ๋‹ค์‹œ ๋ฐ›์•„์˜ค๊ณ  ์žˆ๋‹ค
๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์ด๋“œ์ดํŽ™ํŠธ (๋กœ๋“œ ์ƒํƒœ์— ์•ฝ๊ฐ„์˜ ๋ณต์žก์„ฑ์ด) ์ด์žˆ๋‹ค ์•„๋ž˜์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃฐ ์˜ˆ์ •

์บ์‹œ์— ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ ๋ฐ ์“ฐ๊ธฐ

Apollo client ์—์„œ ์บ์‹œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ์‹์„ ์ง€์›ํ•œ๋‹ค

readQuery

์บ์‹œ์—์„œ ์ง์ ‘ GraphQL ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
ID๊ฐ€ 5์ธ ์บ์‹œ๋œ ํ•  ์ผ ํ•ญ๋ชฉ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
์ด ๊ฐœ์ฒด์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
์œ ํ˜•์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๋ณ€์ˆ˜๋Š” null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


๋กœ๋“œ ์ƒํƒœ ๊ฒ€์‚ฌ

useQuery ๋Š” loading ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค

๊ทธ๋Ÿผ ์žฌํ˜ธ์ถœ์‹œ(refetch) ์‹œ์—๋„ loading ์ƒํƒœ๊ฐ€ ๋ณ€ํ• ๊นŒ?
์•„๋‹ˆ๋‹ค ํ˜ธ์ถœ๋œ ๋ฐ์ดํ„ฐ๋Š” ๊ทธ๋Œ€๋กœ์ด๊ณ  loading ๊ฐ’๋„ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค
๊ทธ๋Ÿผ ์žฌํ˜ธ์ถœ์‹œ ๋กœ๋”ฉ ์ƒํƒœ์— ๋Œ€ํ•ด์„œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋ ค์ฃผ๊ณ  ์‹ถ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ?

๋ฒˆ์—ญ์ด ์ด์ƒํ•œ๋ฐ... useQuery ํ›…์—์„œ ์กฐ๊ฑด(notifyOnNetworkStatusChange) ์„ ๊ฑธ์–ด์ฃผ๋ฉด ๋œ๋‹ค

  if (networkStatus === NetworkStatus.refetch) return 'Refetching!';
networkStatus์˜ refetch ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๋“œ ์ƒํƒœ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค
NetworkStatus ์ƒํƒœ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด enum ์œผ๋กœ ์ •์˜ ๋˜์–ด์žˆ๋‹ค(5๋Š” ์–ด๋”ฐ๋บด๋จน์€๊ฒจ)
export enum NetworkStatus {
  loading = 1,
  setVariables = 2,
  fetchMore = 3,
  refetch = 4,
  poll = 6,
  ready = 7,
  error = 8,
}

https://github.com/apollographql/apollo-client/blob/main/src/core/networkStatus.ts


2. ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ

Apollo client ์‚ฌ์šฉํ•ด ์„œ๋ฒ„๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•ด ๋ด…์‹œ๋‹ค


3.๋‹น์‹ ์ด apollo -client ๋„์ž…์‹œ ๋งˆ์ฃผํ•  ์„ค์ •๋“ค

ApolloClient

ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค


Advanced HTTP networking

์ด๊ธฐ๋Šฅ์œผ๋กœ ๋„คํŠธ์›Œํฌ ๋™์ž‘ , ๋ฐ์ดํ„ฐ ํ๋ฆ„ ์„ ์ œ์–ด ํ•จ
์“ธ๋•Œ๋Š” ๊ฐœ์ฒด๋ชจ์Œ์œผ๋กœ ์ •์˜
Ex)
 link: from([
   link.auth,
   link.error,
   link.persistedQueries,
 ]),
์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ๋ณด์ž!

์—๋Ÿฌ ํ•ธ๋“ค๋ง ๋งํฌ๋„ ํ•œ๋ฒˆ ๋ณด์ž


์ข…๋ฅ˜

 `from` ํ•จ์ˆ˜๋Š” ๊ฐœ๋ณ„ ๋งํฌ์˜ ๋ฐฐ์—ด์„ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.

๋Œ€๋žต์ ์ธ ์‹œ์ž‘ ์‹œ๊ฐ„์„ ์ž‘์—… ์ปจํ…์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๋Š” ์š”์ฒญ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ •์˜ ํ•˜๋Š” ์˜ˆ์ œ

operation , forward ๋ž€ ?

Context ๊ด€๋ฆฌ

์˜ˆ์ œ

์บ์‹ฑ์กฐํšŒ

์‚ฌ์šฉ์ž ์ธ์ฆ ํ† ํฐ ์˜ˆ์ œ
์ธ์ฆํ† ํฐ์„์ฐพ๊ณ  ์บ์‹œํ•œ๋‹ค์Œ 401 ์‘๋‹ต์—์„œ ์ œ๊ฑฐ

๊นƒํ—™ ๋ฌธ์„œ

InMemoryCache()

์ฐธ์กฐ

profile
medium ์œผ๋กœ ์ด์ „ํ–ˆ์Šต๋‹ˆ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€