@graphql-tools/executor-http 1.1.9 → 1.1.10-alpha-e8c01415b3b19676af5ff8b818bc152e3293f9e4
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 +480 -0
- package/dist/index.cjs +546 -0
- package/{typings → dist}/index.d.cts +28 -25
- package/{typings → dist}/index.d.ts +28 -25
- package/dist/index.js +543 -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,546 @@
|
|
|
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
|
+
if (!isReadableStream(response.body)) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
"Response body is expected to be a readable stream but got; " + utils.inspect(response.body)
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
const decodedBody = response.body.pipeThrough(new fetch.TextDecoderStream());
|
|
124
|
+
return new repeater.Repeater(async (push, stop) => {
|
|
125
|
+
let currChunk = "";
|
|
126
|
+
let cancelled = false;
|
|
127
|
+
stop.finally(() => {
|
|
128
|
+
cancelled = true;
|
|
129
|
+
});
|
|
130
|
+
try {
|
|
131
|
+
for await (const value of decodedBody) {
|
|
132
|
+
if (cancelled) {
|
|
133
|
+
return stop();
|
|
134
|
+
}
|
|
135
|
+
currChunk += value;
|
|
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
|
+
cancelled = true;
|
|
151
|
+
return stop();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return stop();
|
|
156
|
+
} catch (e) {
|
|
157
|
+
cancelled = true;
|
|
158
|
+
return stop(e);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function isIncomingMessage(body) {
|
|
164
|
+
return body != null && typeof body === "object" && "pipe" in body;
|
|
165
|
+
}
|
|
166
|
+
async function handleMultipartMixedResponse(response) {
|
|
167
|
+
const body = response.body;
|
|
168
|
+
const contentType = response.headers.get("content-type") || "";
|
|
169
|
+
let asyncIterator;
|
|
170
|
+
if (isIncomingMessage(body)) {
|
|
171
|
+
body.headers = {
|
|
172
|
+
"content-type": contentType
|
|
173
|
+
};
|
|
174
|
+
const result = await node.meros(body);
|
|
175
|
+
if ("next" in result) {
|
|
176
|
+
asyncIterator = result;
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
const result = await browser.meros(response);
|
|
180
|
+
if ("next" in result) {
|
|
181
|
+
asyncIterator = result;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const executionResult = {};
|
|
185
|
+
if (asyncIterator == null) {
|
|
186
|
+
return executionResult;
|
|
187
|
+
}
|
|
188
|
+
const resultStream = utils.mapAsyncIterator(asyncIterator, (part) => {
|
|
189
|
+
if (part.json) {
|
|
190
|
+
const incrementalResult = part.body;
|
|
191
|
+
utils.mergeIncrementalResult({
|
|
192
|
+
incrementalResult,
|
|
193
|
+
executionResult
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
return executionResult;
|
|
197
|
+
});
|
|
198
|
+
return resultStream;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const isLiveQueryOperationDefinitionNode = utils.memoize1(
|
|
202
|
+
function isLiveQueryOperationDefinitionNode2(node) {
|
|
203
|
+
return node.operation === "query" && node.directives?.some((directive) => directive.name.value === "live");
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
function prepareGETUrl({
|
|
208
|
+
baseUrl = "",
|
|
209
|
+
query,
|
|
210
|
+
variables,
|
|
211
|
+
operationName,
|
|
212
|
+
extensions
|
|
213
|
+
}) {
|
|
214
|
+
const dummyHostname = "https://dummyhostname.com";
|
|
215
|
+
const validUrl = baseUrl.startsWith("http") ? baseUrl : baseUrl?.startsWith("/") ? `${dummyHostname}${baseUrl}` : `${dummyHostname}/${baseUrl}`;
|
|
216
|
+
const urlObj = new URL(validUrl);
|
|
217
|
+
urlObj.searchParams.set("query", graphql.stripIgnoredCharacters(query));
|
|
218
|
+
if (variables && Object.keys(variables).length > 0) {
|
|
219
|
+
urlObj.searchParams.set("variables", JSON.stringify(variables));
|
|
220
|
+
}
|
|
221
|
+
if (operationName) {
|
|
222
|
+
urlObj.searchParams.set("operationName", operationName);
|
|
223
|
+
}
|
|
224
|
+
if (extensions) {
|
|
225
|
+
urlObj.searchParams.set("extensions", JSON.stringify(extensions));
|
|
226
|
+
}
|
|
227
|
+
const finalUrl = urlObj.toString().replace(dummyHostname, "");
|
|
228
|
+
return finalUrl;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function buildHTTPExecutor(options) {
|
|
232
|
+
const printFn = options?.print ?? defaultPrintFn;
|
|
233
|
+
let disposeCtrl;
|
|
234
|
+
const baseExecutor = (request) => {
|
|
235
|
+
if (disposeCtrl?.signal.aborted) {
|
|
236
|
+
return createResultForAbort(disposeCtrl.signal);
|
|
237
|
+
}
|
|
238
|
+
const fetchFn = request.extensions?.fetch ?? options?.fetch ?? fetch.fetch;
|
|
239
|
+
let method = request.extensions?.method || options?.method;
|
|
240
|
+
const operationAst = utils.getOperationASTFromRequest(request);
|
|
241
|
+
const operationType = operationAst.operation;
|
|
242
|
+
if ((options?.useGETForQueries || request.extensions?.useGETForQueries) && operationType === "query") {
|
|
243
|
+
method = "GET";
|
|
244
|
+
}
|
|
245
|
+
let accept = "application/graphql-response+json, application/json, multipart/mixed";
|
|
246
|
+
if (operationType === "subscription" || isLiveQueryOperationDefinitionNode(operationAst)) {
|
|
247
|
+
method ||= "GET";
|
|
248
|
+
accept = "text/event-stream";
|
|
249
|
+
} else {
|
|
250
|
+
method ||= "POST";
|
|
251
|
+
}
|
|
252
|
+
const endpoint = request.extensions?.endpoint || options?.endpoint || "/graphql";
|
|
253
|
+
const headers = { accept };
|
|
254
|
+
if (options?.headers) {
|
|
255
|
+
Object.assign(
|
|
256
|
+
headers,
|
|
257
|
+
typeof options?.headers === "function" ? options.headers(request) : options?.headers
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
if (request.extensions?.headers) {
|
|
261
|
+
const { headers: headersFromExtensions, ...restExtensions } = request.extensions;
|
|
262
|
+
Object.assign(headers, headersFromExtensions);
|
|
263
|
+
request.extensions = restExtensions;
|
|
264
|
+
}
|
|
265
|
+
const query = printFn(request.document);
|
|
266
|
+
let signal = disposeCtrl?.signal;
|
|
267
|
+
let clearTimeoutFn = () => {
|
|
268
|
+
};
|
|
269
|
+
if (options?.timeout) {
|
|
270
|
+
const timeoutCtrl = new AbortController();
|
|
271
|
+
signal = timeoutCtrl.signal;
|
|
272
|
+
disposeCtrl?.signal.addEventListener("abort", clearTimeoutFn);
|
|
273
|
+
const timeoutId = setTimeout(() => {
|
|
274
|
+
if (!timeoutCtrl.signal.aborted) {
|
|
275
|
+
timeoutCtrl.abort("timeout");
|
|
276
|
+
}
|
|
277
|
+
disposeCtrl?.signal.removeEventListener("abort", clearTimeoutFn);
|
|
278
|
+
}, options.timeout);
|
|
279
|
+
clearTimeoutFn = () => {
|
|
280
|
+
clearTimeout(timeoutId);
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
const upstreamErrorExtensions = {
|
|
284
|
+
request: {
|
|
285
|
+
method
|
|
286
|
+
},
|
|
287
|
+
response: {}
|
|
288
|
+
};
|
|
289
|
+
return new valueOrPromise.ValueOrPromise(() => {
|
|
290
|
+
switch (method) {
|
|
291
|
+
case "GET": {
|
|
292
|
+
const finalUrl = prepareGETUrl({
|
|
293
|
+
baseUrl: endpoint,
|
|
294
|
+
query,
|
|
295
|
+
variables: request.variables,
|
|
296
|
+
operationName: request.operationName,
|
|
297
|
+
extensions: request.extensions
|
|
298
|
+
});
|
|
299
|
+
const fetchOptions = {
|
|
300
|
+
method: "GET",
|
|
301
|
+
headers,
|
|
302
|
+
signal
|
|
303
|
+
};
|
|
304
|
+
if (options?.credentials != null) {
|
|
305
|
+
fetchOptions.credentials = options.credentials;
|
|
306
|
+
}
|
|
307
|
+
upstreamErrorExtensions.request.url = finalUrl;
|
|
308
|
+
return fetchFn(finalUrl, fetchOptions, request.context, request.info);
|
|
309
|
+
}
|
|
310
|
+
case "POST": {
|
|
311
|
+
const body = {
|
|
312
|
+
query,
|
|
313
|
+
variables: request.variables,
|
|
314
|
+
operationName: request.operationName,
|
|
315
|
+
extensions: request.extensions
|
|
316
|
+
};
|
|
317
|
+
upstreamErrorExtensions.request.body = body;
|
|
318
|
+
return new valueOrPromise.ValueOrPromise(
|
|
319
|
+
() => createFormDataFromVariables(body, {
|
|
320
|
+
File: options?.File,
|
|
321
|
+
FormData: options?.FormData
|
|
322
|
+
})
|
|
323
|
+
).then((body2) => {
|
|
324
|
+
if (typeof body2 === "string" && !headers["content-type"]) {
|
|
325
|
+
upstreamErrorExtensions.request.body = body2;
|
|
326
|
+
headers["content-type"] = "application/json";
|
|
327
|
+
}
|
|
328
|
+
const fetchOptions = {
|
|
329
|
+
method: "POST",
|
|
330
|
+
body: body2,
|
|
331
|
+
headers,
|
|
332
|
+
signal
|
|
333
|
+
};
|
|
334
|
+
if (options?.credentials != null) {
|
|
335
|
+
fetchOptions.credentials = options.credentials;
|
|
336
|
+
}
|
|
337
|
+
return fetchFn(
|
|
338
|
+
endpoint,
|
|
339
|
+
fetchOptions,
|
|
340
|
+
request.context,
|
|
341
|
+
request.info
|
|
342
|
+
);
|
|
343
|
+
}).resolve();
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}).then((fetchResult) => {
|
|
347
|
+
upstreamErrorExtensions.response.status = fetchResult.status;
|
|
348
|
+
upstreamErrorExtensions.response.statusText = fetchResult.statusText;
|
|
349
|
+
Object.defineProperty(upstreamErrorExtensions.response, "headers", {
|
|
350
|
+
get() {
|
|
351
|
+
return Object.fromEntries(fetchResult.headers.entries());
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
clearTimeoutFn();
|
|
355
|
+
if (options?.retry != null && !fetchResult.status.toString().startsWith("2")) {
|
|
356
|
+
throw new Error(
|
|
357
|
+
fetchResult.statusText || `Upstream HTTP Error: ${fetchResult.status}`
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
const contentType = fetchResult.headers.get("content-type");
|
|
361
|
+
if (contentType?.includes("text/event-stream")) {
|
|
362
|
+
return handleEventStreamResponse(fetchResult);
|
|
363
|
+
} else if (contentType?.includes("multipart/mixed")) {
|
|
364
|
+
return handleMultipartMixedResponse(fetchResult);
|
|
365
|
+
}
|
|
366
|
+
return fetchResult.text();
|
|
367
|
+
}).then((result) => {
|
|
368
|
+
if (typeof result === "string") {
|
|
369
|
+
upstreamErrorExtensions.response.body = result;
|
|
370
|
+
if (result) {
|
|
371
|
+
try {
|
|
372
|
+
const parsedResult = JSON.parse(result);
|
|
373
|
+
upstreamErrorExtensions.response.body = parsedResult;
|
|
374
|
+
if (parsedResult.data == null && (parsedResult.errors == null || parsedResult.errors.length === 0)) {
|
|
375
|
+
return {
|
|
376
|
+
errors: [
|
|
377
|
+
utils.createGraphQLError(
|
|
378
|
+
'Unexpected empty "data" and "errors" fields in result: ' + result,
|
|
379
|
+
{
|
|
380
|
+
extensions: upstreamErrorExtensions
|
|
381
|
+
}
|
|
382
|
+
)
|
|
383
|
+
]
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
if (Array.isArray(parsedResult.errors)) {
|
|
387
|
+
return {
|
|
388
|
+
...parsedResult,
|
|
389
|
+
errors: parsedResult.errors.map(
|
|
390
|
+
({
|
|
391
|
+
message,
|
|
392
|
+
...options2
|
|
393
|
+
}) => utils.createGraphQLError(message, {
|
|
394
|
+
...options2,
|
|
395
|
+
extensions: {
|
|
396
|
+
code: "DOWNSTREAM_SERVICE_ERROR",
|
|
397
|
+
...options2.extensions || {}
|
|
398
|
+
}
|
|
399
|
+
})
|
|
400
|
+
)
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
return parsedResult;
|
|
404
|
+
} catch (e) {
|
|
405
|
+
return {
|
|
406
|
+
errors: [
|
|
407
|
+
utils.createGraphQLError(
|
|
408
|
+
`Unexpected response: ${JSON.stringify(result)}`,
|
|
409
|
+
{
|
|
410
|
+
extensions: upstreamErrorExtensions,
|
|
411
|
+
originalError: e
|
|
412
|
+
}
|
|
413
|
+
)
|
|
414
|
+
]
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
} else {
|
|
419
|
+
return result;
|
|
420
|
+
}
|
|
421
|
+
}).catch((e) => {
|
|
422
|
+
if (e.name === "AggregateError") {
|
|
423
|
+
return {
|
|
424
|
+
errors: e.errors.map(
|
|
425
|
+
(e2) => coerceFetchError(e2, {
|
|
426
|
+
signal,
|
|
427
|
+
endpoint,
|
|
428
|
+
upstreamErrorExtensions
|
|
429
|
+
})
|
|
430
|
+
)
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
errors: [
|
|
435
|
+
coerceFetchError(e, {
|
|
436
|
+
signal,
|
|
437
|
+
endpoint,
|
|
438
|
+
upstreamErrorExtensions
|
|
439
|
+
})
|
|
440
|
+
]
|
|
441
|
+
};
|
|
442
|
+
}).resolve();
|
|
443
|
+
};
|
|
444
|
+
let executor = baseExecutor;
|
|
445
|
+
if (options?.retry != null) {
|
|
446
|
+
executor = function retryExecutor(request) {
|
|
447
|
+
let result;
|
|
448
|
+
let attempt = 0;
|
|
449
|
+
function retryAttempt() {
|
|
450
|
+
if (disposeCtrl?.signal.aborted) {
|
|
451
|
+
return createResultForAbort(disposeCtrl.signal);
|
|
452
|
+
}
|
|
453
|
+
attempt++;
|
|
454
|
+
if (attempt > options.retry) {
|
|
455
|
+
if (result != null) {
|
|
456
|
+
return result;
|
|
457
|
+
}
|
|
458
|
+
return {
|
|
459
|
+
errors: [utils.createGraphQLError("No response returned from fetch")]
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
return new valueOrPromise.ValueOrPromise(() => baseExecutor(request)).then((res) => {
|
|
463
|
+
result = res;
|
|
464
|
+
if (result?.errors?.length) {
|
|
465
|
+
return retryAttempt();
|
|
466
|
+
}
|
|
467
|
+
return result;
|
|
468
|
+
}).resolve();
|
|
469
|
+
}
|
|
470
|
+
return retryAttempt();
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
if (!options?.disposable) {
|
|
474
|
+
disposeCtrl = void 0;
|
|
475
|
+
return executor;
|
|
476
|
+
}
|
|
477
|
+
disposeCtrl = new AbortController();
|
|
478
|
+
Object.defineProperties(executor, {
|
|
479
|
+
[Symbol.dispose]: {
|
|
480
|
+
get() {
|
|
481
|
+
return function dispose() {
|
|
482
|
+
return disposeCtrl.abort(createAbortErrorReason());
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
},
|
|
486
|
+
[Symbol.asyncDispose]: {
|
|
487
|
+
get() {
|
|
488
|
+
return function asyncDispose() {
|
|
489
|
+
return disposeCtrl.abort(createAbortErrorReason());
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
return executor;
|
|
495
|
+
}
|
|
496
|
+
function coerceFetchError(e, {
|
|
497
|
+
signal,
|
|
498
|
+
endpoint,
|
|
499
|
+
upstreamErrorExtensions
|
|
500
|
+
}) {
|
|
501
|
+
if (typeof e === "string") {
|
|
502
|
+
return utils.createGraphQLError(e, {
|
|
503
|
+
extensions: upstreamErrorExtensions
|
|
504
|
+
});
|
|
505
|
+
} else if (e.name === "GraphQLError") {
|
|
506
|
+
return e;
|
|
507
|
+
} else if (e.name === "TypeError" && e.message === "fetch failed") {
|
|
508
|
+
return utils.createGraphQLError(`fetch failed to ${endpoint}`, {
|
|
509
|
+
extensions: upstreamErrorExtensions,
|
|
510
|
+
originalError: e
|
|
511
|
+
});
|
|
512
|
+
} else if (e.name === "AbortError" && signal?.reason) {
|
|
513
|
+
return createGraphQLErrorForAbort(signal, {
|
|
514
|
+
extensions: upstreamErrorExtensions
|
|
515
|
+
});
|
|
516
|
+
} else if (e.message) {
|
|
517
|
+
return utils.createGraphQLError(e.message, {
|
|
518
|
+
extensions: upstreamErrorExtensions,
|
|
519
|
+
originalError: e
|
|
520
|
+
});
|
|
521
|
+
} else {
|
|
522
|
+
return utils.createGraphQLError("Unknown error", {
|
|
523
|
+
extensions: upstreamErrorExtensions,
|
|
524
|
+
originalError: e
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
function createAbortErrorReason() {
|
|
529
|
+
return new Error("Executor was disposed.");
|
|
530
|
+
}
|
|
531
|
+
function createGraphQLErrorForAbort(signal, extensions) {
|
|
532
|
+
return utils.createGraphQLError(
|
|
533
|
+
"The operation was aborted. reason: " + signal.reason,
|
|
534
|
+
{
|
|
535
|
+
extensions
|
|
536
|
+
}
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
function createResultForAbort(signal, extensions) {
|
|
540
|
+
return {
|
|
541
|
+
errors: [createGraphQLErrorForAbort(signal, extensions)]
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
exports.buildHTTPExecutor = buildHTTPExecutor;
|
|
546
|
+
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 };
|