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