All files / web ApolloClientWrapper.tsx

0% Statements 0/25
0% Branches 0/2
0% Functions 0/4
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                                                                                                     
import * as React from 'react';
import { ApolloProvider } from 'react-apollo';
import { connect } from 'react-redux';
import { selectToken } from './modules/auth/reducer';
import { IState } from './types';
 
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClient } from 'apollo-client';
import { ApolloLink } from 'apollo-link';
import { onError } from 'apollo-link-error';
import { createUploadLink } from 'apollo-upload-client';
import { GRAPHQL_ENDPOINT } from '../shared/config';
 
declare const window: any;
 
const ApolloClientWrapper = ({ children, token }: any) => {
  const client = new ApolloClient({
    link: ApolloLink.from([
      onError(({ graphQLErrors, networkError }) => {
        if (graphQLErrors) {
          graphQLErrors.map(({ message, locations, path }) =>
            console.log(
              `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
            ),
          );
        }
        if (networkError) {
          console.log(`[Network error]: ${networkError}`);
        }
      }),
      createUploadLink({
        uri: GRAPHQL_ENDPOINT,
        credentials: 'same-origin',
        headers: {
          Authorization: token ? `Bearer ${token}` : '',
        },
      }),
    ]),
    cache: new InMemoryCache().restore(window.__APOLLO_STATE__),
  });
 
  return <ApolloProvider client={client}>{children}</ApolloProvider>;
};
 
export default connect(
  (state: IState) => ({
    token: selectToken(state),
  }),
  {},
)(ApolloClientWrapper);