Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Apollo Client Cache

kobayang
October 10, 2019

Apollo Client Cache

kobayang

October 10, 2019
Tweet

More Decks by kobayang

Other Decks in Technology

Transcript

  1. ©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 } } `;
  2. ©2018 Wantedly, Inc. Agenda • Apollo Client ͷ Cache ͸͍ͭߦΘΕΔ͔

    • ͲͷΑ͏ʹCacheͷߋ৽Λߦ͏͔ • ͲΜͳσʔλ͕Cacheʹೖ͍ͬͯΔ͔ • Cacheͷߋ৽ͷ࠷దԽ
  3. ©2018 Wantedly, Inc. Fetch Data Flow User Interface Fetch Cache

    3FRVFTUUP UIFTFSWFS .PVOUPSVTFSBDUJPO USJHHFSTGFUDI Apollo Server 3FBEDBDIF
  4. ©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 } `;
  5. ©2018 Wantedly, Inc. Mutation Data Flow User Interface Mutation Apollo

    Server Cache 3FRVFTUUPUIFTFSWFS VTFSBDUJPO USJHHFSTNVUBUJPO 4UPSFDIBOHFT ESJWFT6*
  6. ©2018 Wantedly, Inc. Mutation Data Flow User Interface Mutation Apollo

    Server Cache 3FRVFTUUPUIFTFSWFS VTFSBDUJPO USJHHFSTNVUBUJPO 4UPSFDIBOHFT ESJWFT6* ࣮૷͢Δඞཁ͕͋Δ
  7. ©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
  8. ©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
  9. ©2018 Wantedly, Inc. readFragment const data = client.readFragment<TodoFragment>({ id: "Todo:1",

    fragment: todoFragment, }); console.log(data); // => { id: 1, title: "ൃදࢿྉΛ࡞Δ", __typename: "Todo" }
  10. ©2018 Wantedly, Inc. id͕ͳ͍৔߹ • ࣮͸readFragmentͰऔಘͰ͖Δ const data = client.readFragment<TodoFragment>({

    id: “ROOT_QUERY.todos.0”, fragment: todoFragment, }); console.log(data); // => { title: "ൃදࢿྉΛ࡞Δ", __typename: "Todo" }
  11. ©2018 Wantedly, Inc. Mutation Data Flowʢ࠶ܝʣ User Interface Mutation Apollo

    Server Cache 3FRVFTUUPUIFTFSWFS VTFSBDUJPO USJHHFSTNVUBUJPO 4UPSFDIBOHFT ESJWFT6*
  12. ©2018 Wantedly, Inc. Optimistic Response Mutation ͷҾ਺ʹ௥Ճ optimisticResponse: vars =>

    ({ addTodo: { id: "-1", title: vars.title, __typename: "Todo", }, }), update: (cache, { data }) => { console.log("update", data); //… } update͸mutation࣮ߦ࣌ͱߋ৽࣌ʹ 2ճݺ͹ΕΔ
  13. ©2018 Wantedly, Inc. ·ͱΊ • Apollo Clientʹ͸Ωϟογϡͷ࢓૊Έ͕͋Γ
 Fetchͨ͠σʔλΛਖ਼نԽͯ͠Ωϟογϡ͍ͯ͠Δ
 • Mutation࣌͸ɺঢ়گʹ߹Θͤͯ

    refetchQueries, update, optimisticResponse Λ࣮૷ͯ͠ΩϟογϡΛߋ৽ͤ͞Δඞཁ͕͋Δ
 • ΩϟογϡΛҙࣝ͢Δ͜ͱͰύϑΥʔϚϯεΛ޲্ͤ͞Δ͜ͱ͕Ͱ͖Δ