我有一个使用阿波罗客户端的反应应用程序。我使用的是apollo-link-state和apollo-cache-persist,在我的应用程序中调用client.resetStore()时,需要将其重置为默认值。
文档说,在创建客户端时,您应该调用client.onResetStore(stateLink.writeDefaults),这将完成任务,但是writeDefaults是由阿波罗链接状态公开的,我无法直接访问它,因为我使用的是阿波罗助推。
这是我的阿波罗客户端代码:
import ApolloClient from 'apollo-boost';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { persistCache } from 'apollo-cache-persist';
import { defaults, resolvers } from "./store";
const cache = new InMemoryCache();
persistCache({
storage: window.localStorage,
debug: process.env.NODE_ENV !== 'production',
cache
});
const client = new ApolloClient({
uri: process.env.NODE_ENV === 'production' ? 'https://five-yards-api.herokuapp.com/graphql' : 'http://localhost:7777/graphql',
credentials: 'include',
clientState: {
defaults,
resolvers
},
cache
});
// TODO: Doesn't work as expected
client.onResetStore(client.writeDefaults);
export default client;发布于 2018-09-23 02:26:06
AFAIK的唯一方法是从阿波罗Boost迁移,它为您配置了很多东西,并手动设置了阿波罗客户端。迁移之后,我能够按照文档调用onResetStore(),并且一切都在工作:)
阿波罗推进迁移:https://www.apollographql.com/docs/react/advanced/boost-migration.html
发布于 2019-08-06 01:51:33
我使用阿波罗客户2.x,阿波罗助推可能是一样的。我对阿波罗客户2.x也有同样的问题,下面是我的解决方案。在您配置阿波罗的根文件中(例如,App.js):
cache.writeData({data : defaultData }); # I assume you already have this to initialise your default data.
# Then, you just add below underneath.
client.onResetStore(() => {
cache.writeData({data : defaultData });
});
const App = () => (...https://stackoverflow.com/questions/52423012
复制相似问题