首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要将我的阿波罗客户端缓存设置为默认值。

我需要将我的阿波罗客户端缓存设置为默认值。
EN

Stack Overflow用户
提问于 2018-09-20 10:29:47
回答 2查看 1.3K关注 0票数 1

我有一个使用阿波罗客户端的反应应用程序。我使用的是apollo-link-stateapollo-cache-persist,在我的应用程序中调用client.resetStore()时,需要将其重置为默认值。

文档说,在创建客户端时,您应该调用client.onResetStore(stateLink.writeDefaults),这将完成任务,但是writeDefaults是由阿波罗链接状态公开的,我无法直接访问它,因为我使用的是阿波罗助推。

这是我的阿波罗客户端代码:

代码语言:javascript
复制
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;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-23 02:26:06

AFAIK的唯一方法是从阿波罗Boost迁移,它为您配置了很多东西,并手动设置了阿波罗客户端。迁移之后,我能够按照文档调用onResetStore(),并且一切都在工作:)

阿波罗推进迁移:https://www.apollographql.com/docs/react/advanced/boost-migration.html

票数 0
EN

Stack Overflow用户

发布于 2019-08-06 01:51:33

我使用阿波罗客户2.x,阿波罗助推可能是一样的。我对阿波罗客户2.x也有同样的问题,下面是我的解决方案。在您配置阿波罗的根文件中(例如,App.js):

代码语言:javascript
复制
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 = () => (...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52423012

复制
相关文章

相似问题

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