使用SRSR Apollo客户端时无法找到全局对象错误消息

时间:2018-11-25 02:52:08

标签: reactjs react-apollo apollo-client serverside-rendering react-on-rails

在使用React Router v4的服务器端渲染apollo的导轨上使用React时,出现以下错误:Error: unable to locate global object。我尝试实现以下内容,但没有成功:

ApolloClientServer.js

import { ApolloClient } from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';
import fetch from 'node-fetch';

const httpLink = createHttpLink({
  uri: '/graphql',
  fetch,
  clientState: {
    defaults: {},
  }
});

const authLink = setContext((_, { headers }) => {
  const email = localStorage.getItem('email');
  const token = localStorage.getItem('token');
  return {
    headers: {
      ...headers,
      email,
      authorization: token ? `Bearer ${token}` : "",
    }
  }
});

export const client = new ApolloClient({
  ssrMode: true,
  link: authLink.concat(httpLink),
  cache: new InMemoryCache()
});

Server.js:

import React from 'react';
import { Provider } from 'react-redux';
import { StaticRouter } from 'react-router';
import adminStore from '../redux/store';
import AdminMain from './AdminMain';
import { ApolloProvider } from 'react-apollo';
import { client } from '../utils/ApolloClientServer';

export default (_props, railsContext) => {
  ....

  return (
    <ApolloProvider client={client}>
      <StaticRouter
        location={location}
        context={context}
      >
        <AdminMain />
      </StaticRouter>
    </ApolloProvider>
  );
};

是否缺少我的配置,或者我需要导入另一个库?

0 个答案:

没有答案