reactjs - 您可以在 ApolloClient 的客户端解析器中访问“客户端”,而不是“缓存”吗?
问题描述
是否可以client
从 ApolloClient 解析器中访问,而不是cache
?
我正在尝试编写一个解析器,当单击按钮(本质上是注销)时清除 localStorage 和缓存,但不知道如何访问客户端。
我可以毫无问题地访问缓存,但是尽管有官方文档,但我在控制台(或)中得到一个cache.resetStore()
不是函数错误。cache.clearStore() if I use that function
运行 ApolloClient、React 和 apollo-link-state,使用 compose 将组件包装在graphql()
HOC 中。
解析器.js
Mutation: {
logout: (_, __, { cache }) => {
localStorage.clear();
cache.writeData({
data: {
AuthData: {
__typename: 'AuthData',
token: null,
currentUserId: null,
expirationTime: null
}
}
});
}
}
};
index.js
import ReactDOM from 'react-dom';
import App from './App';
import { BrowserRouter } from 'react-router-dom';
//Apollo
import { ApolloProvider } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import { ApolloLink } from 'apollo-link';
import { createHttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { withClientState } from 'apollo-link-state';
import { resolvers } from './resolvers';
const cache = new InMemoryCache();
...
...
const initialState = {
AuthData: {
__typename: 'AuthData',
token: localStorage.getItem('token') || null,
currentUserId: localStorage.getItem('currentUserId') || null,
expirationTime: localStorage.getItem('expirationTime') || null
},
ErrorLog: {
__typename: 'ErrorLog',
errors: []
}
};
const stateLink = withClientState({
cache,
defaults: initialState,
resolvers
});
const link = ApolloLink.from([stateLink, httpLink]);
const client = new ApolloClient({
link,
cache
});
预期的:
- 要清除客户端缓存中的所有数据。
实际的:
- 得到错误
client is not defined
或者cache.resetStore()
不是一个函数。
解决方案
推荐阅读
- android - 将 LayoutInflater 替换为片段
- python - 在 HTML 中使用 Django 框架查询 MYSQL
- github - 联系github,源码树
- android - 如何在 for 循环中修改 List 的元素
- 3d - 嵌入网站的 3D 模型修改器
- python - 如何让 tox 测试具有不同 python 版本的“命名”测试环境?
- python - Django cms:{% url %} 标签未在 static_placeholders [django-cms 版本:3.5.2] 下加载
- dialogflow-es - 如何将后续意图上传到另一个父意图
- twitter-bootstrap - 引导弹出框在导航栏中不起作用
- winforms - 在 Krypton 工具包中导致异常“ReadOnly 控件集合”