coderplanets_web icon indicating copy to clipboard operation
coderplanets_web copied to clipboard

client side GraphQL query cache

Open mydearxym opened this issue 6 years ago • 2 comments
trafficstars

Apollo Client has the ability to cache query in client side:

export const client = new ApolloClient({
  link,
  /* cache: new InMemoryCache(), */
  cache: new InMemoryCache(),
  connectToDevTools: true,
  /* shouldBatch: false, */
  // defaultOptions,
})

the tricky part is to refresh cache when user do some mutate operation, like create post (post list should then be update)。

the official writeQuery solution mentioned in apollo doc is complex and adjectived.

need better solution.

mydearxym avatar Jan 15 '19 04:01 mydearxym

here is the temporary plan:

  • still use the default InMemoryCache in Apollo Client
  • once user do any mutate operation, then clear the cache store

key code:

const doMutate = (mutation, variables) =>
  client
    .mutate({
      mutation,
      variables,
      context,
    })
    .then(res => {
      // once login user has mutation to server
      // then clear all the cache store in Apollo client
      client.clearStore()
      return res.data
    })
    .catch(formatGraphErrors)

docs: https://www.apollographql.com/docs/react/recipes/authentication.html

it's not the perfect solution, but works well.

mydearxym avatar Jan 15 '19 05:01 mydearxym

reopen it for better solution

mydearxym avatar Jan 15 '19 05:01 mydearxym