import { InMemoryCache , defaultDataIdFromObject } from 'apollo-cache-inmemory' ; import { ApolloClient } from 'apollo-client' ; import { ApolloLink } from 'apollo-link' ; import { setContext } from 'apollo-link-context' ; import { onError } from 'apollo-link-error' ; import { createUploadLink } from 'apollo-upload-client' ; import 'cross-fetch/polyfill' ; import intl from 'react-intl-universal' ; declare var API_SERVER ; const uploadLink = createUploadLink ( { uri : API_SERVER , fetch } ) ; const errorLink = onError ( ( { graphQLErrors , networkError } ) => { if ( networkError ) { console . log ( `[Network error]: ${ networkError }` ) ; } if ( graphQLErrors ) { graphQLErrors . map ( ( { message , locations , path } ) => { console . log ( `[GraphQL error]: Message: ${ message }, Location: ${ locations }, Path: ${ path }` ) ; } ) ; } } ) ; const authLink = setContext ( ( _ , { headers } ) => { const token = localStorage . getItem ( 'token' ) ; return { headers : { ... headers , authorization : token ? `JWT ${ token }` : '' } } ; } ) ; const acceptLanguageLink = setContext ( ( _ , { headers } ) => { const locale = intl . getInitOptions ( ) . currentLocale ; return { headers : { ... headers , 'Accept-Language' : locale } } ; } ) ; const link = ApolloLink . from ( [ authLink , errorLink , acceptLanguageLink , uploadLink ] ) ; const cache = new InMemoryCache ( { dataIdFromObject : object => { if ( 'name' in object ) { return defaultDataIdFromObject ( object ) + object [ 'name' ] ; } if ( 'title' in object ) { return defaultDataIdFromObject ( object ) + object [ 'title' ] ; } return defaultDataIdFromObject ( object ) ; } } ) ; export const client = new ApolloClient ( { link , cache , } ) ;