【发布时间】:2019-02-24 15:22:49
【问题描述】:
我有一个使用 Apollo 客户端的 React 应用程序。我正在使用apollo-link-state 和apollo-cache-persist 并且需要在我的应用程序中调用client.resetStore() 时将我的商店重置为其默认值。
文档说,在创建客户端时,您应该调用 client.onResetStore(stateLink.writeDefaults),这将完成这项工作,但 writeDefaults 由 Apollo Link State 公开,我无法直接访问,因为我正在使用 Apollo Boost。
这是我的 Apollo 客户端代码:
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;
【问题讨论】:
标签: reactjs apollo react-apollo apollo-client apollo-link-state