我想使用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);
我如何使它起作用?谢谢!
答案 0 :(得分:1)
部分Apollo缓存清除: https://github.com/apollographql/apollo-client/issues/621
您可以使用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()
时。