我想使用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);
我该怎么做才能让它工作呢?谢谢!
发布于 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()
。
https://stackoverflow.com/questions/51485713
复制相似问题