message: Reconfigures client initialization; allowing it to be used server-side
diff: +import fetch from 'node-fetch';
  import sharify from 'sharify';
  import React from 'react';
  import { ApolloProvider } from 'react-apollo';
  @@ -10,13 +11,39 @@ import mount from 'react/util/mount';
  import introspectionQueryResultData from 'react/apollo/fragmentTypes.json';
  -const { data: { GRAPHQL_ENDPOINT, X_APP_TOKEN } } = sharify;
  +const isClientSide = typeof window !== 'undefined';
  -const httpLink = createHttpLink({ uri: GRAPHQL_ENDPOINT });
  +const { data: { GRAPHQL_ENDPOINT } } = sharify;
  +
  +const httpLink = createHttpLink({ uri: GRAPHQL_ENDPOINT, fetch });
  +
  +const fragmentMatcher = new IntrospectionFragmentMatcher({
  + introspectionQueryResultData: {
  + __schema: {
  + types: introspectionQueryResultData,
  + },
  + },
  +});
  +
  +export const wrapWithApolloProvider = client => (Component, props = {}) => (
  + <ApolloProvider client={client}>
  + <Component {...props} />
  + </ApolloProvider>
  +);
  +
  +export const initApolloClient = (token) => {
  + const cache = new InMemoryCache({ fragmentMatcher });
  +
  + if (isClientSide && window.__APOLLO_STATE__) {
  + cache.restore(window.__APOLLO_STATE__);
  + }
  +
  + const { data: { X_APP_TOKEN, CURRENT_USER } } = sharify;
  const authLink = setContext((_, { headers }) => {
  - const { data: { CURRENT_USER } } = sharify;
  - const X_AUTH_TOKEN = (CURRENT_USER && CURRENT_USER.authentication_token) || '';
  + const X_AUTH_TOKEN = (
  + token || (isClientSide && CURRENT_USER && CURRENT_USER.authentication_token) || ''
  + );
  return {
  headers: {
  @@ -27,26 +54,26 @@ const authLink = setContext((_, { headers }) => {
  };
  });
  -const fragmentMatcher = new IntrospectionFragmentMatcher({
  - introspectionQueryResultData: {
  - __schema: {
  - types: introspectionQueryResultData,
  - },
  - },
  + const link = authLink.concat(httpLink);
  +
  + const client = new ApolloClient({
  + ssrMode: !isClientSide,
  + link,
  + cache,
  });
  -const cache = new InMemoryCache({ fragmentMatcher });
  -const link = authLink.concat(httpLink);
  -const client = new ApolloClient({ link, cache });
  + return client;
  +};
  -export const provide = (Component, props = {}) => (
  - <ApolloProvider client={client}>
  - <Component {...props} />
  - </ApolloProvider>
  -);
  +if (isClientSide) {
  + initApolloClient();
  +}
  export default (Component, props = {}, mountNode) => {
  if (!mountNode) return null;
  - return mount(provide(Component, props), mountNode);
  + const client = initApolloClient();
  + const WrappedComponent = wrapWithApolloProvider(client)(Component, props);
  +
  + return mount(WrappedComponent, mountNode);
  };
  