如何在 apollo-link 或 apollo-client 中禁用缓存? [英] How to disable cache in apollo-link or apollo-client?

查看:141
本文介绍了如何在 apollo-link 或 apollo-client 中禁用缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 apollo-clientapollo-linkreact-apollo,我想完全禁用缓存,但不要不知道怎么做.

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

我阅读了 apollo-cache-inmemory 的源代码,它的构造函数中有一个 config 参数,但我无法构建一个虚拟的 storeFactory 使其工作.

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.

推荐答案

您可以将 defaultOptions 设置为您的客户端,如下所示:

You can set defaultOptions to your client like this:

const defaultOptions: 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 no-cache 避免使用缓存.

fetchPolicy as no-cache avoids using the cache.

参见 https://www.apollographql.com/docs/react/api/apollo-client/#apolloclient-functions

这篇关于如何在 apollo-link 或 apollo-client 中禁用缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆