-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApollo.ts
175 lines (165 loc) · 5.56 KB
/
Apollo.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* The App utils include helper functions for routing, link building and the
* apollo client
* @packageDocumentation
*/
import { ApolloClient, ApolloLink, concat, FieldPolicy, HttpLink, InMemoryCache } from "@apollo/client";
import { createPersistedQueryLink } from "@apollo/client/link/persisted-queries";
import { sha256 } from "crypto-hash";
import possibleTypes from "@coremedia-labs/graphql-layer/dist/__downloaded__/possibleTypes.json";
import log from "loglevel";
import { isPreview } from "../Preview/Preview";
import { getEndpoint } from "./App";
type KeyArgs = FieldPolicy<any>["keyArgs"];
/**
* Global singleton instance of the ApolloClient.
* @category Apollo
*/
let apolloClient: ApolloClient<unknown>;
/**
* @category Apollo
* @internal
*/
const createInMemoryCache = (): InMemoryCache => {
return new InMemoryCache({
possibleTypes,
typePolicies: {
PageGridPlacement: { keyFields: ["id", "name"] },
PageGridRow: { keyFields: ["id", "rowId"] },
Query: {
fields: {
content: {
merge: true,
},
commerce: {
merge: true,
},
searchProducts: offsetLimitPaginationForItems(["categoryId", "filterFacets"]),
},
},
ContentRoot: {
fields: {
search: offsetLimitPagination(["query", "siteId", "sortFields", "customFilterQueries", "docTypes"]),
facetedSearch: offsetLimitPagination([
"query",
"siteId",
"sortFields",
"customFilterQueries",
"facetLimit",
"facetFilters",
]),
},
},
},
});
};
/**
* Creates a new ApolloClient with the given ApolloLink.
* @category Apollo
* @internal
* @param link includes the URI of the GraphQl Endpoint
*/
const createApolloClient = (link: ApolloLink): ApolloClient<unknown> => {
log.info("New Apollo Client created", link);
const cache = createInMemoryCache();
return new ApolloClient({
cache: cache,
link: link,
});
};
/**
* Adds X-Preview-Date header with a given previewDate to the apolloClient.
* @category Apollo
* @internal
*/
const createPreviewMiddleWare = (previewDate: Date): ApolloLink => {
return new ApolloLink((operation, forward) => {
operation.setContext({
headers: {
"X-Preview-Date": previewDate.toUTCString(),
},
});
return forward(operation);
});
};
/**
* Returns the singleton instance of the ApolloClient.
* If no client exist, or previewDate is set,
* a new instance of the client is created.
* @category Apollo
* @param newPreviewDate optional preview date used for Time Travel in CoreMedia Studio Preview
* @param apqEnabled set to true to use APQ via GET requests
*/
export const initializeApollo = (newPreviewDate: Date | undefined, apqEnabled: boolean): ApolloClient<unknown> => {
// Create the Apollo Client once in the client, if not changed or previewDate is set
if (!apolloClient || newPreviewDate) {
let link: ApolloLink = new HttpLink({
uri: getEndpoint(),
});
if (newPreviewDate && isPreview()) {
log.info("Time travel is activated.", newPreviewDate.toUTCString());
const previewMiddleware = createPreviewMiddleWare(newPreviewDate);
link = previewMiddleware ? concat(previewMiddleware, link) : link;
}
if (apqEnabled) {
log.info("Automatic persistent queries are activated.");
const persistedQueriesLink = createPersistedQueryLink({ sha256, useGETForHashedQueries: true });
link = persistedQueriesLink.concat(link);
}
// Set global state
apolloClient = createApolloClient(link);
}
return apolloClient;
};
/**
* A basic field policy that uses options.args.{offset,limit} to splice
* the incoming data into the existing array. If your arguments are called
* something different (like args.{start,count}), feel free to copy/paste
* this implementation and make the appropriate changes.
*
* @param keyArgs
*/
export const offsetLimitPagination = (keyArgs: KeyArgs = false): FieldPolicy => {
return {
keyArgs,
merge(existing, incoming, { args }) {
const merged = existing && existing.result ? existing.result.slice(0) : [];
if (args) {
const { offset = 0 } = args;
for (let i = 0; i < incoming.result.length; ++i) {
merged[offset + i] = incoming.result[i];
}
} else {
// It's unusual (probably a mistake) for a paginated field not
// to receive any arguments, so you might prefer to throw an
// exception here, instead of recovering by appending incoming
// onto the existing array.
// eslint-disable-next-line prefer-spread
merged.push.apply(merged, incoming.result);
}
return { ...incoming, result: merged };
},
};
};
export const offsetLimitPaginationForItems = (keyArgs: KeyArgs = false): FieldPolicy => {
return {
keyArgs,
merge(existing, incoming, { args }) {
const merged = existing && existing.items ? existing.items.slice(0) : [];
if (args) {
const { offset = 0 } = args;
for (let i = 0; i < incoming.items.length; ++i) {
merged[offset + i] = incoming.items[i];
}
} else {
// It's unusual (probably a mistake) for a paginated field not
// to receive any arguments, so you might prefer to throw an
// exception here, instead of recovering by appending incoming
// onto the existing array.
// eslint-disable-next-line prefer-spread
merged.push.apply(merged, incoming.items);
}
return { ...incoming, items: merged };
},
};
};