File size: 3,160 Bytes
1e92f2d |
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 112 |
// @flow
import { ApolloClient } from 'apollo-client';
import { createUploadLink } from 'apollo-upload-client';
import { RetryLink } from 'apollo-link-retry';
import {
InMemoryCache,
IntrospectionFragmentMatcher,
} from 'apollo-cache-inmemory';
import { split } from 'apollo-link';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';
import introspectionQueryResultData from './schema.json';
import getSharedApolloClientOptions from './apollo-client-options';
import { API_URI, WS_URI } from './constants';
// Fixes a bug with ReactNative, see https://github.com/facebook/react-native/issues/9599
if (typeof global.self === 'undefined') {
global.self = global;
}
type CreateClientOptions = {
token?: ?string,
};
// Websocket link for subscriptions
export const wsLink = new WebSocketLink({
uri: WS_URI,
options: {
reconnect: true,
},
});
// NOTE(@mxstbr): Use the exported client instance from below instead of using this factory!
// Only use this factory if you need to create a new instance of the client with the Authorization token,
// i.e. only use this factory on mobile
export const createClient = (options?: CreateClientOptions = {}) => {
const cache = new InMemoryCache({
fragmentMatcher: new IntrospectionFragmentMatcher({
introspectionQueryResultData,
}),
...getSharedApolloClientOptions(),
});
const headers = options.token
? {
authorization: `Bearer ${options.token}`,
}
: undefined;
const retryLink = new RetryLink({
attempts: (count, operation, error) => {
const isMutation =
operation &&
operation.query &&
operation.query.definitions &&
Array.isArray(operation.query.definitions) &&
operation.query.definitions.some(
def =>
def.kind === 'OperationDefinition' && def.operation === 'mutation'
);
// Retry mutations for a looong time, those are very important to us so we want them to go through eventually
if (isMutation) {
return !!error && count < 25;
}
// Retry queries for way less long as this just ends up showing
// loading indicators for that whole time which is v annoying
return !!error && count < 6;
},
});
// HTTP Link for queries and mutations including file uploads
const httpLink = retryLink.concat(
createUploadLink({
uri: API_URI,
credentials: 'include',
headers,
})
);
// Switch between the two links based on operation
const link = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink
);
return new ApolloClient({
link,
// eslint-disable-next-line
cache: window.__DATA__ ? cache.restore(window.__DATA__) : cache,
ssrForceFetchDelay: 100,
queryDeduplication: true,
});
};
const client = createClient();
export { client };
export const clearApolloStore = () => {
try {
client.resetStore();
} catch (e) {
console.error('error clearing store');
}
};
|