1import ApolloClient from 'apollo-boost'; 2import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'; 3 4import Store from '../redux/Store'; 5import Config from './Config'; 6import Connectivity from './Connectivity'; 7import graphqlFragmentTypes from './generated/graphqlFragmentTypes.json'; 8 9export default new ApolloClient({ 10 uri: `${Config.api.origin}/--/graphql`, 11 12 async request(operation): Promise<void> { 13 const isConnected = await Connectivity.isAvailableAsync(); 14 if (!isConnected) { 15 throw new Error('No connection available'); 16 } 17 18 const { sessionSecret } = Store.getState().session; 19 if (sessionSecret) { 20 operation.setContext({ 21 headers: { 'expo-session': sessionSecret }, 22 }); 23 } 24 }, 25 26 cache: new InMemoryCache({ 27 fragmentMatcher: new IntrospectionFragmentMatcher({ 28 introspectionQueryResultData: graphqlFragmentTypes, 29 }), 30 dataIdFromObject(value) { 31 // Make sure to return null if this object doesn't have an ID 32 return value.hasOwnProperty('id') ? value.id : null; 33 }, 34 }), 35}); 36