@graphql-tools/executor 1.2.5 → 1.2.6-alpha-20240408104556-2aa181653bea4c5ee87f6cd99263dee40fa63a5b

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.
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.coerceError = void 0;
4
+ function coerceError(error) {
5
+ if (error instanceof Error) {
6
+ return error;
7
+ }
8
+ if (typeof error === 'object' && error != null) {
9
+ if ('message' in error && typeof error.message === 'string') {
10
+ let errorOptions;
11
+ if ('cause' in error) {
12
+ errorOptions = { cause: error.cause };
13
+ }
14
+ const coercedError = new Error(error.message, errorOptions);
15
+ if ('stack' in error && typeof error.stack === 'string') {
16
+ coercedError.stack = error.stack;
17
+ }
18
+ if ('name' in error && typeof error.name === 'string') {
19
+ coercedError.name = error.name;
20
+ }
21
+ return coercedError;
22
+ }
23
+ }
24
+ return new Error(String(error));
25
+ }
26
+ exports.coerceError = coerceError;
@@ -4,6 +4,7 @@ exports.isIncrementalResult = exports.getFieldDef = exports.flattenIncrementalRe
4
4
  const graphql_1 = require("graphql");
5
5
  const value_or_promise_1 = require("value-or-promise");
6
6
  const utils_1 = require("@graphql-tools/utils");
7
+ const coerceError_js_1 = require("./coerceError.js");
7
8
  const flattenAsyncIterable_js_1 = require("./flattenAsyncIterable.js");
8
9
  const invariant_js_1 = require("./invariant.js");
9
10
  const promiseForObject_js_1 = require("./promiseForObject.js");
@@ -327,6 +328,7 @@ function executeField(exeContext, parentType, source, fieldNodes, path, asyncPay
327
328
  // Note: we don't rely on a `catch` method, but we do expect "thenable"
328
329
  // to take a second callback for the error case.
329
330
  return completed.then(undefined, rawError => {
331
+ rawError = (0, coerceError_js_1.coerceError)(rawError);
330
332
  const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(path));
331
333
  const handledError = handleFieldError(error, returnType, errors);
332
334
  filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
@@ -336,7 +338,8 @@ function executeField(exeContext, parentType, source, fieldNodes, path, asyncPay
336
338
  return completed;
337
339
  }
338
340
  catch (rawError) {
339
- const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(path));
341
+ const coercedError = (0, coerceError_js_1.coerceError)(rawError);
342
+ const error = (0, graphql_1.locatedError)(coercedError, fieldNodes, (0, utils_1.pathToArray)(path));
340
343
  const handledError = handleFieldError(error, returnType, errors);
341
344
  filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
342
345
  return handledError;
@@ -488,7 +491,8 @@ async function completeAsyncIteratorValue(exeContext, itemType, fieldNodes, info
488
491
  }
489
492
  }
490
493
  catch (rawError) {
491
- const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
494
+ const coercedError = (0, coerceError_js_1.coerceError)(rawError);
495
+ const error = (0, graphql_1.locatedError)(coercedError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
492
496
  completedResults.push(handleFieldError(error, itemType, errors));
493
497
  break;
494
498
  }
@@ -554,6 +558,7 @@ function completeListItemValue(item, completedResults, errors, exeContext, itemT
554
558
  // Note: we don't rely on a `catch` method, but we do expect "thenable"
555
559
  // to take a second callback for the error case.
556
560
  completedResults.push(completedItem.then(undefined, rawError => {
561
+ rawError = (0, coerceError_js_1.coerceError)(rawError);
557
562
  const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
558
563
  const handledError = handleFieldError(error, itemType, errors);
559
564
  filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
@@ -564,7 +569,8 @@ function completeListItemValue(item, completedResults, errors, exeContext, itemT
564
569
  completedResults.push(completedItem);
565
570
  }
566
571
  catch (rawError) {
567
- const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
572
+ const coercedError = (0, coerceError_js_1.coerceError)(rawError);
573
+ const error = (0, graphql_1.locatedError)(coercedError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
568
574
  const handledError = handleFieldError(error, itemType, errors);
569
575
  filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
570
576
  completedResults.push(handledError);
@@ -969,6 +975,7 @@ function executeStreamField(path, itemPath, item, exeContext, fieldNodes, info,
969
975
  // Note: we don't rely on a `catch` method, but we do expect "thenable"
970
976
  // to take a second callback for the error case.
971
977
  completedItem = completedItem.then(undefined, rawError => {
978
+ rawError = (0, coerceError_js_1.coerceError)(rawError);
972
979
  const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
973
980
  const handledError = handleFieldError(error, itemType, asyncPayloadRecord.errors);
974
981
  filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
@@ -977,7 +984,8 @@ function executeStreamField(path, itemPath, item, exeContext, fieldNodes, info,
977
984
  }
978
985
  }
979
986
  catch (rawError) {
980
- const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
987
+ const coercedError = (0, coerceError_js_1.coerceError)(rawError);
988
+ const error = (0, graphql_1.locatedError)(coercedError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
981
989
  completedItem = handleFieldError(error, itemType, asyncPayloadRecord.errors);
982
990
  filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
983
991
  }
@@ -1013,7 +1021,8 @@ async function executeStreamIteratorItem(iterator, exeContext, fieldNodes, info,
1013
1021
  item = value;
1014
1022
  }
1015
1023
  catch (rawError) {
1016
- const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
1024
+ const coercedError = (0, coerceError_js_1.coerceError)(rawError);
1025
+ const error = (0, graphql_1.locatedError)(coercedError, fieldNodes, (0, utils_1.pathToArray)(itemPath));
1017
1026
  const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
1018
1027
  // don't continue if iterator throws
1019
1028
  return { done: true, value };
@@ -0,0 +1,22 @@
1
+ export function coerceError(error) {
2
+ if (error instanceof Error) {
3
+ return error;
4
+ }
5
+ if (typeof error === 'object' && error != null) {
6
+ if ('message' in error && typeof error.message === 'string') {
7
+ let errorOptions;
8
+ if ('cause' in error) {
9
+ errorOptions = { cause: error.cause };
10
+ }
11
+ const coercedError = new Error(error.message, errorOptions);
12
+ if ('stack' in error && typeof error.stack === 'string') {
13
+ coercedError.stack = error.stack;
14
+ }
15
+ if ('name' in error && typeof error.name === 'string') {
16
+ coercedError.name = error.name;
17
+ }
18
+ return coercedError;
19
+ }
20
+ }
21
+ return new Error(String(error));
22
+ }
@@ -1,6 +1,7 @@
1
1
  import { assertValidSchema, getDirectiveValues, GraphQLError, isAbstractType, isLeafType, isListType, isNonNullType, isObjectType, Kind, locatedError, SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef, } from 'graphql';
2
2
  import { ValueOrPromise } from 'value-or-promise';
3
3
  import { collectSubFields as _collectSubfields, addPath, collectFields, createGraphQLError, getArgumentValues, getDefinedRootType, GraphQLStreamDirective, inspect, isAsyncIterable, isIterableObject, isObjectLike, isPromise, mapAsyncIterator, memoize1, memoize3, pathToArray, promiseReduce, } from '@graphql-tools/utils';
4
+ import { coerceError } from './coerceError.js';
4
5
  import { flattenAsyncIterable } from './flattenAsyncIterable.js';
5
6
  import { invariant } from './invariant.js';
6
7
  import { promiseForObject } from './promiseForObject.js';
@@ -320,6 +321,7 @@ function executeField(exeContext, parentType, source, fieldNodes, path, asyncPay
320
321
  // Note: we don't rely on a `catch` method, but we do expect "thenable"
321
322
  // to take a second callback for the error case.
322
323
  return completed.then(undefined, rawError => {
324
+ rawError = coerceError(rawError);
323
325
  const error = locatedError(rawError, fieldNodes, pathToArray(path));
324
326
  const handledError = handleFieldError(error, returnType, errors);
325
327
  filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
@@ -329,7 +331,8 @@ function executeField(exeContext, parentType, source, fieldNodes, path, asyncPay
329
331
  return completed;
330
332
  }
331
333
  catch (rawError) {
332
- const error = locatedError(rawError, fieldNodes, pathToArray(path));
334
+ const coercedError = coerceError(rawError);
335
+ const error = locatedError(coercedError, fieldNodes, pathToArray(path));
333
336
  const handledError = handleFieldError(error, returnType, errors);
334
337
  filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
335
338
  return handledError;
@@ -480,7 +483,8 @@ async function completeAsyncIteratorValue(exeContext, itemType, fieldNodes, info
480
483
  }
481
484
  }
482
485
  catch (rawError) {
483
- const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
486
+ const coercedError = coerceError(rawError);
487
+ const error = locatedError(coercedError, fieldNodes, pathToArray(itemPath));
484
488
  completedResults.push(handleFieldError(error, itemType, errors));
485
489
  break;
486
490
  }
@@ -546,6 +550,7 @@ function completeListItemValue(item, completedResults, errors, exeContext, itemT
546
550
  // Note: we don't rely on a `catch` method, but we do expect "thenable"
547
551
  // to take a second callback for the error case.
548
552
  completedResults.push(completedItem.then(undefined, rawError => {
553
+ rawError = coerceError(rawError);
549
554
  const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
550
555
  const handledError = handleFieldError(error, itemType, errors);
551
556
  filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
@@ -556,7 +561,8 @@ function completeListItemValue(item, completedResults, errors, exeContext, itemT
556
561
  completedResults.push(completedItem);
557
562
  }
558
563
  catch (rawError) {
559
- const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
564
+ const coercedError = coerceError(rawError);
565
+ const error = locatedError(coercedError, fieldNodes, pathToArray(itemPath));
560
566
  const handledError = handleFieldError(error, itemType, errors);
561
567
  filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
562
568
  completedResults.push(handledError);
@@ -957,6 +963,7 @@ function executeStreamField(path, itemPath, item, exeContext, fieldNodes, info,
957
963
  // Note: we don't rely on a `catch` method, but we do expect "thenable"
958
964
  // to take a second callback for the error case.
959
965
  completedItem = completedItem.then(undefined, rawError => {
966
+ rawError = coerceError(rawError);
960
967
  const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
961
968
  const handledError = handleFieldError(error, itemType, asyncPayloadRecord.errors);
962
969
  filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
@@ -965,7 +972,8 @@ function executeStreamField(path, itemPath, item, exeContext, fieldNodes, info,
965
972
  }
966
973
  }
967
974
  catch (rawError) {
968
- const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
975
+ const coercedError = coerceError(rawError);
976
+ const error = locatedError(coercedError, fieldNodes, pathToArray(itemPath));
969
977
  completedItem = handleFieldError(error, itemType, asyncPayloadRecord.errors);
970
978
  filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
971
979
  }
@@ -1001,7 +1009,8 @@ async function executeStreamIteratorItem(iterator, exeContext, fieldNodes, info,
1001
1009
  item = value;
1002
1010
  }
1003
1011
  catch (rawError) {
1004
- const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1012
+ const coercedError = coerceError(rawError);
1013
+ const error = locatedError(coercedError, fieldNodes, pathToArray(itemPath));
1005
1014
  const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
1006
1015
  // don't continue if iterator throws
1007
1016
  return { done: true, value };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/executor",
3
- "version": "1.2.5",
3
+ "version": "1.2.6-alpha-20240408104556-2aa181653bea4c5ee87f6cd99263dee40fa63a5b",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
@@ -0,0 +1 @@
1
+ export declare function coerceError(error: unknown): Error;
@@ -0,0 +1 @@
1
+ export declare function coerceError(error: unknown): Error;