xref: /expo/home/api/ApolloClient.ts (revision 0dfbbd2a)
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(async (): Promise<any> => {
14  const isConnected = await Connectivity.isAvailableAsync();
15  if (!isConnected) {
16    throw new Error('No connection available');
17  }
18});
19
20const authMiddlewareLink = setContext((): any => {
21  const { sessionSecret } = Store.getState().session;
22  if (sessionSecret) {
23    return {
24      headers: { 'expo-session': sessionSecret },
25    };
26  }
27});
28
29const link = connectivityLink.concat(authMiddlewareLink.concat(httpLink));
30
31const cache = new InMemoryCache({
32  possibleTypes: {
33    ActivityTimelineProjectActivity: ['Build', 'BuildJob'],
34    BuildOrBuildJob: ['Build', 'BuildJob'],
35    BaseSearchResult: ['UserSearchResult', 'AppSearchResult'],
36    Project: ['App', 'Snack'],
37  },
38  addTypename: true,
39  typePolicies: {
40    Query: {
41      fields: {
42        account: {
43          merge: false,
44        },
45        app: {
46          merge: false,
47        },
48      },
49    },
50  },
51});
52
53export default new ApolloClient({
54  link,
55  cache,
56});
57