1import { ApolloClient, InMemoryCache } from '@apollo/client'; 2import { setContext } from '@apollo/client/link/context'; 3import { HttpLink } from '@apollo/client/link/http'; 4 5import Store from '../redux/Store'; 6import Config from './Config'; 7import Connectivity from './Connectivity'; 8 9const httpLink = new HttpLink({ 10 uri: `${Config.api.origin}/--/graphql`, 11}); 12 13const connectivityLink = setContext( 14 async (): Promise<any> => { 15 const isConnected = await Connectivity.isAvailableAsync(); 16 if (!isConnected) { 17 throw new Error('No connection available'); 18 } 19 } 20); 21 22const authMiddlewareLink = setContext((): any => { 23 const { sessionSecret } = Store.getState().session; 24 if (sessionSecret) { 25 return { 26 headers: { 'expo-session': sessionSecret }, 27 }; 28 } 29}); 30 31const link = connectivityLink.concat(authMiddlewareLink.concat(httpLink)); 32 33const cache = new InMemoryCache({ 34 possibleTypes: { 35 ActivityTimelineProjectActivity: ['Build', 'BuildJob'], 36 BuildOrBuildJob: ['Build', 'BuildJob'], 37 BaseSearchResult: ['UserSearchResult', 'AppSearchResult'], 38 Project: ['App', 'Snack'], 39 }, 40 addTypename: true, 41 typePolicies: { 42 Query: { 43 fields: { 44 account: { 45 merge: false, 46 }, 47 app: { 48 merge: false, 49 }, 50 }, 51 }, 52 }, 53}); 54 55export default new ApolloClient({ 56 link, 57 cache, 58}); 59