首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过特定的查询或突变重置缓存

通过特定的查询或突变重置缓存
EN

Stack Overflow用户
提问于 2018-07-24 03:12:54
回答 1查看 1.9K关注 0票数 2

我想使用react-apollo的客户端从键或查询中重置缓存的一部分,而不是使用client.resetStore()清除整个缓存。

示例:

import { withApollo } from "react-apollo";
import { compose } from "recompose";
import MyComponent from "./MyComponent";

const cleanEspecificCache = ({ client }) => () =>{
  // What i do here?
}

const enhance = compose(
 withApollo,
 withHandlers({cleanEspecificCache})
)

export default enhance(MyComponent);

我该怎么做才能让它工作呢?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-07-24 03:43:53

根据此问题,目前不支持部分阿波罗缓存清除:https://github.com/apollographql/apollo-feature-requests/issues/4

您可以使用fechPolicy: 'network-only'根本不缓存特定的查询:

const enhance = compose(
  graphql(
    gql`{
      food {
        id
        name
      }
    }`,
    {
      options: { fetchPolicy: 'network-only' },
    }
  ),
  ...
);

export default enhance(MyComponent);

如果你想深入到兔子洞,现在有一个解决方案,你可以尝试https://github.com/sysgears/apollo-cache-router,它允许将你的缓存拆分为两个或更多缓存,然后你可以分别重置这些缓存:

import { InMemoryCache } from 'apollo-cache-inmemory';
import ApolloCacheRouter from 'apollo-cache-router';
import { hasDirectives } from 'apollo-utilities';

const generalCache = new InMemoryCache();
const specialCache = new InMemoryCache();
const cache = ApolloCacheRouter.override(
  ApolloCacheRouter.route([generalCache, specialCache], document => {
    if (hasDirectives(['special'], document)) {
      // Pass all queries marked with @special into specialCache
      return [specialCache];
    } else {
      // Pass all the other queries to generalCache
      return [generalCache];
    }
  })
);

当您想要重置specialCache时,在代码中调用specialCache.reset()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51485713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档