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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
Don't write the architecture only in the prompt.
neohsu
0
310
kaonavi Tech Night#2
kaonavi
0
110
Content Mapper APIの応用例~CSS Modules、言語ファイル~
uhyo
2
570
増え続けるプロダクトの品質をどう担保するか。ProductSpecとTypeScriptによる取り組み
joere
3
380
AX時代の自治体支援リソース再配分と役割分担の再構成:戦略とロードマップ(概要)
sishi2026
0
190
[General] 개발자로 해외 기업에서 일하기
skydoves
1
290
サボる子はいねぇがー!AgentCoreハーネスの脆弱性に学ぶ、エージェントの入力バリデーション
iwamot
PRO
3
150
cdkd (CDK Direct)
gotok365
3
200
AIで選択肢が広がった組織設計の理想と現実
konifar
16
7.7k
re:Inventで、人生は変わったのか? / growth-builders-hub-1-reinvent-2026
mhrtech
2
310
IoTとは〜? 数字で見るSORACOM UG【SORACOM UG Explorer 2026】
soracom
PRO
0
170
(swiftc) NonCopyableとライフタイム修飾子
teamhimeh
0
170
Featured
See All Featured
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
740
A Soul's Torment
seathinner
8
3.9k
Building Better People: How to give real-time feedback that sticks.
wjessup
371
20k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
560
GraphQLとの向き合い方2022年版
quramy
50
15k
Being A Developer After 40
akosma
PRO
92
590k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
470
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
520
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
730
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
520
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 Λ࣮ͯ͠ΩϟογϡΛߋ৽ͤ͞Δඞཁ͕͋Δ • ΩϟογϡΛҙࣝ͢Δ͜ͱͰύϑΥʔϚϯεΛ্ͤ͞Δ͜ͱ͕Ͱ͖Δ