Unable to locate global object error message when using ssr apollo client

800 views Asked by At

I am getting the following error when using react on rails to server side render apollo with react router v4: Error: unable to locate global object. I tried to implement the following but it didnt work:

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>
  );
};

Is there a config I am missing or do I need import another library?

0

There are 0 answers