-
Notifications
You must be signed in to change notification settings - Fork 680
/
index.js
63 lines (54 loc) 路 2.29 KB
/
index.js
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
import { createHttpLink } from '@apollo/client';
import createErrorLink from '@magento/peregrine/lib/Apollo/links/errorLink';
import createGqlCacheLink from '@magento/peregrine/lib/Apollo/links/gqlCacheLink';
import createMutationQueueLink from '@magento/peregrine/lib/Apollo/links/mutationQueueLink';
import createRetryLink from '@magento/peregrine/lib/Apollo/links/retryLink';
import createStoreLink from '@magento/peregrine/lib/Apollo/links/storeLink';
import shrinkQuery from '@magento/peregrine/lib/util/shrinkQuery';
/**
* Intercept and shrink URLs from GET queries.
*
* Using GET makes it possible to use edge caching in Magento Cloud, but risks
* exceeding URL limits with default usage of Apollo's http link.
*
* `shrinkQuery` encodes the URL in a more efficient way.
*
* @param {*} uri
* @param {*} options
*/
export const customFetchToShrinkQuery = (uri, options) => {
// TODO: add `ismorphic-fetch` or equivalent to avoid this error
if (typeof globalThis.fetch !== 'function') {
console.error('This environment does not define `fetch`.');
return () => {};
}
const resource = options.method === 'GET' ? shrinkQuery(uri) : uri;
return globalThis.fetch(resource, options);
};
const getLinks = apiBase => {
const storeLink = createStoreLink();
const errorLink = createErrorLink();
const retryLink = createRetryLink();
const gqlCacheLink = createGqlCacheLink();
const mutationQueueLink = createMutationQueueLink();
// Warning: `useGETForQueries` risks exceeding URL length limits.
// These limits in practice are typically set at or behind where TLS
// terminates. For Magento Cloud & Fastly, 8kb is the maximum by default.
// https://docs.fastly.com/en/guides/resource-limits#request-and-response-limits
const httpLink = createHttpLink({
fetch: customFetchToShrinkQuery,
useGETForQueries: true,
uri: apiBase
});
// preserve this array order, it's important
// as the terminating link, `httpLink` must be last
const links = new Map()
.set('MUTATION_QUEUE', mutationQueueLink)
.set('RETRY', retryLink)
.set('GQL_CACHE', gqlCacheLink)
.set('STORE', storeLink)
.set('ERROR', errorLink)
.set('HTTP', httpLink);
return links;
};
export default getLinks;