首页 > 解决方案 > 您可以在 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
});

预期的:

实际的:

标签: reactjsgraphqlapollo-client

解决方案


推荐阅读