@apollo/client 3.6.1 → 3.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/apollo-client.cjs +133 -78
- package/apollo-client.cjs.map +1 -1
- package/apollo-client.min.cjs +1 -1
- package/cache/cache.cjs.native.js +2289 -0
- package/core/ApolloClient.d.ts.map +1 -1
- package/core/ApolloClient.js.map +1 -1
- package/core/ObservableQuery.d.ts.map +1 -1
- package/core/ObservableQuery.js +3 -1
- package/core/ObservableQuery.js.map +1 -1
- package/core/core.cjs +4 -2
- package/core/core.cjs.map +1 -1
- package/core/core.cjs.native.js +2141 -0
- package/errors/errors.cjs.native.js +48 -0
- package/invariantErrorCodes.js +1 -1
- package/link/batch/batch.cjs.native.js +161 -0
- package/link/batch-http/batch-http.cjs.native.js +127 -0
- package/link/context/context.cjs.native.js +38 -0
- package/link/core/core.cjs.native.js +121 -0
- package/link/error/error.cjs.native.js +90 -0
- package/link/http/http.cjs.native.js +320 -0
- package/link/http/selectHttpOptionsAndBody.d.ts.map +1 -1
- package/link/http/selectHttpOptionsAndBody.js.map +1 -1
- package/link/persisted-queries/index.d.ts +2 -1
- package/link/persisted-queries/index.d.ts.map +1 -1
- package/link/persisted-queries/index.js +26 -13
- package/link/persisted-queries/index.js.map +1 -1
- package/link/persisted-queries/persisted-queries.cjs +25 -12
- package/link/persisted-queries/persisted-queries.cjs.map +1 -1
- package/link/persisted-queries/persisted-queries.cjs.native.js +174 -0
- package/link/retry/retry.cjs.native.js +170 -0
- package/link/schema/schema.cjs.native.js +56 -0
- package/link/subscriptions/subscriptions.cjs.native.js +45 -0
- package/link/utils/utils.cjs.native.js +115 -0
- package/link/ws/ws.cjs.native.js +28 -0
- package/main.cjs.native.js +16 -0
- package/package.json +17 -18
- package/react/components/components.cjs.native.js +79 -0
- package/react/context/ApolloConsumer.js +2 -2
- package/react/context/ApolloProvider.js +2 -2
- package/react/context/context.cjs +4 -4
- package/react/context/context.cjs.map +1 -1
- package/react/context/context.cjs.native.js +67 -0
- package/react/hoc/hoc.cjs.native.js +325 -0
- package/react/hooks/hooks.cjs +151 -90
- package/react/hooks/hooks.cjs.map +1 -1
- package/react/hooks/hooks.cjs.native.js +604 -0
- package/react/hooks/useLazyQuery.d.ts.map +1 -1
- package/react/hooks/useLazyQuery.js +7 -22
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/react/hooks/useQuery.d.ts +5 -1
- package/react/hooks/useQuery.d.ts.map +1 -1
- package/react/hooks/useQuery.js +46 -39
- package/react/hooks/useQuery.js.map +1 -1
- package/react/hooks/useSubscription.d.ts.map +1 -1
- package/react/hooks/useSubscription.js +17 -7
- package/react/hooks/useSubscription.js.map +1 -1
- package/react/hooks/useSyncExternalStore.d.ts +4 -0
- package/react/hooks/useSyncExternalStore.d.ts.map +1 -0
- package/react/hooks/useSyncExternalStore.js +48 -0
- package/react/hooks/useSyncExternalStore.js.map +1 -0
- package/react/parser/parser.cjs.native.js +103 -0
- package/react/react.cjs.native.js +22 -0
- package/react/ssr/ssr.cjs.native.js +150 -0
- package/testing/core/core.cjs.native.js +288 -0
- package/testing/core/mocking/mockQueryManager.js +1 -1
- package/testing/core/mocking/mockWatchQuery.js +1 -1
- package/testing/core/wrap.js +1 -1
- package/testing/testing.cjs.native.js +58 -0
- package/utilities/common/canUse.d.ts +2 -0
- package/utilities/common/canUse.d.ts.map +1 -1
- package/utilities/common/canUse.js +6 -2
- package/utilities/common/canUse.js.map +1 -1
- package/utilities/common/mergeOptions.d.ts +1 -1
- package/utilities/common/mergeOptions.d.ts.map +1 -1
- package/utilities/common/mergeOptions.js +1 -1
- package/utilities/common/mergeOptions.js.map +1 -1
- package/utilities/globals/global.js +1 -2
- package/utilities/globals/globals.cjs.native.js +56 -0
- package/utilities/observables/Concast.d.ts.map +1 -1
- package/utilities/observables/Concast.js +5 -2
- package/utilities/observables/Concast.js.map +1 -1
- package/utilities/policies/pagination.d.ts.map +1 -1
- package/utilities/policies/pagination.js +9 -7
- package/utilities/policies/pagination.js.map +1 -1
- package/utilities/utilities.cjs +22 -12
- package/utilities/utilities.cjs.map +1 -1
- package/utilities/utilities.cjs.native.js +1284 -0
- package/version.js +1 -1
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var globals = require('../../utilities/globals');
|
|
6
|
+
var utils = require('../utils');
|
|
7
|
+
var tslib = require('tslib');
|
|
8
|
+
var graphql = require('graphql');
|
|
9
|
+
var core = require('../core');
|
|
10
|
+
var utilities = require('../../utilities');
|
|
11
|
+
|
|
12
|
+
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
13
|
+
function parseAndCheckHttpResponse(operations) {
|
|
14
|
+
return function (response) { return response
|
|
15
|
+
.text()
|
|
16
|
+
.then(function (bodyText) {
|
|
17
|
+
try {
|
|
18
|
+
return JSON.parse(bodyText);
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
var parseError = err;
|
|
22
|
+
parseError.name = 'ServerParseError';
|
|
23
|
+
parseError.response = response;
|
|
24
|
+
parseError.statusCode = response.status;
|
|
25
|
+
parseError.bodyText = bodyText;
|
|
26
|
+
throw parseError;
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
.then(function (result) {
|
|
30
|
+
if (response.status >= 300) {
|
|
31
|
+
utils.throwServerError(response, result, "Response not successful: Received status code ".concat(response.status));
|
|
32
|
+
}
|
|
33
|
+
if (!Array.isArray(result) &&
|
|
34
|
+
!hasOwnProperty.call(result, 'data') &&
|
|
35
|
+
!hasOwnProperty.call(result, 'errors')) {
|
|
36
|
+
utils.throwServerError(response, result, "Server response was missing for query '".concat(Array.isArray(operations)
|
|
37
|
+
? operations.map(function (op) { return op.operationName; })
|
|
38
|
+
: operations.operationName, "'."));
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}); };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
var serializeFetchParameter = function (p, label) {
|
|
45
|
+
var serialized;
|
|
46
|
+
try {
|
|
47
|
+
serialized = JSON.stringify(p);
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
var parseError = __DEV__ ? new globals.InvariantError("Network request failed. ".concat(label, " is not serializable: ").concat(e.message)) : new globals.InvariantError(21);
|
|
51
|
+
parseError.parseError = e;
|
|
52
|
+
throw parseError;
|
|
53
|
+
}
|
|
54
|
+
return serialized;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
var defaultHttpOptions = {
|
|
58
|
+
includeQuery: true,
|
|
59
|
+
includeExtensions: false,
|
|
60
|
+
};
|
|
61
|
+
var defaultHeaders = {
|
|
62
|
+
accept: '*/*',
|
|
63
|
+
'content-type': 'application/json',
|
|
64
|
+
};
|
|
65
|
+
var defaultOptions = {
|
|
66
|
+
method: 'POST',
|
|
67
|
+
};
|
|
68
|
+
var fallbackHttpConfig = {
|
|
69
|
+
http: defaultHttpOptions,
|
|
70
|
+
headers: defaultHeaders,
|
|
71
|
+
options: defaultOptions,
|
|
72
|
+
};
|
|
73
|
+
var defaultPrinter = function (ast, printer) { return printer(ast); };
|
|
74
|
+
function selectHttpOptionsAndBody(operation, fallbackConfig) {
|
|
75
|
+
var configs = [];
|
|
76
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
77
|
+
configs[_i - 2] = arguments[_i];
|
|
78
|
+
}
|
|
79
|
+
configs.unshift(fallbackConfig);
|
|
80
|
+
return selectHttpOptionsAndBodyInternal.apply(void 0, tslib.__spreadArray([operation,
|
|
81
|
+
defaultPrinter], configs, false));
|
|
82
|
+
}
|
|
83
|
+
function selectHttpOptionsAndBodyInternal(operation, printer) {
|
|
84
|
+
var configs = [];
|
|
85
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
86
|
+
configs[_i - 2] = arguments[_i];
|
|
87
|
+
}
|
|
88
|
+
var options = {};
|
|
89
|
+
var http = {};
|
|
90
|
+
configs.forEach(function (config) {
|
|
91
|
+
options = tslib.__assign(tslib.__assign(tslib.__assign({}, options), config.options), { headers: tslib.__assign(tslib.__assign({}, options.headers), headersToLowerCase(config.headers)) });
|
|
92
|
+
if (config.credentials) {
|
|
93
|
+
options.credentials = config.credentials;
|
|
94
|
+
}
|
|
95
|
+
http = tslib.__assign(tslib.__assign({}, http), config.http);
|
|
96
|
+
});
|
|
97
|
+
var operationName = operation.operationName, extensions = operation.extensions, variables = operation.variables, query = operation.query;
|
|
98
|
+
var body = { operationName: operationName, variables: variables };
|
|
99
|
+
if (http.includeExtensions)
|
|
100
|
+
body.extensions = extensions;
|
|
101
|
+
if (http.includeQuery)
|
|
102
|
+
body.query = printer(query, graphql.print);
|
|
103
|
+
return {
|
|
104
|
+
options: options,
|
|
105
|
+
body: body,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function headersToLowerCase(headers) {
|
|
109
|
+
if (headers) {
|
|
110
|
+
var normalized_1 = Object.create(null);
|
|
111
|
+
Object.keys(Object(headers)).forEach(function (name) {
|
|
112
|
+
normalized_1[name.toLowerCase()] = headers[name];
|
|
113
|
+
});
|
|
114
|
+
return normalized_1;
|
|
115
|
+
}
|
|
116
|
+
return headers;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
var checkFetcher = function (fetcher) {
|
|
120
|
+
if (!fetcher && typeof fetch === 'undefined') {
|
|
121
|
+
throw __DEV__ ? new globals.InvariantError("\n\"fetch\" has not been found globally and no fetcher has been configured. To fix this, install a fetch package (like https://www.npmjs.com/package/cross-fetch), instantiate the fetcher, and pass it into your HttpLink constructor. For example:\n\nimport fetch from 'cross-fetch';\nimport { ApolloClient, HttpLink } from '@apollo/client';\nconst client = new ApolloClient({\n link: new HttpLink({ uri: '/graphql', fetch })\n});\n ") : new globals.InvariantError(20);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
var createSignalIfSupported = function () {
|
|
126
|
+
if (typeof AbortController === 'undefined')
|
|
127
|
+
return { controller: false, signal: false };
|
|
128
|
+
var controller = new AbortController();
|
|
129
|
+
var signal = controller.signal;
|
|
130
|
+
return { controller: controller, signal: signal };
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
var selectURI = function (operation, fallbackURI) {
|
|
134
|
+
var context = operation.getContext();
|
|
135
|
+
var contextURI = context.uri;
|
|
136
|
+
if (contextURI) {
|
|
137
|
+
return contextURI;
|
|
138
|
+
}
|
|
139
|
+
else if (typeof fallbackURI === 'function') {
|
|
140
|
+
return fallbackURI(operation);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
return fallbackURI || '/graphql';
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
function rewriteURIForGET(chosenURI, body) {
|
|
148
|
+
var queryParams = [];
|
|
149
|
+
var addQueryParam = function (key, value) {
|
|
150
|
+
queryParams.push("".concat(key, "=").concat(encodeURIComponent(value)));
|
|
151
|
+
};
|
|
152
|
+
if ('query' in body) {
|
|
153
|
+
addQueryParam('query', body.query);
|
|
154
|
+
}
|
|
155
|
+
if (body.operationName) {
|
|
156
|
+
addQueryParam('operationName', body.operationName);
|
|
157
|
+
}
|
|
158
|
+
if (body.variables) {
|
|
159
|
+
var serializedVariables = void 0;
|
|
160
|
+
try {
|
|
161
|
+
serializedVariables = serializeFetchParameter(body.variables, 'Variables map');
|
|
162
|
+
}
|
|
163
|
+
catch (parseError) {
|
|
164
|
+
return { parseError: parseError };
|
|
165
|
+
}
|
|
166
|
+
addQueryParam('variables', serializedVariables);
|
|
167
|
+
}
|
|
168
|
+
if (body.extensions) {
|
|
169
|
+
var serializedExtensions = void 0;
|
|
170
|
+
try {
|
|
171
|
+
serializedExtensions = serializeFetchParameter(body.extensions, 'Extensions map');
|
|
172
|
+
}
|
|
173
|
+
catch (parseError) {
|
|
174
|
+
return { parseError: parseError };
|
|
175
|
+
}
|
|
176
|
+
addQueryParam('extensions', serializedExtensions);
|
|
177
|
+
}
|
|
178
|
+
var fragment = '', preFragment = chosenURI;
|
|
179
|
+
var fragmentStart = chosenURI.indexOf('#');
|
|
180
|
+
if (fragmentStart !== -1) {
|
|
181
|
+
fragment = chosenURI.substr(fragmentStart);
|
|
182
|
+
preFragment = chosenURI.substr(0, fragmentStart);
|
|
183
|
+
}
|
|
184
|
+
var queryParamsPrefix = preFragment.indexOf('?') === -1 ? '?' : '&';
|
|
185
|
+
var newURI = preFragment + queryParamsPrefix + queryParams.join('&') + fragment;
|
|
186
|
+
return { newURI: newURI };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
var backupFetch = utilities.maybe(function () { return fetch; });
|
|
190
|
+
var createHttpLink = function (linkOptions) {
|
|
191
|
+
if (linkOptions === void 0) { linkOptions = {}; }
|
|
192
|
+
var _a = linkOptions.uri, uri = _a === void 0 ? '/graphql' : _a, preferredFetch = linkOptions.fetch, _b = linkOptions.print, print = _b === void 0 ? defaultPrinter : _b, includeExtensions = linkOptions.includeExtensions, useGETForQueries = linkOptions.useGETForQueries, _c = linkOptions.includeUnusedVariables, includeUnusedVariables = _c === void 0 ? false : _c, requestOptions = tslib.__rest(linkOptions, ["uri", "fetch", "print", "includeExtensions", "useGETForQueries", "includeUnusedVariables"]);
|
|
193
|
+
if (__DEV__) {
|
|
194
|
+
checkFetcher(preferredFetch || backupFetch);
|
|
195
|
+
}
|
|
196
|
+
var linkConfig = {
|
|
197
|
+
http: { includeExtensions: includeExtensions },
|
|
198
|
+
options: requestOptions.fetchOptions,
|
|
199
|
+
credentials: requestOptions.credentials,
|
|
200
|
+
headers: requestOptions.headers,
|
|
201
|
+
};
|
|
202
|
+
return new core.ApolloLink(function (operation) {
|
|
203
|
+
var chosenURI = selectURI(operation, uri);
|
|
204
|
+
var context = operation.getContext();
|
|
205
|
+
var clientAwarenessHeaders = {};
|
|
206
|
+
if (context.clientAwareness) {
|
|
207
|
+
var _a = context.clientAwareness, name_1 = _a.name, version = _a.version;
|
|
208
|
+
if (name_1) {
|
|
209
|
+
clientAwarenessHeaders['apollographql-client-name'] = name_1;
|
|
210
|
+
}
|
|
211
|
+
if (version) {
|
|
212
|
+
clientAwarenessHeaders['apollographql-client-version'] = version;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
var contextHeaders = tslib.__assign(tslib.__assign({}, clientAwarenessHeaders), context.headers);
|
|
216
|
+
var contextConfig = {
|
|
217
|
+
http: context.http,
|
|
218
|
+
options: context.fetchOptions,
|
|
219
|
+
credentials: context.credentials,
|
|
220
|
+
headers: contextHeaders,
|
|
221
|
+
};
|
|
222
|
+
var _b = selectHttpOptionsAndBodyInternal(operation, print, fallbackHttpConfig, linkConfig, contextConfig), options = _b.options, body = _b.body;
|
|
223
|
+
if (body.variables && !includeUnusedVariables) {
|
|
224
|
+
var unusedNames_1 = new Set(Object.keys(body.variables));
|
|
225
|
+
graphql.visit(operation.query, {
|
|
226
|
+
Variable: function (node, _key, parent) {
|
|
227
|
+
if (parent && parent.kind !== 'VariableDefinition') {
|
|
228
|
+
unusedNames_1.delete(node.name.value);
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
if (unusedNames_1.size) {
|
|
233
|
+
body.variables = tslib.__assign({}, body.variables);
|
|
234
|
+
unusedNames_1.forEach(function (name) {
|
|
235
|
+
delete body.variables[name];
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
var controller;
|
|
240
|
+
if (!options.signal) {
|
|
241
|
+
var _c = createSignalIfSupported(), _controller = _c.controller, signal = _c.signal;
|
|
242
|
+
controller = _controller;
|
|
243
|
+
if (controller)
|
|
244
|
+
options.signal = signal;
|
|
245
|
+
}
|
|
246
|
+
var definitionIsMutation = function (d) {
|
|
247
|
+
return d.kind === 'OperationDefinition' && d.operation === 'mutation';
|
|
248
|
+
};
|
|
249
|
+
if (useGETForQueries &&
|
|
250
|
+
!operation.query.definitions.some(definitionIsMutation)) {
|
|
251
|
+
options.method = 'GET';
|
|
252
|
+
}
|
|
253
|
+
if (options.method === 'GET') {
|
|
254
|
+
var _d = rewriteURIForGET(chosenURI, body), newURI = _d.newURI, parseError = _d.parseError;
|
|
255
|
+
if (parseError) {
|
|
256
|
+
return utils.fromError(parseError);
|
|
257
|
+
}
|
|
258
|
+
chosenURI = newURI;
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
try {
|
|
262
|
+
options.body = serializeFetchParameter(body, 'Payload');
|
|
263
|
+
}
|
|
264
|
+
catch (parseError) {
|
|
265
|
+
return utils.fromError(parseError);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return new utilities.Observable(function (observer) {
|
|
269
|
+
var currentFetch = preferredFetch || utilities.maybe(function () { return fetch; }) || backupFetch;
|
|
270
|
+
currentFetch(chosenURI, options)
|
|
271
|
+
.then(function (response) {
|
|
272
|
+
operation.setContext({ response: response });
|
|
273
|
+
return response;
|
|
274
|
+
})
|
|
275
|
+
.then(parseAndCheckHttpResponse(operation))
|
|
276
|
+
.then(function (result) {
|
|
277
|
+
observer.next(result);
|
|
278
|
+
observer.complete();
|
|
279
|
+
return result;
|
|
280
|
+
})
|
|
281
|
+
.catch(function (err) {
|
|
282
|
+
if (err.name === 'AbortError')
|
|
283
|
+
return;
|
|
284
|
+
if (err.result && err.result.errors && err.result.data) {
|
|
285
|
+
observer.next(err.result);
|
|
286
|
+
}
|
|
287
|
+
observer.error(err);
|
|
288
|
+
});
|
|
289
|
+
return function () {
|
|
290
|
+
if (controller)
|
|
291
|
+
controller.abort();
|
|
292
|
+
};
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
var HttpLink = (function (_super) {
|
|
298
|
+
tslib.__extends(HttpLink, _super);
|
|
299
|
+
function HttpLink(options) {
|
|
300
|
+
if (options === void 0) { options = {}; }
|
|
301
|
+
var _this = _super.call(this, createHttpLink(options).request) || this;
|
|
302
|
+
_this.options = options;
|
|
303
|
+
return _this;
|
|
304
|
+
}
|
|
305
|
+
return HttpLink;
|
|
306
|
+
}(core.ApolloLink));
|
|
307
|
+
|
|
308
|
+
exports.HttpLink = HttpLink;
|
|
309
|
+
exports.checkFetcher = checkFetcher;
|
|
310
|
+
exports.createHttpLink = createHttpLink;
|
|
311
|
+
exports.createSignalIfSupported = createSignalIfSupported;
|
|
312
|
+
exports.defaultPrinter = defaultPrinter;
|
|
313
|
+
exports.fallbackHttpConfig = fallbackHttpConfig;
|
|
314
|
+
exports.parseAndCheckHttpResponse = parseAndCheckHttpResponse;
|
|
315
|
+
exports.rewriteURIForGET = rewriteURIForGET;
|
|
316
|
+
exports.selectHttpOptionsAndBody = selectHttpOptionsAndBody;
|
|
317
|
+
exports.selectHttpOptionsAndBodyInternal = selectHttpOptionsAndBodyInternal;
|
|
318
|
+
exports.selectURI = selectURI;
|
|
319
|
+
exports.serializeFetchParameter = serializeFetchParameter;
|
|
320
|
+
//# sourceMappingURL=http.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selectHttpOptionsAndBody.d.ts","sourceRoot":"","sources":["../../../src/link/http/selectHttpOptionsAndBody.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEpC,MAAM,WAAW,OAAO;IACtB,CAAC,IAAI,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,KAAK,GAAG,MAAM,CAAA;CACrD;AAED,MAAM,WAAW,WAAW;IAC1B,CAAC,SAAS,EAAE,SAAS,GAAG,MAAM,CAAC;CAChC;AAED,MAAM,WAAW,IAAI;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAA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../src/link/http/selectHttpOptionsAndBody.ts"],"names":[],"mappings":";AAAA,OAAO,EAAW,KAAK,EAAE,MAAM,SAAS,CAAC;AAMxC,CAAC;AAoFF,IAAM,kBAAkB,GAAqB;IAC3C,YAAY,EAAE,IAAI;IAClB,iBAAiB,EAAE,KAAK;CACzB,CAAC;AAEF,IAAM,cAAc,GAAG;IAErB,MAAM,EAAE,KAAK;
|
|
1
|
+
{"version":3,"file":"selectHttpOptionsAndBody.js","sourceRoot":"","sources":["../../../src/link/http/selectHttpOptionsAndBody.ts"],"names":[],"mappings":";AAAA,OAAO,EAAW,KAAK,EAAE,MAAM,SAAS,CAAC;AAMxC,CAAC;AAoFF,IAAM,kBAAkB,GAAqB;IAC3C,YAAY,EAAE,IAAI;IAClB,iBAAiB,EAAE,KAAK;CACzB,CAAC;AAEF,IAAM,cAAc,GAAG;IAErB,MAAM,EAAE,KAAK;IAab,cAAc,EAAE,kBAAkB;CACnC,CAAC;AAEF,IAAM,cAAc,GAAG;IACrB,MAAM,EAAE,MAAM;CACf,CAAC;AAEF,MAAM,CAAC,IAAM,kBAAkB,GAAG;IAChC,IAAI,EAAE,kBAAkB;IACxB,OAAO,EAAE,cAAc;IACvB,OAAO,EAAE,cAAc;CACxB,CAAC;AAEF,MAAM,CAAC,IAAM,cAAc,GAAY,UAAC,GAAG,EAAE,OAAO,IAAK,OAAA,OAAO,CAAC,GAAG,CAAC,EAAZ,CAAY,CAAC;AAEtE,MAAM,UAAU,wBAAwB,CACtC,SAAoB,EACpB,cAA0B;IAC1B,iBAA6B;SAA7B,UAA6B,EAA7B,qBAA6B,EAA7B,IAA6B;QAA7B,gCAA6B;;IAE7B,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;IAChC,OAAO,gCAAgC,8BACrC,SAAS;QACT,cAAc,GACX,OAAO,UACV;AACJ,CAAC;AAED,MAAM,UAAU,gCAAgC,CAC9C,SAAoB,EACpB,OAAgB;IAChB,iBAAwB;SAAxB,UAAwB,EAAxB,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AAC,CAAC;QACjD,CAAC,CAAC,CAAC;QACH,OAAO,YAAU,CAAC;KACnB;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { ASTNode, print } from 'graphql';\n\nimport { Operation } from '../core';\n\nexport interface Printer {\n (node: ASTNode, originalPrint: typeof print): string\n};\n\nexport interface UriFunction {\n (operation: Operation): string;\n}\n\nexport interface Body {\n query?: string;\n operationName?: string;\n variables?: Record<string, any>;\n extensions?: Record<string, any>;\n}\n\nexport interface HttpOptions {\n /**\n * The URI to use when fetching operations.\n *\n * Defaults to '/graphql'.\n */\n uri?: string | UriFunction;\n\n /**\n * Passes the extensions field to your graphql server.\n *\n * Defaults to false.\n */\n includeExtensions?: boolean;\n\n /**\n * A `fetch`-compatible API to use when making requests.\n */\n fetch?: WindowOrWorkerGlobalScope['fetch'];\n\n /**\n * An object representing values to be sent as headers on the request.\n */\n headers?: any;\n\n /**\n * The credentials policy you want to use for the fetch call.\n */\n credentials?: string;\n\n /**\n * Any overrides of the fetch options argument to pass to the fetch call.\n */\n fetchOptions?: any;\n\n /**\n * If set to true, use the HTTP GET method for query operations. Mutations\n * will still use the method specified in fetchOptions.method (which defaults\n * to POST).\n */\n useGETForQueries?: boolean;\n\n /**\n * If set to true, the default behavior of stripping unused variables\n * from the request will be disabled.\n *\n * Unused variables are likely to trigger server-side validation errors,\n * per https://spec.graphql.org/draft/#sec-All-Variables-Used, but this\n * includeUnusedVariables option can be useful if your server deviates\n * from the GraphQL specification by not strictly enforcing that rule.\n */\n includeUnusedVariables?: boolean;\n /**\n * A function to substitute for the default query print function. Can be\n * used to apply changes to the results of the print function.\n */\n print?: Printer;\n}\n\nexport interface HttpQueryOptions {\n includeQuery?: boolean;\n includeExtensions?: boolean;\n}\n\nexport interface HttpConfig {\n http?: HttpQueryOptions;\n options?: any;\n headers?: any;\n credentials?: any;\n}\n\nconst defaultHttpOptions: HttpQueryOptions = {\n includeQuery: true,\n includeExtensions: false,\n};\n\nconst defaultHeaders = {\n // headers are case insensitive (https://stackoverflow.com/a/5259004)\n accept: '*/*',\n // The content-type header describes the type of the body of the request, and\n // so it typically only is sent with requests that actually have bodies. One\n // could imagine that Apollo Client would remove this header when constructing\n // a GET request (which has no body), but we historically have not done that.\n // This means that browsers will preflight all Apollo Client requests (even\n // GET requests). Apollo Server's CSRF prevention feature (introduced in\n // AS3.7) takes advantage of this fact and does not block requests with this\n // header. If you want to drop this header from GET requests, then you should\n // probably replace it with a `apollo-require-preflight` header, or servers\n // with CSRF prevention enabled might block your GET request. See\n // https://www.apollographql.com/docs/apollo-server/security/cors/#preventing-cross-site-request-forgery-csrf\n // for more details.\n 'content-type': 'application/json',\n};\n\nconst defaultOptions = {\n method: 'POST',\n};\n\nexport const fallbackHttpConfig = {\n http: defaultHttpOptions,\n headers: defaultHeaders,\n options: defaultOptions,\n};\n\nexport const defaultPrinter: Printer = (ast, printer) => printer(ast);\n\nexport function selectHttpOptionsAndBody(\n operation: Operation,\n fallbackConfig: HttpConfig,\n ...configs: Array<HttpConfig>\n) {\n configs.unshift(fallbackConfig);\n return selectHttpOptionsAndBodyInternal(\n operation,\n defaultPrinter,\n ...configs,\n );\n}\n\nexport function selectHttpOptionsAndBodyInternal(\n operation: Operation,\n printer: Printer,\n ...configs: HttpConfig[]\n) {\n let options = {} as HttpConfig & Record<string, any>;\n let http = {} as HttpQueryOptions;\n\n configs.forEach(config => {\n options = {\n ...options,\n ...config.options,\n headers: {\n ...options.headers,\n ...headersToLowerCase(config.headers),\n },\n };\n\n if (config.credentials) {\n options.credentials = config.credentials;\n }\n\n http = {\n ...http,\n ...config.http,\n };\n });\n\n //The body depends on the http options\n const { operationName, extensions, variables, query } = operation;\n const body: Body = { operationName, variables };\n\n if (http.includeExtensions) (body as any).extensions = extensions;\n\n // not sending the query (i.e persisted queries)\n if (http.includeQuery) (body as any).query = printer(query, print);\n\n return {\n options,\n body,\n };\n};\n\nfunction headersToLowerCase(\n headers: Record<string, string> | undefined\n): typeof headers {\n if (headers) {\n const normalized = Object.create(null);\n Object.keys(Object(headers)).forEach(name => {\n normalized[name.toLowerCase()] = headers[name];\n });\n return normalized;\n }\n return headers;\n}\n"]}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { DocumentNode, ExecutionResult, GraphQLError } from 'graphql';
|
|
2
2
|
import { ApolloLink, Operation } from '../core';
|
|
3
|
+
import { NetworkError } from '../../errors';
|
|
3
4
|
export declare const VERSION = 1;
|
|
4
5
|
export interface ErrorResponse {
|
|
5
6
|
graphQLErrors?: readonly GraphQLError[];
|
|
6
|
-
networkError?:
|
|
7
|
+
networkError?: NetworkError;
|
|
7
8
|
response?: ExecutionResult;
|
|
8
9
|
operation: Operation;
|
|
9
10
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/link/persisted-queries/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,YAAY,EACZ,eAAe,EACf,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAQhD,eAAO,MAAM,OAAO,IAAI,CAAC;AAEzB,MAAM,WAAW,aAAa;IAC5B,aAAa,CAAC,EAAE,SAAS,YAAY,EAAE,CAAC;IACxC,YAAY,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/link/persisted-queries/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,YAAY,EACZ,eAAe,EACf,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAQhD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C,eAAO,MAAM,OAAO,IAAI,CAAC;AAEzB,MAAM,WAAW,aAAa;IAC5B,aAAa,CAAC,EAAE,SAAS,YAAY,EAAE,CAAC;IACxC,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,EAAE,eAAe,CAAC;IAC3B,SAAS,EAAE,SAAS,CAAC;CACtB;AAED,aAAK,cAAc,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;AACvE,aAAK,oBAAoB,GAAG,CAAC,QAAQ,EAAE,YAAY,KAAK,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;AAErF,yBAAiB,kBAAkB,CAAC;IAClC,UAAU,WAAW;QACnB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,OAAO,CAAC;QAC5C,sBAAsB,CAAC,EAAE,OAAO,CAAC;KAClC;IAED,UAAU,aAAc,SAAQ,WAAW;QACzC,MAAM,EAAE,cAAc,CAAC;QACvB,YAAY,CAAC,EAAE,KAAK,CAAC;KACtB;IAED,UAAU,mBAAoB,SAAQ,WAAW;QAC/C,MAAM,CAAC,EAAE,KAAK,CAAC;QACf,YAAY,EAAE,oBAAoB,CAAC;KACpC;IAED,MAAM,MAAM,OAAO,GAAG,aAAa,GAAG,mBAAmB,CAAC;;CAC3D;AAuDD,eAAO,MAAM,wBAAwB,YAC1B,mBAAmB,OAAO,eA4LpC,CAAC"}
|
|
@@ -2,7 +2,7 @@ import { __assign } from "tslib";
|
|
|
2
2
|
import { invariant } from "../../utilities/globals/index.js";
|
|
3
3
|
import { print } from 'graphql';
|
|
4
4
|
import { ApolloLink } from "../core/index.js";
|
|
5
|
-
import { Observable, compact, } from "../../utilities/index.js";
|
|
5
|
+
import { Observable, compact, isNonEmptyArray, } from "../../utilities/index.js";
|
|
6
6
|
export var VERSION = 1;
|
|
7
7
|
export var PersistedQueryLink;
|
|
8
8
|
(function (PersistedQueryLink) {
|
|
@@ -10,16 +10,23 @@ export var PersistedQueryLink;
|
|
|
10
10
|
;
|
|
11
11
|
;
|
|
12
12
|
})(PersistedQueryLink || (PersistedQueryLink = {}));
|
|
13
|
+
function collectErrorsByMessage(graphQLErrors) {
|
|
14
|
+
var collected = Object.create(null);
|
|
15
|
+
if (isNonEmptyArray(graphQLErrors)) {
|
|
16
|
+
graphQLErrors.forEach(function (error) { return collected[error.message] = error; });
|
|
17
|
+
}
|
|
18
|
+
return collected;
|
|
19
|
+
}
|
|
13
20
|
var defaultOptions = {
|
|
14
21
|
disable: function (_a) {
|
|
15
22
|
var graphQLErrors = _a.graphQLErrors, operation = _a.operation;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var message = _a.message;
|
|
19
|
-
return message === 'PersistedQueryNotSupported';
|
|
20
|
-
})) {
|
|
23
|
+
var errorMessages = collectErrorsByMessage(graphQLErrors);
|
|
24
|
+
if (errorMessages.PersistedQueryNotSupported) {
|
|
21
25
|
return true;
|
|
22
26
|
}
|
|
27
|
+
if (errorMessages.PersistedQueryNotFound) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
23
30
|
var response = operation.getContext().response;
|
|
24
31
|
if (response &&
|
|
25
32
|
response.status &&
|
|
@@ -73,19 +80,25 @@ export var createPersistedQueryLink = function (options) {
|
|
|
73
80
|
var response = _a.response, networkError = _a.networkError;
|
|
74
81
|
if (!retried && ((response && response.errors) || networkError)) {
|
|
75
82
|
retried = true;
|
|
83
|
+
var graphQLErrors = [];
|
|
84
|
+
var responseErrors = response && response.errors;
|
|
85
|
+
if (isNonEmptyArray(responseErrors)) {
|
|
86
|
+
graphQLErrors.push.apply(graphQLErrors, responseErrors);
|
|
87
|
+
}
|
|
88
|
+
var networkErrors = networkError &&
|
|
89
|
+
networkError.result &&
|
|
90
|
+
networkError.result.errors;
|
|
91
|
+
if (isNonEmptyArray(networkErrors)) {
|
|
92
|
+
graphQLErrors.push.apply(graphQLErrors, networkErrors);
|
|
93
|
+
}
|
|
76
94
|
var disablePayload = {
|
|
77
95
|
response: response,
|
|
78
96
|
networkError: networkError,
|
|
79
97
|
operation: operation,
|
|
80
|
-
graphQLErrors:
|
|
98
|
+
graphQLErrors: isNonEmptyArray(graphQLErrors) ? graphQLErrors : void 0,
|
|
81
99
|
};
|
|
82
100
|
supportsPersistedQueries = !disable(disablePayload);
|
|
83
|
-
if ((
|
|
84
|
-
response.errors &&
|
|
85
|
-
response.errors.some(function (_a) {
|
|
86
|
-
var message = _a.message;
|
|
87
|
-
return message === 'PersistedQueryNotFound';
|
|
88
|
-
})) ||
|
|
101
|
+
if (collectErrorsByMessage(graphQLErrors).PersistedQueryNotFound ||
|
|
89
102
|
!supportsPersistedQueries) {
|
|
90
103
|
if (subscription)
|
|
91
104
|
subscription.unsubscribe();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/link/persisted-queries/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAOhC,OAAO,EAAE,UAAU,EAAa,MAAM,SAAS,CAAC;AAChD,OAAO,EACL,UAAU,EAGV,OAAO,GACR,MAAM,iBAAiB,CAAC;AAEzB,MAAM,CAAC,IAAM,OAAO,GAAG,CAAC,CAAC;AAYzB,MAAM,KAAW,kBAAkB,CAiBlC;AAjBD,WAAiB,kBAAkB;IAIhC,CAAC;IAKD,CAAC;IAKD,CAAC;AAGJ,CAAC,EAjBgB,kBAAkB,KAAlB,kBAAkB,QAiBlC;AAED,IAAM,cAAc,GAAG;IACrB,OAAO,EAAE,UAAC,EAA2C;YAAzC,aAAa,mBAAA,EAAE,SAAS,eAAA;QAElC,IACE,aAAa;YACb,aAAa,CAAC,IAAI,CAChB,UAAC,EAAW;oBAAT,OAAO,aAAA;gBAAO,OAAA,OAAO,KAAK,4BAA4B;YAAxC,CAAwC,CAC1D,EACD;YACA,OAAO,IAAI,CAAC;SACb;QAEO,IAAA,QAAQ,GAAK,SAAS,CAAC,UAAU,EAAE,SAA3B,CAA4B;QAG5C,IACE,QAAQ;YACR,QAAQ,CAAC,MA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ities/globals';\n\nimport { print } from 'graphql';\nimport {\n DocumentNode,\n ExecutionResult,\n GraphQLError,\n} from 'graphql';\n\nimport { ApolloLink, Operation } from '../core';\nimport {\n Observable,\n Observer,\n ObservableSubscription,\n compact,\n} from '../../utilities';\n\nexport const VERSION = 1;\n\nexport interface ErrorResponse {\n graphQLErrors?: readonly GraphQLError[];\n networkError?: Error;\n response?: ExecutionResult;\n operation: Operation;\n}\n\ntype SHA256Function = (...args: any[]) => string | PromiseLike<string>;\ntype GenerateHashFunction = (document: DocumentNode) => string | PromiseLike<string>;\n\nexport namespace PersistedQueryLink {\n interface BaseOptions {\n disable?: (error: ErrorResponse) => boolean;\n useGETForHashedQueries?: boolean;\n };\n\n interface SHA256Options extends BaseOptions {\n sha256: SHA256Function;\n generateHash?: never;\n };\n\n interface GenerateHashOptions extends BaseOptions {\n sha256?: never;\n generateHash: GenerateHashFunction;\n };\n\n export type Options = SHA256Options | GenerateHashOptions;\n}\n\nconst defaultOptions = {\n disable: ({ graphQLErrors, operation }: ErrorResponse) => {\n // if the server doesn't support persisted queries, don't try anymore\n if (\n graphQLErrors &&\n graphQLErrors.some(\n ({ message }) => message === 'PersistedQueryNotSupported',\n )\n ) {\n return true;\n }\n\n const { response } = operation.getContext();\n // if the server responds with bad request\n // apollo-server responds with 400 for GET and 500 for POST when no query is found\n if (\n response &&\n response.status &&\n (response.status === 400 || response.status === 500)\n ) {\n return true;\n }\n\n return false;\n },\n useGETForHashedQueries: false,\n};\n\nfunction operationDefinesMutation(operation: Operation) {\n return operation.query.definitions.some(\n d => d.kind === 'OperationDefinition' && d.operation === 'mutation');\n}\n\nconst { hasOwnProperty } = Object.prototype;\n\nconst hashesByQuery = new WeakMap<\n DocumentNode,\n Record<string, Promise<string>>\n>();\n\nlet nextHashesChildKey = 0;\n\nexport const createPersistedQueryLink = (\n options: PersistedQueryLink.Options,\n) => {\n // Ensure a SHA-256 hash function is provided, if a custom hash\n // generation function is not provided. We don't supply a SHA-256 hash\n // function by default, to avoid forcing one as a dependency. Developers\n // should pick the most appropriate SHA-256 function (sync or async) for\n // their needs/environment, or provide a fully custom hash generation\n // function (via the `generateHash` option) if they want to handle\n // hashing with something other than SHA-256.\n invariant(\n options && (\n typeof options.sha256 === 'function' ||\n typeof options.generateHash === 'function'\n ),\n 'Missing/invalid \"sha256\" or \"generateHash\" function. Please ' +\n 'configure one using the \"createPersistedQueryLink(options)\" options ' +\n 'parameter.'\n );\n\n const {\n sha256,\n // If both a `sha256` and `generateHash` option are provided, the\n // `sha256` option will be ignored. Developers can configure and\n // use any hashing approach they want in a custom `generateHash`\n // function; they aren't limited to SHA-256.\n generateHash = (query: DocumentNode) =>\n Promise.resolve<string>(sha256!(print(query))),\n disable,\n useGETForHashedQueries\n } = compact(defaultOptions, options);\n\n let supportsPersistedQueries = true;\n\n const hashesChildKey = 'forLink' + nextHashesChildKey++;\n\n const getHashPromise = (query: DocumentNode) =>\n new Promise<string>(resolve => resolve(generateHash(query)));\n\n function getQueryHash(query: DocumentNode): Promise<string> {\n if (!query || typeof query !== 'object') {\n // If the query is not an object, we won't be able to store its hash as\n // a property of query[hashesKey], so we let generateHash(query) decide\n // what to do with the bogus query.\n return getHashPromise(query);\n }\n let hashes = hashesByQuery.get(query)!;\n if (!hashes) hashesByQuery.set(query, hashes = Object.create(null));\n return hasOwnProperty.call(hashes, hashesChildKey)\n ? hashes[hashesChildKey]\n : hashes[hashesChildKey] = getHashPromise(query);\n }\n\n return new ApolloLink((operation, forward) => {\n invariant(\n forward,\n 'PersistedQueryLink cannot be the last link in the chain.'\n );\n\n const { query } = operation;\n\n return new Observable((observer: Observer<ExecutionResult>) => {\n let subscription: ObservableSubscription;\n let retried = false;\n let originalFetchOptions: any;\n let setFetchOptions = false;\n const retry = (\n {\n response,\n networkError,\n }: { response?: ExecutionResult; networkError?: Error },\n cb: () => void,\n ) => {\n if (!retried && ((response && response.errors) || networkError)) {\n retried = true;\n\n const disablePayload = {\n response,\n networkError,\n operation,\n graphQLErrors: response ? response.errors : undefined,\n };\n\n // if the server doesn't support persisted queries, don't try anymore\n supportsPersistedQueries = !disable(disablePayload);\n\n // if its not found, we can try it again, otherwise just report the error\n if (\n (response &&\n response.errors &&\n response.errors.some(\n ({ message }: { message: string }) =>\n message === 'PersistedQueryNotFound',\n )) ||\n !supportsPersistedQueries\n ) {\n // need to recall the link chain\n if (subscription) subscription.unsubscribe();\n // actually send the query this time\n operation.setContext({\n http: {\n includeQuery: true,\n includeExtensions: supportsPersistedQueries,\n },\n fetchOptions: {\n // Since we're including the full query, which may be\n // large, we should send it in the body of a POST request.\n // See issue #7456.\n method: 'POST',\n },\n });\n if (setFetchOptions) {\n operation.setContext({ fetchOptions: originalFetchOptions });\n }\n subscription = forward(operation).subscribe(handler);\n\n return;\n }\n }\n cb();\n };\n const handler = {\n next: (response: ExecutionResult) => {\n retry({ response }, () => observer.next!(response));\n },\n error: (networkError: Error) => {\n retry({ networkError }, () => observer.error!(networkError));\n },\n complete: observer.complete!.bind(observer),\n };\n\n // don't send the query the first time\n operation.setContext({\n http: {\n includeQuery: !supportsPersistedQueries,\n includeExtensions: supportsPersistedQueries,\n },\n });\n\n // If requested, set method to GET if there are no mutations. Remember the\n // original fetchOptions so we can restore them if we fall back to a\n // non-hashed request.\n if (\n useGETForHashedQueries &&\n supportsPersistedQueries &&\n !operationDefinesMutation(operation)\n ) {\n operation.setContext(\n ({ fetchOptions = {} }: { fetchOptions: Record<string, any> }) => {\n originalFetchOptions = fetchOptions;\n return {\n fetchOptions: {\n ...fetchOptions,\n method: 'GET',\n },\n };\n },\n );\n setFetchOptions = true;\n }\n\n if (supportsPersistedQueries) {\n getQueryHash(query).then((sha256Hash) => {\n operation.extensions.persistedQuery = {\n version: VERSION,\n sha256Hash,\n };\n subscription = forward(operation).subscribe(handler);\n }).catch(observer.error!.bind(observer));;\n } else {\n subscription = forward(operation).subscribe(handler);\n }\n\n return () => {\n if (subscription) subscription.unsubscribe();\n };\n });\n });\n};\n"]}
|
|
1
|
+
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AM;gBACL,YAAY,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;aACtD;YAED,OAAO;gBACL,IAAI,YAAY;oBAAE,YAAY,CAAC,WAAW,EAAE,CAAC;YAC/C,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC","sourcesContent":["import { invariant } from '../../utilities/globals';\n\nimport { print } from 'graphql';\nimport {\n DocumentNode,\n ExecutionResult,\n GraphQLError,\n} from 'graphql';\n\nimport { ApolloLink, Operation } from '../core';\nimport {\n Observable,\n Observer,\n ObservableSubscription,\n compact,\n isNonEmptyArray,\n} from '../../utilities';\nimport { NetworkError } from '../../errors';\nimport { ServerError } from '../utils';\n\nexport const VERSION = 1;\n\nexport interface ErrorResponse {\n graphQLErrors?: readonly GraphQLError[];\n networkError?: NetworkError;\n response?: ExecutionResult;\n operation: Operation;\n}\n\ntype SHA256Function = (...args: any[]) => string | PromiseLike<string>;\ntype GenerateHashFunction = (document: DocumentNode) => string | PromiseLike<string>;\n\nexport namespace PersistedQueryLink {\n interface BaseOptions {\n disable?: (error: ErrorResponse) => boolean;\n useGETForHashedQueries?: boolean;\n };\n\n interface SHA256Options extends BaseOptions {\n sha256: SHA256Function;\n generateHash?: never;\n };\n\n interface GenerateHashOptions extends BaseOptions {\n sha256?: never;\n generateHash: GenerateHashFunction;\n };\n\n export type Options = SHA256Options | GenerateHashOptions;\n}\n\nfunction collectErrorsByMessage<TError extends Error>(\n graphQLErrors: TError[] | readonly TError[] | undefined,\n): Record<string, TError> {\n const collected: Record<string, TError> = Object.create(null);\n if (isNonEmptyArray(graphQLErrors)) {\n graphQLErrors.forEach(error => collected[error.message] = error);\n }\n return collected;\n}\n\nconst defaultOptions = {\n disable: ({ graphQLErrors, operation }: ErrorResponse) => {\n const errorMessages = collectErrorsByMessage(graphQLErrors);\n\n // if the server doesn't support persisted queries, don't try anymore\n if (errorMessages.PersistedQueryNotSupported) {\n return true;\n }\n\n if (errorMessages.PersistedQueryNotFound) {\n return false;\n }\n\n const { response } = operation.getContext();\n // if the server responds with bad request\n // apollo-server responds with 400 for GET and 500 for POST when no query is found\n if (\n response &&\n response.status &&\n (response.status === 400 || response.status === 500)\n ) {\n return true;\n }\n\n return false;\n },\n useGETForHashedQueries: false,\n};\n\nfunction operationDefinesMutation(operation: Operation) {\n return operation.query.definitions.some(\n d => d.kind === 'OperationDefinition' && d.operation === 'mutation');\n}\n\nconst { hasOwnProperty } = Object.prototype;\n\nconst hashesByQuery = new WeakMap<\n DocumentNode,\n Record<string, Promise<string>>\n>();\n\nlet nextHashesChildKey = 0;\n\nexport const createPersistedQueryLink = (\n options: PersistedQueryLink.Options,\n) => {\n // Ensure a SHA-256 hash function is provided, if a custom hash\n // generation function is not provided. We don't supply a SHA-256 hash\n // function by default, to avoid forcing one as a dependency. Developers\n // should pick the most appropriate SHA-256 function (sync or async) for\n // their needs/environment, or provide a fully custom hash generation\n // function (via the `generateHash` option) if they want to handle\n // hashing with something other than SHA-256.\n invariant(\n options && (\n typeof options.sha256 === 'function' ||\n typeof options.generateHash === 'function'\n ),\n 'Missing/invalid \"sha256\" or \"generateHash\" function. Please ' +\n 'configure one using the \"createPersistedQueryLink(options)\" options ' +\n 'parameter.'\n );\n\n const {\n sha256,\n // If both a `sha256` and `generateHash` option are provided, the\n // `sha256` option will be ignored. Developers can configure and\n // use any hashing approach they want in a custom `generateHash`\n // function; they aren't limited to SHA-256.\n generateHash = (query: DocumentNode) =>\n Promise.resolve<string>(sha256!(print(query))),\n disable,\n useGETForHashedQueries\n } = compact(defaultOptions, options);\n\n let supportsPersistedQueries = true;\n\n const hashesChildKey = 'forLink' + nextHashesChildKey++;\n\n const getHashPromise = (query: DocumentNode) =>\n new Promise<string>(resolve => resolve(generateHash(query)));\n\n function getQueryHash(query: DocumentNode): Promise<string> {\n if (!query || typeof query !== 'object') {\n // If the query is not an object, we won't be able to store its hash as\n // a property of query[hashesKey], so we let generateHash(query) decide\n // what to do with the bogus query.\n return getHashPromise(query);\n }\n let hashes = hashesByQuery.get(query)!;\n if (!hashes) hashesByQuery.set(query, hashes = Object.create(null));\n return hasOwnProperty.call(hashes, hashesChildKey)\n ? hashes[hashesChildKey]\n : hashes[hashesChildKey] = getHashPromise(query);\n }\n\n return new ApolloLink((operation, forward) => {\n invariant(\n forward,\n 'PersistedQueryLink cannot be the last link in the chain.'\n );\n\n const { query } = operation;\n\n return new Observable((observer: Observer<ExecutionResult>) => {\n let subscription: ObservableSubscription;\n let retried = false;\n let originalFetchOptions: any;\n let setFetchOptions = false;\n const retry = (\n {\n response,\n networkError,\n }: { response?: ExecutionResult; networkError?: ServerError },\n cb: () => void,\n ) => {\n if (!retried && ((response && response.errors) || networkError)) {\n retried = true;\n\n const graphQLErrors: GraphQLError[] = [];\n\n const responseErrors = response && response.errors;\n if (isNonEmptyArray(responseErrors)) {\n graphQLErrors.push(...responseErrors);\n }\n\n // Network errors can return GraphQL errors on for example a 403\n const networkErrors =\n networkError &&\n networkError.result &&\n networkError.result.errors as GraphQLError[];\n if (isNonEmptyArray(networkErrors)) {\n graphQLErrors.push(...networkErrors);\n }\n\n const disablePayload = {\n response,\n networkError,\n operation,\n graphQLErrors: isNonEmptyArray(graphQLErrors) ? graphQLErrors : void 0,\n };\n\n // if the server doesn't support persisted queries, don't try anymore\n supportsPersistedQueries = !disable(disablePayload);\n\n // if its not found, we can try it again, otherwise just report the error\n if (\n collectErrorsByMessage(graphQLErrors).PersistedQueryNotFound ||\n !supportsPersistedQueries\n ) {\n // need to recall the link chain\n if (subscription) subscription.unsubscribe();\n // actually send the query this time\n operation.setContext({\n http: {\n includeQuery: true,\n includeExtensions: supportsPersistedQueries,\n },\n fetchOptions: {\n // Since we're including the full query, which may be\n // large, we should send it in the body of a POST request.\n // See issue #7456.\n method: 'POST',\n },\n });\n if (setFetchOptions) {\n operation.setContext({ fetchOptions: originalFetchOptions });\n }\n subscription = forward(operation).subscribe(handler);\n\n return;\n }\n }\n cb();\n };\n const handler = {\n next: (response: ExecutionResult) => {\n retry({ response }, () => observer.next!(response));\n },\n error: (networkError: ServerError) => {\n retry({ networkError }, () => observer.error!(networkError));\n },\n complete: observer.complete!.bind(observer),\n };\n\n // don't send the query the first time\n operation.setContext({\n http: {\n includeQuery: !supportsPersistedQueries,\n includeExtensions: supportsPersistedQueries,\n },\n });\n\n // If requested, set method to GET if there are no mutations. Remember the\n // original fetchOptions so we can restore them if we fall back to a\n // non-hashed request.\n if (\n useGETForHashedQueries &&\n supportsPersistedQueries &&\n !operationDefinesMutation(operation)\n ) {\n operation.setContext(\n ({ fetchOptions = {} }: { fetchOptions: Record<string, any> }) => {\n originalFetchOptions = fetchOptions;\n return {\n fetchOptions: {\n ...fetchOptions,\n method: 'GET',\n },\n };\n },\n );\n setFetchOptions = true;\n }\n\n if (supportsPersistedQueries) {\n getQueryHash(query).then((sha256Hash) => {\n operation.extensions.persistedQuery = {\n version: VERSION,\n sha256Hash,\n };\n subscription = forward(operation).subscribe(handler);\n }).catch(observer.error!.bind(observer));;\n } else {\n subscription = forward(operation).subscribe(handler);\n }\n\n return () => {\n if (subscription) subscription.unsubscribe();\n };\n });\n });\n};\n"]}
|
|
@@ -12,16 +12,23 @@ var VERSION = 1;
|
|
|
12
12
|
exports.PersistedQueryLink = void 0;
|
|
13
13
|
(function (PersistedQueryLink) {
|
|
14
14
|
})(exports.PersistedQueryLink || (exports.PersistedQueryLink = {}));
|
|
15
|
+
function collectErrorsByMessage(graphQLErrors) {
|
|
16
|
+
var collected = Object.create(null);
|
|
17
|
+
if (utilities.isNonEmptyArray(graphQLErrors)) {
|
|
18
|
+
graphQLErrors.forEach(function (error) { return collected[error.message] = error; });
|
|
19
|
+
}
|
|
20
|
+
return collected;
|
|
21
|
+
}
|
|
15
22
|
var defaultOptions = {
|
|
16
23
|
disable: function (_a) {
|
|
17
24
|
var graphQLErrors = _a.graphQLErrors, operation = _a.operation;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var message = _a.message;
|
|
21
|
-
return message === 'PersistedQueryNotSupported';
|
|
22
|
-
})) {
|
|
25
|
+
var errorMessages = collectErrorsByMessage(graphQLErrors);
|
|
26
|
+
if (errorMessages.PersistedQueryNotSupported) {
|
|
23
27
|
return true;
|
|
24
28
|
}
|
|
29
|
+
if (errorMessages.PersistedQueryNotFound) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
25
32
|
var response = operation.getContext().response;
|
|
26
33
|
if (response &&
|
|
27
34
|
response.status &&
|
|
@@ -75,19 +82,25 @@ var createPersistedQueryLink = function (options) {
|
|
|
75
82
|
var response = _a.response, networkError = _a.networkError;
|
|
76
83
|
if (!retried && ((response && response.errors) || networkError)) {
|
|
77
84
|
retried = true;
|
|
85
|
+
var graphQLErrors = [];
|
|
86
|
+
var responseErrors = response && response.errors;
|
|
87
|
+
if (utilities.isNonEmptyArray(responseErrors)) {
|
|
88
|
+
graphQLErrors.push.apply(graphQLErrors, responseErrors);
|
|
89
|
+
}
|
|
90
|
+
var networkErrors = networkError &&
|
|
91
|
+
networkError.result &&
|
|
92
|
+
networkError.result.errors;
|
|
93
|
+
if (utilities.isNonEmptyArray(networkErrors)) {
|
|
94
|
+
graphQLErrors.push.apply(graphQLErrors, networkErrors);
|
|
95
|
+
}
|
|
78
96
|
var disablePayload = {
|
|
79
97
|
response: response,
|
|
80
98
|
networkError: networkError,
|
|
81
99
|
operation: operation,
|
|
82
|
-
graphQLErrors:
|
|
100
|
+
graphQLErrors: utilities.isNonEmptyArray(graphQLErrors) ? graphQLErrors : void 0,
|
|
83
101
|
};
|
|
84
102
|
supportsPersistedQueries = !disable(disablePayload);
|
|
85
|
-
if ((
|
|
86
|
-
response.errors &&
|
|
87
|
-
response.errors.some(function (_a) {
|
|
88
|
-
var message = _a.message;
|
|
89
|
-
return message === 'PersistedQueryNotFound';
|
|
90
|
-
})) ||
|
|
103
|
+
if (collectErrorsByMessage(graphQLErrors).PersistedQueryNotFound ||
|
|
91
104
|
!supportsPersistedQueries) {
|
|
92
105
|
if (subscription)
|
|
93
106
|
subscription.unsubscribe();
|