How to disable cache in apollo-link or apollo-clie

2019-03-25 01:41发布

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.

2条回答
何必那么认真
2楼-- · 2019-03-25 02:06

You can set defaultOptions to your client like this:

const defaultOptions = {
      watchQuery: {
        fetchPolicy: 'network-only',
        errorPolicy: 'ignore',
      },
      query: {
        fetchPolicy: 'network-only',
        errorPolicy: 'all',
      },
    }

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

});

fetchPolicy as network-only avoids using the cache.

查看更多
smile是对你的礼貌
3楼-- · 2019-03-25 02:11

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

查看更多
登录 后发表回答