Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Apollo Client Cache
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
kobayang
October 10, 2019
Technology
1.3k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Apollo Client Cache
kobayang
October 10, 2019
More Decks by kobayang
See All by kobayang
React Suspenseを使って遷移体験を向上させる
kobayang
4
2.1k
React Hooks を安全に使う
kobayang
2
1.4k
Puppeteerを導入してみた話
kobayang
2
150k
ゼロから学ぶWeb Authentication API
kobayang
0
1.3k
react-beautiful-dnd を使いたかった話
kobayang
2
2k
Other Decks in Technology
See All in Technology
ScotSecure West 2026 - Glasgow
raybugg
0
200
AIに賢く動いてもらうためのコンテキスト〜Snowflake女子会 vol.8
snowwmn0824
0
190
PQC移行の今 -- IETF からみた現在地
satokan
4
590
覗いてみよう 関数型ビジュアル言語×2Dグラフィックスの世界
yohyamasaki
0
140
AIは推し活である。
kurazuuuuuu
2
1k
AIエージェント時代のPlatform as a Product —— テックリードがPdMとして回す発見・導入・計測 / Platform as a Product in the AI Agent Era
toshi0607
1
480
2026-09-26 Platform Engineering Kaigi 2026 インフラとアプリの境界線と委譲の設計 / Drawing the Infra and App Line
masasuzu
0
420
Lambda MicroVMsが分からなすぎたので使い所を1から考えてみた
tsukuboshi
1
190
AIエージェントを安全で速い現場監督にする:Jev・Obsidian・メタハーネス
x5gtrn
PRO
0
140
HacobuにおけるFDEとは/登壇資料(戸井田 裕貴)
hacobu
PRO
1
360
【Findyテック文化祭ワークショップ】新卒エンジニア&採用担当と作る、 なりたい姿と今やるべき一歩
dip_tech
PRO
0
140
DORA_Metrics.pdf
wagnerfusca
1
120
Featured
See All Featured
Utilizing Notion as your number one productivity tool
mfonobong
4
600
We Are The Robots
honzajavorek
0
380
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
840
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Google's AI Overviews - The New Search
badams
0
1.6k
GitHub's CSS Performance
jonrohan
1033
470k
Thoughts on Productivity
jonyablonski
76
5.4k
Transcript
©2018 Wantedly, Inc. Apollo Client Cache meguro.es 2019.10.10 - Naoki
Kobayashi
©2018 Wantedly, Inc. Naoki Kobayashi GitHub: @kobayang Twitter: @kbys_02 I’m
an Frontend Engineer @Wantedly
©2018 Wantedly, Inc. https://blog.apollographql.com/introducing-the-apollo-graphql-platform-8ef34bb269e5 cache
©2018 Wantedly, Inc. { Declare GraphQL Query Use Query to
fetch data const TodoContainer = () => { const { data, loading, error } = useQuery(todosQuery); const todos = data ? data.todos : []; } const todosQuery = gql` query TodosQuery { todos { id title } } `;
©2018 Wantedly, Inc. Agenda • Apollo Client ͷ Cache ͍ͭߦΘΕΔ͔
• ͲͷΑ͏ʹCacheͷߋ৽Λߦ͏͔ • ͲΜͳσʔλ͕Cacheʹೖ͍ͬͯΔ͔ • Cacheͷߋ৽ͷ࠷దԽ
©2018 Wantedly, Inc. Fetch Data Flow User Interface Fetch Cache
3FRVFTUUP UIFTFSWFS .PVOUPSVTFSBDUJPO USJHHFSTGFUDI Apollo Server 3FBEDBDIF
©2018 Wantedly, Inc. Prefetching Data https://www.apollographql.com/docs/react/performance/performance/ onMouseOver Ͱ query ΛಡࠐΉ
LinkઌͰಉ͡ΫΤϦΛಡࠐΉͱ Cache͕ώοτ͢ΔͨΊɺ Loadingͳ͠ͰData͕͑Δ
©2018 Wantedly, Inc. Local State Management https://www.apollographql.com/docs/react/performance/performance/ • CacheΛར༻ͯ͠LocalͷStateΛѻ͑Δ •
@client directive Λ Query/Mutation ʹ ͚ͭΔ͜ͱͰऔಘઌ͕ϩʔΧϧʹͳΔ # local-schema.grpahql extend type Todo { done: Boolean! } // apollo.ts const resolvers: Resolvers = { Todo: { done(todo: TodoFragment) { return todo.done || false; }, }, }; const todoFragment = gql` fragment TodoFragment on Todo { id title done @client } `;
©2018 Wantedly, Inc. InMemoryCache https://medium.com/expedia-group-tech/creating-your-own-apollo-cache-for-perf-and-profit-8f786849e5f6 • ApolloClientͷΩϟογϡͷσϑΥϧτ࣮ • Ωϟογϡ͢ΔࡍʹσʔλΛਖ਼نԽ͢Δ(Normalize)
©2018 Wantedly, Inc. Mutation Data Flow User Interface Mutation Apollo
Server Cache 3FRVFTUUPUIFTFSWFS VTFSBDUJPO USJHHFSTNVUBUJPO 4UPSFDIBOHFT ESJWFT6*
©2018 Wantedly, Inc. Mutation Data Flow User Interface Mutation Apollo
Server Cache 3FRVFTUUPUIFTFSWFS VTFSBDUJPO USJHHFSTNVUBUJPO 4UPSFDIBOHFT ESJWFT6* ࣮͢Δඞཁ͕͋Δ
©2018 Wantedly, Inc. ྫ: TodoͷՃ BEE5PEP.VUBUJPO UPEPT2VFSZ const todosQuery =
gql` query TodosQuery { todos { id title } } `;
©2018 Wantedly, Inc. ྫ: refetchQueries const addTodoMutation = gql` mutation
AddTodoMutation($title: String!) { addTodo(title: $title) { id title } } `; const [addTodo] = useMutation(addTodoMutation, { refetchQueries: [{ query: todosQuery }], }); Cache 3FGFUDI 4FSWFS3FTQPOTF
©2018 Wantedly, Inc. ྫ: update const [addTodo] = useMutation(addTodoMutation, {
update: (cache, { data }) => { const cachedQuery = cache.readQuery({ query: todosQuery }); if (!cachedQuery || !data) { throw new Error(); } const todos = cachedQuery.todos; cache.writeQuery({ query: todosQuery, data: { todos: todos.concat([data.addTodo]) }, }); }, }); 4FSWFS3FTQPOTF Cache
©2018 Wantedly, Inc. ΩϟογϡΛ͍ͯΈΔ Apollo Client Developer Tools: ΩϟογϡͷதΛ֬ೝͰ͖Δ
©2018 Wantedly, Inc. dataIdFromObject https://github.com/apollographql/apollo-client/blob/22a450aace/packages/apollo-cache-inmemory/src/inMemoryCache.ts
©2018 Wantedly, Inc. readFragment const data = client.readFragment<TodoFragment>({ id: "Todo:1",
fragment: todoFragment, }); console.log(data); // => { id: 1, title: "ൃදࢿྉΛ࡞Δ", __typename: "Todo" }
©2018 Wantedly, Inc. id͕ͳ͍߹ • ROOT_QUERY.todos.0 ͷΑ͏ʹ ROOT_QUERY ʹඥ͍ͮͯΩϟογϡ͞ΕΔ
const todosQuery = gql` query TodosQuery { todos { title } } `;
©2018 Wantedly, Inc. id͕ͳ͍߹ • ࣮readFragmentͰऔಘͰ͖Δ const data = client.readFragment<TodoFragment>({
id: “ROOT_QUERY.todos.0”, fragment: todoFragment, }); console.log(data); // => { title: "ൃදࢿྉΛ࡞Δ", __typename: "Todo" }
©2018 Wantedly, Inc. Mutation Data Flowʢ࠶ܝʣ User Interface Mutation Apollo
Server Cache 3FRVFTUUPUIFTFSWFS VTFSBDUJPO USJHHFSTNVUBUJPO 4UPSFDIBOHFT ESJWFT6*
©2018 Wantedly, Inc. Optimistic Response Mutation Apollo Server Cache User
Interface Optimistic Response
©2018 Wantedly, Inc. Optimistic Response Mutation ͷҾʹՃ optimisticResponse: vars =>
({ addTodo: { id: "-1", title: vars.title, __typename: "Todo", }, }), update: (cache, { data }) => { console.log("update", data); //… } updatemutation࣮ߦ࣌ͱߋ৽࣌ʹ 2ճݺΕΔ
©2018 Wantedly, Inc. Optimistic Response ApolloCache removeOptimistic Ͱ optimisticResponseͷΩϟογϡআ͞ΕΔ https://github.com/apollographql/apollo-client/blob/22a450aace/packages/apollo-cache-inmemory/src/inMemoryCache.ts#L251
©2018 Wantedly, Inc. ·ͱΊ • Apollo ClientʹΩϟογϡͷΈ͕͋Γ Fetchͨ͠σʔλΛਖ਼نԽͯ͠Ωϟογϡ͍ͯ͠Δ • Mutation࣌ɺঢ়گʹ߹Θͤͯ
refetchQueries, update, optimisticResponse Λ࣮ͯ͠ΩϟογϡΛߋ৽ͤ͞Δඞཁ͕͋Δ • ΩϟογϡΛҙࣝ͢Δ͜ͱͰύϑΥʔϚϯεΛ্ͤ͞Δ͜ͱ͕Ͱ͖Δ