Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

client side GraphQL query cache #335

Open
mydearxym opened this issue Jan 15, 2019 · 2 comments
Open

client side GraphQL query cache #335

mydearxym opened this issue Jan 15, 2019 · 2 comments

Comments

@mydearxym
Copy link
Member

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
Copy link
Member Author

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 mydearxym reopened this Jan 15, 2019
@mydearxym
Copy link
Member Author

reopen it for better solution

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant