@graphql-tools/executor-http 1.1.9 → 1.1.10-alpha-f6e086f56c835f084b67e73148dbd7a9dcdd1ec9
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/CHANGELOG.md +482 -0
- package/dist/index.cjs +542 -0
- package/{typings → dist}/index.d.cts +28 -25
- package/{typings → dist}/index.d.ts +28 -25
- package/dist/index.js +539 -0
- package/package.json +38 -46
- package/cjs/createFormDataFromVariables.js +0 -76
- package/cjs/createTimeoutSignal.js +0 -1
- package/cjs/defaultPrintFn.js +0 -14
- package/cjs/handleEventStreamResponse.js +0 -55
- package/cjs/handleMultipartMixedResponse.js +0 -48
- package/cjs/index.js +0 -314
- package/cjs/isGraphQLUpload.js +0 -7
- package/cjs/isLiveQueryOperationDefinitionNode.js +0 -8
- package/cjs/package.json +0 -1
- package/cjs/prepareGETUrl.js +0 -26
- package/esm/createFormDataFromVariables.js +0 -72
- package/esm/createTimeoutSignal.js +0 -1
- package/esm/defaultPrintFn.js +0 -10
- package/esm/handleEventStreamResponse.js +0 -50
- package/esm/handleMultipartMixedResponse.js +0 -44
- package/esm/index.js +0 -310
- package/esm/isGraphQLUpload.js +0 -3
- package/esm/isLiveQueryOperationDefinitionNode.js +0 -5
- package/esm/prepareGETUrl.js +0 -22
- package/typings/createFormDataFromVariables.d.cts +0 -10
- package/typings/createFormDataFromVariables.d.ts +0 -10
- package/typings/createTimeoutSignal.d.cts +0 -0
- package/typings/createTimeoutSignal.d.ts +0 -0
- package/typings/defaultPrintFn.d.cts +0 -2
- package/typings/defaultPrintFn.d.ts +0 -2
- package/typings/handleEventStreamResponse.d.cts +0 -4
- package/typings/handleEventStreamResponse.d.ts +0 -4
- package/typings/handleMultipartMixedResponse.d.cts +0 -2
- package/typings/handleMultipartMixedResponse.d.ts +0 -2
- package/typings/isGraphQLUpload.d.cts +0 -9
- package/typings/isGraphQLUpload.d.ts +0 -9
- package/typings/isLiveQueryOperationDefinitionNode.d.cts +0 -2
- package/typings/isLiveQueryOperationDefinitionNode.d.ts +0 -2
- package/typings/prepareGETUrl.d.cts +0 -7
- package/typings/prepareGETUrl.d.ts +0 -7
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('@graphql-tools/utils');
|
|
4
|
+
var fetch = require('@whatwg-node/fetch');
|
|
5
|
+
var valueOrPromise = require('value-or-promise');
|
|
6
|
+
var extractFiles = require('extract-files');
|
|
7
|
+
var graphql = require('graphql');
|
|
8
|
+
var repeater = require('@repeaterjs/repeater');
|
|
9
|
+
var browser = require('meros/browser');
|
|
10
|
+
var node = require('meros/node');
|
|
11
|
+
|
|
12
|
+
function isGraphQLUpload(upload) {
|
|
13
|
+
return typeof upload.createReadStream === "function";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createFormDataFromVariables({
|
|
17
|
+
query,
|
|
18
|
+
variables,
|
|
19
|
+
operationName,
|
|
20
|
+
extensions
|
|
21
|
+
}, {
|
|
22
|
+
File: FileCtor = fetch.File,
|
|
23
|
+
FormData: FormDataCtor = fetch.FormData
|
|
24
|
+
}) {
|
|
25
|
+
const vars = Object.assign({}, variables);
|
|
26
|
+
const { clone, files } = extractFiles.extractFiles(
|
|
27
|
+
vars,
|
|
28
|
+
"variables",
|
|
29
|
+
(v) => extractFiles.isExtractableFile(v) || v?.promise || utils.isAsyncIterable(v) || v?.then || typeof v?.arrayBuffer === "function"
|
|
30
|
+
);
|
|
31
|
+
if (files.size === 0) {
|
|
32
|
+
return JSON.stringify(
|
|
33
|
+
{
|
|
34
|
+
query,
|
|
35
|
+
variables,
|
|
36
|
+
operationName,
|
|
37
|
+
extensions
|
|
38
|
+
},
|
|
39
|
+
null,
|
|
40
|
+
2
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
const map = {};
|
|
44
|
+
const uploads = [];
|
|
45
|
+
let currIndex = 0;
|
|
46
|
+
for (const [file, curr] of files) {
|
|
47
|
+
map[currIndex] = curr;
|
|
48
|
+
uploads[currIndex] = file;
|
|
49
|
+
currIndex++;
|
|
50
|
+
}
|
|
51
|
+
const form = new FormDataCtor();
|
|
52
|
+
form.append(
|
|
53
|
+
"operations",
|
|
54
|
+
JSON.stringify({
|
|
55
|
+
query,
|
|
56
|
+
variables: clone,
|
|
57
|
+
operationName,
|
|
58
|
+
extensions
|
|
59
|
+
})
|
|
60
|
+
);
|
|
61
|
+
form.append("map", JSON.stringify(map, null, 2));
|
|
62
|
+
function handleUpload(upload, i) {
|
|
63
|
+
const indexStr = i.toString();
|
|
64
|
+
if (upload != null) {
|
|
65
|
+
const filename = upload.filename || upload.name || upload.path || `blob-${indexStr}`;
|
|
66
|
+
if (utils.isPromise(upload)) {
|
|
67
|
+
return upload.then(
|
|
68
|
+
(resolvedUpload) => handleUpload(resolvedUpload, i)
|
|
69
|
+
);
|
|
70
|
+
} else if (isBlob(upload)) {
|
|
71
|
+
form.append(indexStr, upload, filename);
|
|
72
|
+
} else if (isGraphQLUpload(upload)) {
|
|
73
|
+
const stream = upload.createReadStream();
|
|
74
|
+
const chunks = [];
|
|
75
|
+
return Promise.resolve().then(async () => {
|
|
76
|
+
for await (const chunk of stream) {
|
|
77
|
+
if (chunk) {
|
|
78
|
+
chunks.push(...chunk);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const blobPart = new Uint8Array(chunks);
|
|
82
|
+
form.append(
|
|
83
|
+
indexStr,
|
|
84
|
+
new FileCtor([blobPart], filename, { type: upload.mimetype }),
|
|
85
|
+
filename
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
} else {
|
|
89
|
+
form.append(indexStr, new FileCtor([upload], filename), filename);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return valueOrPromise.ValueOrPromise.all(
|
|
94
|
+
uploads.map(
|
|
95
|
+
(upload, i) => new valueOrPromise.ValueOrPromise(() => handleUpload(upload, i))
|
|
96
|
+
)
|
|
97
|
+
).then(() => form).resolve();
|
|
98
|
+
}
|
|
99
|
+
function isBlob(obj) {
|
|
100
|
+
return typeof obj.arrayBuffer === "function";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const printCache = /* @__PURE__ */ new WeakMap();
|
|
104
|
+
function defaultPrintFn(document) {
|
|
105
|
+
let printed = printCache.get(document);
|
|
106
|
+
if (!printed) {
|
|
107
|
+
printed = graphql.stripIgnoredCharacters(graphql.print(document));
|
|
108
|
+
printCache.set(document, printed);
|
|
109
|
+
}
|
|
110
|
+
return printed;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const DELIM = "\n\n";
|
|
114
|
+
function isReadableStream(value) {
|
|
115
|
+
return value && typeof value.getReader === "function";
|
|
116
|
+
}
|
|
117
|
+
function handleEventStreamResponse(response) {
|
|
118
|
+
const body = response.body;
|
|
119
|
+
if (!isReadableStream(body)) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
"Response body is expected to be a readable stream but got; " + utils.inspect(body)
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return new repeater.Repeater((push, stop) => {
|
|
125
|
+
const decoder = new fetch.TextDecoder();
|
|
126
|
+
const reader = body.getReader();
|
|
127
|
+
reader.closed.then(stop).catch(stop);
|
|
128
|
+
stop.finally(() => reader.cancel());
|
|
129
|
+
let currChunk = "";
|
|
130
|
+
async function pump() {
|
|
131
|
+
const { done, value: chunk } = await reader.read();
|
|
132
|
+
if (done) {
|
|
133
|
+
return stop();
|
|
134
|
+
}
|
|
135
|
+
currChunk += typeof chunk === "string" ? chunk : decoder.decode(chunk);
|
|
136
|
+
for (; ; ) {
|
|
137
|
+
const delimIndex = currChunk.indexOf(DELIM);
|
|
138
|
+
if (delimIndex === -1) {
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
const msg = currChunk.slice(0, delimIndex);
|
|
142
|
+
currChunk = currChunk.slice(delimIndex + DELIM.length);
|
|
143
|
+
const dataStr = msg.split("data:")[1]?.trim();
|
|
144
|
+
if (dataStr) {
|
|
145
|
+
const data = JSON.parse(dataStr);
|
|
146
|
+
await push(data.payload || data);
|
|
147
|
+
}
|
|
148
|
+
const event = msg.split("event:")[1]?.trim();
|
|
149
|
+
if (event === "complete") {
|
|
150
|
+
return stop();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return pump();
|
|
154
|
+
}
|
|
155
|
+
return pump();
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function isIncomingMessage(body) {
|
|
160
|
+
return body != null && typeof body === "object" && "pipe" in body;
|
|
161
|
+
}
|
|
162
|
+
async function handleMultipartMixedResponse(response) {
|
|
163
|
+
const body = response.body;
|
|
164
|
+
const contentType = response.headers.get("content-type") || "";
|
|
165
|
+
let asyncIterator;
|
|
166
|
+
if (isIncomingMessage(body)) {
|
|
167
|
+
body.headers = {
|
|
168
|
+
"content-type": contentType
|
|
169
|
+
};
|
|
170
|
+
const result = await node.meros(body);
|
|
171
|
+
if ("next" in result) {
|
|
172
|
+
asyncIterator = result;
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
const result = await browser.meros(response);
|
|
176
|
+
if ("next" in result) {
|
|
177
|
+
asyncIterator = result;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const executionResult = {};
|
|
181
|
+
if (asyncIterator == null) {
|
|
182
|
+
return executionResult;
|
|
183
|
+
}
|
|
184
|
+
const resultStream = utils.mapAsyncIterator(asyncIterator, (part) => {
|
|
185
|
+
if (part.json) {
|
|
186
|
+
const incrementalResult = part.body;
|
|
187
|
+
utils.mergeIncrementalResult({
|
|
188
|
+
incrementalResult,
|
|
189
|
+
executionResult
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
return executionResult;
|
|
193
|
+
});
|
|
194
|
+
return resultStream;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const isLiveQueryOperationDefinitionNode = utils.memoize1(
|
|
198
|
+
function isLiveQueryOperationDefinitionNode2(node) {
|
|
199
|
+
return node.operation === "query" && node.directives?.some((directive) => directive.name.value === "live");
|
|
200
|
+
}
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
function prepareGETUrl({
|
|
204
|
+
baseUrl = "",
|
|
205
|
+
query,
|
|
206
|
+
variables,
|
|
207
|
+
operationName,
|
|
208
|
+
extensions
|
|
209
|
+
}) {
|
|
210
|
+
const dummyHostname = "https://dummyhostname.com";
|
|
211
|
+
const validUrl = baseUrl.startsWith("http") ? baseUrl : baseUrl?.startsWith("/") ? `${dummyHostname}${baseUrl}` : `${dummyHostname}/${baseUrl}`;
|
|
212
|
+
const urlObj = new URL(validUrl);
|
|
213
|
+
urlObj.searchParams.set("query", graphql.stripIgnoredCharacters(query));
|
|
214
|
+
if (variables && Object.keys(variables).length > 0) {
|
|
215
|
+
urlObj.searchParams.set("variables", JSON.stringify(variables));
|
|
216
|
+
}
|
|
217
|
+
if (operationName) {
|
|
218
|
+
urlObj.searchParams.set("operationName", operationName);
|
|
219
|
+
}
|
|
220
|
+
if (extensions) {
|
|
221
|
+
urlObj.searchParams.set("extensions", JSON.stringify(extensions));
|
|
222
|
+
}
|
|
223
|
+
const finalUrl = urlObj.toString().replace(dummyHostname, "");
|
|
224
|
+
return finalUrl;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function buildHTTPExecutor(options) {
|
|
228
|
+
const printFn = options?.print ?? defaultPrintFn;
|
|
229
|
+
let disposeCtrl;
|
|
230
|
+
const baseExecutor = (request) => {
|
|
231
|
+
if (disposeCtrl?.signal.aborted) {
|
|
232
|
+
return createResultForAbort(disposeCtrl.signal);
|
|
233
|
+
}
|
|
234
|
+
const fetchFn = request.extensions?.fetch ?? options?.fetch ?? fetch.fetch;
|
|
235
|
+
let method = request.extensions?.method || options?.method;
|
|
236
|
+
const operationAst = utils.getOperationASTFromRequest(request);
|
|
237
|
+
const operationType = operationAst.operation;
|
|
238
|
+
if ((options?.useGETForQueries || request.extensions?.useGETForQueries) && operationType === "query") {
|
|
239
|
+
method = "GET";
|
|
240
|
+
}
|
|
241
|
+
let accept = "application/graphql-response+json, application/json, multipart/mixed";
|
|
242
|
+
if (operationType === "subscription" || isLiveQueryOperationDefinitionNode(operationAst)) {
|
|
243
|
+
method ||= "GET";
|
|
244
|
+
accept = "text/event-stream";
|
|
245
|
+
} else {
|
|
246
|
+
method ||= "POST";
|
|
247
|
+
}
|
|
248
|
+
const endpoint = request.extensions?.endpoint || options?.endpoint || "/graphql";
|
|
249
|
+
const headers = { accept };
|
|
250
|
+
if (options?.headers) {
|
|
251
|
+
Object.assign(
|
|
252
|
+
headers,
|
|
253
|
+
typeof options?.headers === "function" ? options.headers(request) : options?.headers
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
if (request.extensions?.headers) {
|
|
257
|
+
const { headers: headersFromExtensions, ...restExtensions } = request.extensions;
|
|
258
|
+
Object.assign(headers, headersFromExtensions);
|
|
259
|
+
request.extensions = restExtensions;
|
|
260
|
+
}
|
|
261
|
+
const query = printFn(request.document);
|
|
262
|
+
let signal = disposeCtrl?.signal;
|
|
263
|
+
let clearTimeoutFn = () => {
|
|
264
|
+
};
|
|
265
|
+
if (options?.timeout) {
|
|
266
|
+
const timeoutCtrl = new AbortController();
|
|
267
|
+
signal = timeoutCtrl.signal;
|
|
268
|
+
disposeCtrl?.signal.addEventListener("abort", clearTimeoutFn);
|
|
269
|
+
const timeoutId = setTimeout(() => {
|
|
270
|
+
if (!timeoutCtrl.signal.aborted) {
|
|
271
|
+
timeoutCtrl.abort("timeout");
|
|
272
|
+
}
|
|
273
|
+
disposeCtrl?.signal.removeEventListener("abort", clearTimeoutFn);
|
|
274
|
+
}, options.timeout);
|
|
275
|
+
clearTimeoutFn = () => {
|
|
276
|
+
clearTimeout(timeoutId);
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
const upstreamErrorExtensions = {
|
|
280
|
+
request: {
|
|
281
|
+
method
|
|
282
|
+
},
|
|
283
|
+
response: {}
|
|
284
|
+
};
|
|
285
|
+
return new valueOrPromise.ValueOrPromise(() => {
|
|
286
|
+
switch (method) {
|
|
287
|
+
case "GET": {
|
|
288
|
+
const finalUrl = prepareGETUrl({
|
|
289
|
+
baseUrl: endpoint,
|
|
290
|
+
query,
|
|
291
|
+
variables: request.variables,
|
|
292
|
+
operationName: request.operationName,
|
|
293
|
+
extensions: request.extensions
|
|
294
|
+
});
|
|
295
|
+
const fetchOptions = {
|
|
296
|
+
method: "GET",
|
|
297
|
+
headers,
|
|
298
|
+
signal
|
|
299
|
+
};
|
|
300
|
+
if (options?.credentials != null) {
|
|
301
|
+
fetchOptions.credentials = options.credentials;
|
|
302
|
+
}
|
|
303
|
+
upstreamErrorExtensions.request.url = finalUrl;
|
|
304
|
+
return fetchFn(finalUrl, fetchOptions, request.context, request.info);
|
|
305
|
+
}
|
|
306
|
+
case "POST": {
|
|
307
|
+
const body = {
|
|
308
|
+
query,
|
|
309
|
+
variables: request.variables,
|
|
310
|
+
operationName: request.operationName,
|
|
311
|
+
extensions: request.extensions
|
|
312
|
+
};
|
|
313
|
+
upstreamErrorExtensions.request.body = body;
|
|
314
|
+
return new valueOrPromise.ValueOrPromise(
|
|
315
|
+
() => createFormDataFromVariables(body, {
|
|
316
|
+
File: options?.File,
|
|
317
|
+
FormData: options?.FormData
|
|
318
|
+
})
|
|
319
|
+
).then((body2) => {
|
|
320
|
+
if (typeof body2 === "string" && !headers["content-type"]) {
|
|
321
|
+
upstreamErrorExtensions.request.body = body2;
|
|
322
|
+
headers["content-type"] = "application/json";
|
|
323
|
+
}
|
|
324
|
+
const fetchOptions = {
|
|
325
|
+
method: "POST",
|
|
326
|
+
body: body2,
|
|
327
|
+
headers,
|
|
328
|
+
signal
|
|
329
|
+
};
|
|
330
|
+
if (options?.credentials != null) {
|
|
331
|
+
fetchOptions.credentials = options.credentials;
|
|
332
|
+
}
|
|
333
|
+
return fetchFn(
|
|
334
|
+
endpoint,
|
|
335
|
+
fetchOptions,
|
|
336
|
+
request.context,
|
|
337
|
+
request.info
|
|
338
|
+
);
|
|
339
|
+
}).resolve();
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}).then((fetchResult) => {
|
|
343
|
+
upstreamErrorExtensions.response.status = fetchResult.status;
|
|
344
|
+
upstreamErrorExtensions.response.statusText = fetchResult.statusText;
|
|
345
|
+
Object.defineProperty(upstreamErrorExtensions.response, "headers", {
|
|
346
|
+
get() {
|
|
347
|
+
return Object.fromEntries(fetchResult.headers.entries());
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
clearTimeoutFn();
|
|
351
|
+
if (options?.retry != null && !fetchResult.status.toString().startsWith("2")) {
|
|
352
|
+
throw new Error(
|
|
353
|
+
fetchResult.statusText || `Upstream HTTP Error: ${fetchResult.status}`
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
const contentType = fetchResult.headers.get("content-type");
|
|
357
|
+
if (contentType?.includes("text/event-stream")) {
|
|
358
|
+
return handleEventStreamResponse(fetchResult);
|
|
359
|
+
} else if (contentType?.includes("multipart/mixed")) {
|
|
360
|
+
return handleMultipartMixedResponse(fetchResult);
|
|
361
|
+
}
|
|
362
|
+
return fetchResult.text();
|
|
363
|
+
}).then((result) => {
|
|
364
|
+
if (typeof result === "string") {
|
|
365
|
+
upstreamErrorExtensions.response.body = result;
|
|
366
|
+
if (result) {
|
|
367
|
+
try {
|
|
368
|
+
const parsedResult = JSON.parse(result);
|
|
369
|
+
upstreamErrorExtensions.response.body = parsedResult;
|
|
370
|
+
if (parsedResult.data == null && (parsedResult.errors == null || parsedResult.errors.length === 0)) {
|
|
371
|
+
return {
|
|
372
|
+
errors: [
|
|
373
|
+
utils.createGraphQLError(
|
|
374
|
+
'Unexpected empty "data" and "errors" fields in result: ' + result,
|
|
375
|
+
{
|
|
376
|
+
extensions: upstreamErrorExtensions
|
|
377
|
+
}
|
|
378
|
+
)
|
|
379
|
+
]
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
if (Array.isArray(parsedResult.errors)) {
|
|
383
|
+
return {
|
|
384
|
+
...parsedResult,
|
|
385
|
+
errors: parsedResult.errors.map(
|
|
386
|
+
({
|
|
387
|
+
message,
|
|
388
|
+
...options2
|
|
389
|
+
}) => utils.createGraphQLError(message, {
|
|
390
|
+
...options2,
|
|
391
|
+
extensions: {
|
|
392
|
+
code: "DOWNSTREAM_SERVICE_ERROR",
|
|
393
|
+
...options2.extensions || {}
|
|
394
|
+
}
|
|
395
|
+
})
|
|
396
|
+
)
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
return parsedResult;
|
|
400
|
+
} catch (e) {
|
|
401
|
+
return {
|
|
402
|
+
errors: [
|
|
403
|
+
utils.createGraphQLError(
|
|
404
|
+
`Unexpected response: ${JSON.stringify(result)}`,
|
|
405
|
+
{
|
|
406
|
+
extensions: upstreamErrorExtensions,
|
|
407
|
+
originalError: e
|
|
408
|
+
}
|
|
409
|
+
)
|
|
410
|
+
]
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
} else {
|
|
415
|
+
return result;
|
|
416
|
+
}
|
|
417
|
+
}).catch((e) => {
|
|
418
|
+
if (e.name === "AggregateError") {
|
|
419
|
+
return {
|
|
420
|
+
errors: e.errors.map(
|
|
421
|
+
(e2) => coerceFetchError(e2, {
|
|
422
|
+
signal,
|
|
423
|
+
endpoint,
|
|
424
|
+
upstreamErrorExtensions
|
|
425
|
+
})
|
|
426
|
+
)
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
return {
|
|
430
|
+
errors: [
|
|
431
|
+
coerceFetchError(e, {
|
|
432
|
+
signal,
|
|
433
|
+
endpoint,
|
|
434
|
+
upstreamErrorExtensions
|
|
435
|
+
})
|
|
436
|
+
]
|
|
437
|
+
};
|
|
438
|
+
}).resolve();
|
|
439
|
+
};
|
|
440
|
+
let executor = baseExecutor;
|
|
441
|
+
if (options?.retry != null) {
|
|
442
|
+
executor = function retryExecutor(request) {
|
|
443
|
+
let result;
|
|
444
|
+
let attempt = 0;
|
|
445
|
+
function retryAttempt() {
|
|
446
|
+
if (disposeCtrl?.signal.aborted) {
|
|
447
|
+
return createResultForAbort(disposeCtrl.signal);
|
|
448
|
+
}
|
|
449
|
+
attempt++;
|
|
450
|
+
if (attempt > options.retry) {
|
|
451
|
+
if (result != null) {
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
454
|
+
return {
|
|
455
|
+
errors: [utils.createGraphQLError("No response returned from fetch")]
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
return new valueOrPromise.ValueOrPromise(() => baseExecutor(request)).then((res) => {
|
|
459
|
+
result = res;
|
|
460
|
+
if (result?.errors?.length) {
|
|
461
|
+
return retryAttempt();
|
|
462
|
+
}
|
|
463
|
+
return result;
|
|
464
|
+
}).resolve();
|
|
465
|
+
}
|
|
466
|
+
return retryAttempt();
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
if (!options?.disposable) {
|
|
470
|
+
disposeCtrl = void 0;
|
|
471
|
+
return executor;
|
|
472
|
+
}
|
|
473
|
+
disposeCtrl = new AbortController();
|
|
474
|
+
Object.defineProperties(executor, {
|
|
475
|
+
[Symbol.dispose]: {
|
|
476
|
+
get() {
|
|
477
|
+
return function dispose() {
|
|
478
|
+
return disposeCtrl.abort(createAbortErrorReason());
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
},
|
|
482
|
+
[Symbol.asyncDispose]: {
|
|
483
|
+
get() {
|
|
484
|
+
return function asyncDispose() {
|
|
485
|
+
return disposeCtrl.abort(createAbortErrorReason());
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
return executor;
|
|
491
|
+
}
|
|
492
|
+
function coerceFetchError(e, {
|
|
493
|
+
signal,
|
|
494
|
+
endpoint,
|
|
495
|
+
upstreamErrorExtensions
|
|
496
|
+
}) {
|
|
497
|
+
if (typeof e === "string") {
|
|
498
|
+
return utils.createGraphQLError(e, {
|
|
499
|
+
extensions: upstreamErrorExtensions
|
|
500
|
+
});
|
|
501
|
+
} else if (e.name === "GraphQLError") {
|
|
502
|
+
return e;
|
|
503
|
+
} else if (e.name === "TypeError" && e.message === "fetch failed") {
|
|
504
|
+
return utils.createGraphQLError(`fetch failed to ${endpoint}`, {
|
|
505
|
+
extensions: upstreamErrorExtensions,
|
|
506
|
+
originalError: e
|
|
507
|
+
});
|
|
508
|
+
} else if (e.name === "AbortError" && signal?.reason) {
|
|
509
|
+
return createGraphQLErrorForAbort(signal, {
|
|
510
|
+
extensions: upstreamErrorExtensions
|
|
511
|
+
});
|
|
512
|
+
} else if (e.message) {
|
|
513
|
+
return utils.createGraphQLError(e.message, {
|
|
514
|
+
extensions: upstreamErrorExtensions,
|
|
515
|
+
originalError: e
|
|
516
|
+
});
|
|
517
|
+
} else {
|
|
518
|
+
return utils.createGraphQLError("Unknown error", {
|
|
519
|
+
extensions: upstreamErrorExtensions,
|
|
520
|
+
originalError: e
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
function createAbortErrorReason() {
|
|
525
|
+
return new Error("Executor was disposed.");
|
|
526
|
+
}
|
|
527
|
+
function createGraphQLErrorForAbort(signal, extensions) {
|
|
528
|
+
return utils.createGraphQLError(
|
|
529
|
+
"The operation was aborted. reason: " + signal.reason,
|
|
530
|
+
{
|
|
531
|
+
extensions
|
|
532
|
+
}
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
function createResultForAbort(signal, extensions) {
|
|
536
|
+
return {
|
|
537
|
+
errors: [createGraphQLErrorForAbort(signal, extensions)]
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
exports.buildHTTPExecutor = buildHTTPExecutor;
|
|
542
|
+
exports.isLiveQueryOperationDefinitionNode = isLiveQueryOperationDefinitionNode;
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { ExecutionRequest, DisposableSyncExecutor, SyncExecutor, DisposableAsyncExecutor, AsyncExecutor } from '@graphql-tools/utils';
|
|
2
|
+
import { OperationDefinitionNode, GraphQLResolveInfo, DocumentNode } from 'graphql';
|
|
3
|
+
|
|
4
|
+
declare const isLiveQueryOperationDefinitionNode: (node: OperationDefinitionNode) => boolean | undefined;
|
|
5
|
+
|
|
6
|
+
type SyncFetchFn = (url: string, init?: RequestInit, context?: any, info?: GraphQLResolveInfo) => SyncResponse;
|
|
7
|
+
type SyncResponse = Omit<Response, 'json' | 'text'> & {
|
|
6
8
|
json: () => any;
|
|
7
9
|
text: () => string;
|
|
8
10
|
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
type AsyncFetchFn = (url: string, options?: RequestInit, context?: any, info?: GraphQLResolveInfo) => Promise<Response> | Response;
|
|
12
|
+
type RegularFetchFn = (url: string) => Promise<Response> | Response;
|
|
13
|
+
type FetchFn = AsyncFetchFn | SyncFetchFn | RegularFetchFn;
|
|
14
|
+
type AsyncImportFn = (moduleName: string) => PromiseLike<any>;
|
|
15
|
+
type SyncImportFn = (moduleName: string) => any;
|
|
16
|
+
interface HTTPExecutorOptions {
|
|
15
17
|
endpoint?: string;
|
|
16
18
|
fetch?: FetchFn;
|
|
17
19
|
/**
|
|
@@ -59,45 +61,46 @@ export interface HTTPExecutorOptions {
|
|
|
59
61
|
*/
|
|
60
62
|
disposable?: boolean;
|
|
61
63
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
type HeadersConfig = Record<string, string>;
|
|
65
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch' | 'disposable'> & {
|
|
64
66
|
fetch: SyncFetchFn;
|
|
65
67
|
disposable: true;
|
|
66
68
|
}): DisposableSyncExecutor<any, HTTPExecutorOptions>;
|
|
67
|
-
|
|
69
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch' | 'disposable'> & {
|
|
68
70
|
fetch: SyncFetchFn;
|
|
69
71
|
disposable: false;
|
|
70
72
|
}): SyncExecutor<any, HTTPExecutorOptions>;
|
|
71
|
-
|
|
73
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch'> & {
|
|
72
74
|
fetch: SyncFetchFn;
|
|
73
75
|
}): SyncExecutor<any, HTTPExecutorOptions>;
|
|
74
|
-
|
|
76
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch' | 'disposable'> & {
|
|
75
77
|
fetch: AsyncFetchFn;
|
|
76
78
|
disposable: true;
|
|
77
79
|
}): DisposableAsyncExecutor<any, HTTPExecutorOptions>;
|
|
78
|
-
|
|
80
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch' | 'disposable'> & {
|
|
79
81
|
fetch: AsyncFetchFn;
|
|
80
82
|
disposable: false;
|
|
81
83
|
}): AsyncExecutor<any, HTTPExecutorOptions>;
|
|
82
|
-
|
|
84
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch'> & {
|
|
83
85
|
fetch: AsyncFetchFn;
|
|
84
86
|
}): AsyncExecutor<any, HTTPExecutorOptions>;
|
|
85
|
-
|
|
87
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch' | 'disposable'> & {
|
|
86
88
|
fetch: RegularFetchFn;
|
|
87
89
|
disposable: true;
|
|
88
90
|
}): DisposableAsyncExecutor<any, HTTPExecutorOptions>;
|
|
89
|
-
|
|
91
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch' | 'disposable'> & {
|
|
90
92
|
fetch: RegularFetchFn;
|
|
91
93
|
disposable: false;
|
|
92
94
|
}): AsyncExecutor<any, HTTPExecutorOptions>;
|
|
93
|
-
|
|
95
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch'> & {
|
|
94
96
|
fetch: RegularFetchFn;
|
|
95
97
|
}): AsyncExecutor<any, HTTPExecutorOptions>;
|
|
96
|
-
|
|
98
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch' | 'disposable'> & {
|
|
97
99
|
disposable: true;
|
|
98
100
|
}): DisposableAsyncExecutor<any, HTTPExecutorOptions>;
|
|
99
|
-
|
|
101
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch' | 'disposable'> & {
|
|
100
102
|
disposable: false;
|
|
101
103
|
}): AsyncExecutor<any, HTTPExecutorOptions>;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
+
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch'>): AsyncExecutor<any, HTTPExecutorOptions>;
|
|
105
|
+
|
|
106
|
+
export { type AsyncFetchFn, type AsyncImportFn, type FetchFn, type HTTPExecutorOptions, type HeadersConfig, type RegularFetchFn, type SyncFetchFn, type SyncImportFn, type SyncResponse, buildHTTPExecutor, isLiveQueryOperationDefinitionNode };
|