How to disable cache in apollo-link or apollo-client?

久未见 提交于 2019-12-18 11:18:32

问题


I'm using apollo-client, apollo-link and react-apollo, I want to fully disable cache, but don't know how to do it.

I read the source of apollo-cache-inmemory, it has a config argument in its constructor, but I can't build a dummy storeFactory to make it works.


回答1:


You can set defaultOptions to your client like this:

const defaultOptions = {
      watchQuery: {
        fetchPolicy: 'no-cache',
        errorPolicy: 'ignore',
      },
      query: {
        fetchPolicy: 'no-cache',
        errorPolicy: 'all',
      },
    }

const client = new ApolloClient({
    link: concat(authMiddleware, httpLink),
    cache: new InMemoryCache(),
    defaultOptions: defaultOptions,

});

fetchPolicy as network-only avoids using the cache.

See https://www.apollographql.com/docs/react/advanced/caching#ignore




回答2:


Actually, setting fetchPolicy to network-only "still saves the response to the cache for later use, bypassing the reading and forcing a network request".

If you really want to disable the cache, read and write, use no-cache.

Take a look at the official docs: https://www.apollographql.com/docs/react/advanced/caching.html#ignore




回答3:


I would always suggest not to disable inbuild caching feature from apollo client. Instead you can always set fetchPolicy: 'network-only' for an individual queries. Something like this

<Query
    query={GET_DOG_PHOTO}
    variables={{ breed }}
    fetchPolicy='network-only'
>
 {({ loading, error, data, refetch, networkStatus }) => {
   ...
 }}
</Query>

While fetching data with this Query, it would always do a network request instead of reading from cache first.



来源:https://stackoverflow.com/questions/47879016/how-to-disable-cache-in-apollo-link-or-apollo-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!