-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApolloClient.ts
111 lines (96 loc) · 3.1 KB
/
ApolloClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {
InMemoryCache,
NormalizedCacheObject,
IntrospectionFragmentMatcher
} from 'apollo-cache-inmemory';
import { persistCache } from 'apollo-cache-persist';
import { ApolloClient } from 'apollo-client';
import { split } from 'apollo-link';
import { setContext } from 'apollo-link-context';
import { createHttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { onError } from 'apollo-link-error';
import { getMainDefinition } from 'apollo-utilities';
import { AsyncStorage } from 'react-native';
import { PersistentStorage, PersistedData } from 'apollo-cache-persist/types';
import { WEB_SERVER_ADDRESS, WEB_SERVER_PORT } from './config';
import { typeDefs, resolvers } from './apollo-cache';
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData: { __schema: { types: [] } }
});
// TODO change the ip address before production
export const cache = new InMemoryCache({ fragmentMatcher });
cache.writeData({
data: {
lastMessageReadIds: []
}
});
// const persistor = new CachePersistor({
// cache,
// storage: AsyncStorage as PersistentStorage<
// PersistedData<NormalizedCacheObject>
// >
// });
persistCache({
cache,
storage: AsyncStorage as PersistentStorage<
PersistedData<NormalizedCacheObject>
>
});
//Instead of using persistCache, you can instantiate a CachePersistor,
// which will give you fine - grained control of persistence.
// persistor.restore(); // Immediately restore the cache. Returns a Promise.
// // persistor.persist(); // Immediately persist the cache. Returns a Promise.
// persistor.purge(); // Immediately purge the stored cache. Returns a Promise.
// persistor.persist();
// Create an http link:
const httpLink = createHttpLink({
//TODO https
uri: `http://${WEB_SERVER_ADDRESS}:${WEB_SERVER_PORT}`,
fetchOptions: {
reconnect: true
}
});
// Create a WebSocket link:
const wsLink = new WebSocketLink({
//TODO: wss
uri: `ws://${WEB_SERVER_ADDRESS}:${WEB_SERVER_PORT}`,
options: {
reconnect: true
}
});
const errorLink = onError(({ graphQLErrors }) => {
if (graphQLErrors) graphQLErrors.map(({ message }) => console.log(message));
});
const authLink = setContext(async (_, { headers }) => {
// get the authentication token from local storage if it exists
const token = await AsyncStorage.getItem('token');
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
};
});
// using the ability AllChatsAndMessagesto split links, you can send data to each link
// depending on what kind of operation is being sent
const link = split(
// split based on operation type
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
errorLink.concat(authLink).concat(wsLink),
errorLink.concat(authLink).concat(httpLink)
);
const client: ApolloClient<NormalizedCacheObject> = new ApolloClient({
link,
cache,
typeDefs,
resolvers
});
export default client;