@graphql-tools/batch-execute 9.0.16 → 9.0.17-alpha-071d64e79465dc9de058cc16c99ec1a5472d2bc1

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @graphql-tools/batch-execute
2
2
 
3
+ ## 9.0.17-alpha-071d64e79465dc9de058cc16c99ec1a5472d2bc1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1134](https://github.com/graphql-hive/gateway/pull/1134) [`071d64e`](https://github.com/graphql-hive/gateway/commit/071d64e79465dc9de058cc16c99ec1a5472d2bc1) Thanks [@ardatan](https://github.com/ardatan)! - Small improvements on usage of promise helpers
8
+
3
9
  ## 9.0.16
4
10
 
5
11
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -237,27 +237,27 @@ function isFragmentDefinition(def) {
237
237
  return def.kind === graphql.Kind.FRAGMENT_DEFINITION;
238
238
  }
239
239
 
240
- function splitResult({ data, errors }, numResults) {
241
- const splitResults = [];
242
- for (let i = 0; i < numResults; i++) {
243
- splitResults.push({});
244
- }
245
- if (data) {
246
- for (const prefixedKey in data) {
240
+ function splitResult(mergedResult, numResults) {
241
+ const splitResults = new Array(numResults);
242
+ if (mergedResult.data) {
243
+ for (const prefixedKey in mergedResult.data) {
247
244
  const { index, originalKey } = parseKey(prefixedKey);
248
245
  const result = splitResults[index];
249
246
  if (result == null) {
250
- continue;
251
- }
252
- if (result.data == null) {
253
- result.data = { [originalKey]: data[prefixedKey] };
247
+ splitResults[index] = {
248
+ data: {
249
+ [originalKey]: mergedResult.data[prefixedKey]
250
+ }
251
+ };
252
+ } else if (result.data == null) {
253
+ result.data = { [originalKey]: mergedResult.data[prefixedKey] };
254
254
  } else {
255
- result.data[originalKey] = data[prefixedKey];
255
+ result.data[originalKey] = mergedResult.data[prefixedKey];
256
256
  }
257
257
  }
258
258
  }
259
- if (errors) {
260
- for (const error of errors) {
259
+ if (mergedResult.errors) {
260
+ for (const error of mergedResult.errors) {
261
261
  if (error.path) {
262
262
  const { index, originalKey, keyOffset } = parseKeyFromPath(error.path);
263
263
  const newError = utils.relocatedError(error, [
@@ -265,15 +265,28 @@ function splitResult({ data, errors }, numResults) {
265
265
  ...error.path.slice(keyOffset)
266
266
  ]);
267
267
  const splittedResult = splitResults[index];
268
- if (splittedResult) {
269
- const resultErrors = splittedResult.errors ||= [];
270
- resultErrors.push(newError);
268
+ if (splittedResult == null) {
269
+ splitResults[index] = { errors: [newError] };
270
+ continue;
271
+ } else if (splittedResult.errors == null) {
272
+ splittedResult.errors = [newError];
273
+ continue;
274
+ } else {
275
+ splittedResult.errors.push(newError);
271
276
  }
272
277
  } else {
273
- splitResults.forEach((result) => {
274
- const resultErrors = result.errors ||= [];
275
- resultErrors.push(error);
276
- });
278
+ for (let i = 0; i < numResults; i++) {
279
+ const splittedResult = splitResults[i];
280
+ if (splittedResult == null) {
281
+ splitResults[i] = { errors: [error] };
282
+ continue;
283
+ } else if (splittedResult.errors == null) {
284
+ splittedResult.errors = [error];
285
+ continue;
286
+ } else {
287
+ splittedResult.errors.push(error);
288
+ }
289
+ }
277
290
  }
278
291
  }
279
292
  }
@@ -302,29 +315,12 @@ function createLoadFn(executor, extensionsReducer) {
302
315
  return function batchExecuteLoadFn(requests) {
303
316
  if (requests.length === 1 && requests[0]) {
304
317
  const request = requests[0];
305
- return promiseHelpers.fakePromise(
306
- promiseHelpers.handleMaybePromise(
307
- () => executor(request),
308
- (result) => [result],
309
- (err) => [err]
310
- )
311
- );
318
+ return promiseHelpers.fakePromise().then(() => executor(request)).catch((err) => err).then((res) => [res]);
312
319
  }
313
320
  const mergedRequests = mergeRequests(requests, extensionsReducer);
314
- return promiseHelpers.fakePromise(
315
- promiseHelpers.handleMaybePromise(
316
- () => executor(mergedRequests),
317
- (resultBatches) => {
318
- if (utils.isAsyncIterable(resultBatches)) {
319
- throw new Error(
320
- "Executor must not return incremental results for batching"
321
- );
322
- }
323
- return splitResult(resultBatches, requests.length);
324
- },
325
- (err) => requests.map(() => err)
326
- )
327
- );
321
+ return promiseHelpers.fakePromise().then(() => executor(mergedRequests)).then(
322
+ (resultBatches) => splitResult(resultBatches, requests.length)
323
+ ).catch((err) => requests.map(() => err));
328
324
  };
329
325
  }
330
326
  function defaultExtensionsReducer(mergedExtensions, request) {
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { getOperationASTFromRequest, relocatedError, isAsyncIterable, memoize2of4 } from '@graphql-tools/utils';
2
- import { fakePromise, handleMaybePromise } from '@whatwg-node/promise-helpers';
1
+ import { getOperationASTFromRequest, relocatedError, memoize2of4 } from '@graphql-tools/utils';
2
+ import { fakePromise } from '@whatwg-node/promise-helpers';
3
3
  import DataLoader from 'dataloader';
4
4
  import { Kind, visit } from 'graphql';
5
5
 
@@ -231,27 +231,27 @@ function isFragmentDefinition(def) {
231
231
  return def.kind === Kind.FRAGMENT_DEFINITION;
232
232
  }
233
233
 
234
- function splitResult({ data, errors }, numResults) {
235
- const splitResults = [];
236
- for (let i = 0; i < numResults; i++) {
237
- splitResults.push({});
238
- }
239
- if (data) {
240
- for (const prefixedKey in data) {
234
+ function splitResult(mergedResult, numResults) {
235
+ const splitResults = new Array(numResults);
236
+ if (mergedResult.data) {
237
+ for (const prefixedKey in mergedResult.data) {
241
238
  const { index, originalKey } = parseKey(prefixedKey);
242
239
  const result = splitResults[index];
243
240
  if (result == null) {
244
- continue;
245
- }
246
- if (result.data == null) {
247
- result.data = { [originalKey]: data[prefixedKey] };
241
+ splitResults[index] = {
242
+ data: {
243
+ [originalKey]: mergedResult.data[prefixedKey]
244
+ }
245
+ };
246
+ } else if (result.data == null) {
247
+ result.data = { [originalKey]: mergedResult.data[prefixedKey] };
248
248
  } else {
249
- result.data[originalKey] = data[prefixedKey];
249
+ result.data[originalKey] = mergedResult.data[prefixedKey];
250
250
  }
251
251
  }
252
252
  }
253
- if (errors) {
254
- for (const error of errors) {
253
+ if (mergedResult.errors) {
254
+ for (const error of mergedResult.errors) {
255
255
  if (error.path) {
256
256
  const { index, originalKey, keyOffset } = parseKeyFromPath(error.path);
257
257
  const newError = relocatedError(error, [
@@ -259,15 +259,28 @@ function splitResult({ data, errors }, numResults) {
259
259
  ...error.path.slice(keyOffset)
260
260
  ]);
261
261
  const splittedResult = splitResults[index];
262
- if (splittedResult) {
263
- const resultErrors = splittedResult.errors ||= [];
264
- resultErrors.push(newError);
262
+ if (splittedResult == null) {
263
+ splitResults[index] = { errors: [newError] };
264
+ continue;
265
+ } else if (splittedResult.errors == null) {
266
+ splittedResult.errors = [newError];
267
+ continue;
268
+ } else {
269
+ splittedResult.errors.push(newError);
265
270
  }
266
271
  } else {
267
- splitResults.forEach((result) => {
268
- const resultErrors = result.errors ||= [];
269
- resultErrors.push(error);
270
- });
272
+ for (let i = 0; i < numResults; i++) {
273
+ const splittedResult = splitResults[i];
274
+ if (splittedResult == null) {
275
+ splitResults[i] = { errors: [error] };
276
+ continue;
277
+ } else if (splittedResult.errors == null) {
278
+ splittedResult.errors = [error];
279
+ continue;
280
+ } else {
281
+ splittedResult.errors.push(error);
282
+ }
283
+ }
271
284
  }
272
285
  }
273
286
  }
@@ -296,29 +309,12 @@ function createLoadFn(executor, extensionsReducer) {
296
309
  return function batchExecuteLoadFn(requests) {
297
310
  if (requests.length === 1 && requests[0]) {
298
311
  const request = requests[0];
299
- return fakePromise(
300
- handleMaybePromise(
301
- () => executor(request),
302
- (result) => [result],
303
- (err) => [err]
304
- )
305
- );
312
+ return fakePromise().then(() => executor(request)).catch((err) => err).then((res) => [res]);
306
313
  }
307
314
  const mergedRequests = mergeRequests(requests, extensionsReducer);
308
- return fakePromise(
309
- handleMaybePromise(
310
- () => executor(mergedRequests),
311
- (resultBatches) => {
312
- if (isAsyncIterable(resultBatches)) {
313
- throw new Error(
314
- "Executor must not return incremental results for batching"
315
- );
316
- }
317
- return splitResult(resultBatches, requests.length);
318
- },
319
- (err) => requests.map(() => err)
320
- )
321
- );
315
+ return fakePromise().then(() => executor(mergedRequests)).then(
316
+ (resultBatches) => splitResult(resultBatches, requests.length)
317
+ ).catch((err) => requests.map(() => err));
322
318
  };
323
319
  }
324
320
  function defaultExtensionsReducer(mergedExtensions, request) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/batch-execute",
3
- "version": "9.0.16",
3
+ "version": "9.0.17-alpha-071d64e79465dc9de058cc16c99ec1a5472d2bc1",
4
4
  "type": "module",
5
5
  "description": "A set of utils for faster development of GraphQL tools",
6
6
  "repository": {