xref: /expo/home/api/ApolloClient.ts (revision ca5a2fa2)
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
23  if (sessionSecret) {
24    return {
25      headers: { 'expo-session': sessionSecret },
26    };
27  }
28});
29
30const link = connectivityLink.concat(authMiddlewareLink.concat(httpLink));
31
32const cache = new InMemoryCache({
33  possibleTypes: {
34    ActivityTimelineProjectActivity: ['Build', 'BuildJob'],
35    BuildOrBuildJob: ['Build', 'BuildJob'],
36    BaseSearchResult: ['UserSearchResult', 'AppSearchResult'],
37    Project: ['App', 'Snack'],
38  },
39  addTypename: true,
40  typePolicies: {
41    Query: {
42      fields: {
43        account: {
44          merge: false,
45        },
46        app: {
47          merge: false,
48        },
49      },
50    },
51  },
52});
53
54export default new ApolloClient({
55  link,
56  cache,
57});
58