errors.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. extendStatics(d, b);
  10. function __() { this.constructor = d; }
  11. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  12. };
  13. })();
  14. var __assign = (this && this.__assign) || function () {
  15. __assign = Object.assign || function(t) {
  16. for (var s, i = 1, n = arguments.length; i < n; i++) {
  17. s = arguments[i];
  18. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  19. t[p] = s[p];
  20. }
  21. return t;
  22. };
  23. return __assign.apply(this, arguments);
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. var graphql_1 = require("graphql");
  27. var error_1 = require("graphql/error");
  28. var getResponseKeyFromInfo_1 = require("./getResponseKeyFromInfo");
  29. if ((typeof global !== 'undefined' && 'Symbol' in global) ||
  30. (typeof window !== 'undefined' && 'Symbol' in window)) {
  31. exports.ERROR_SYMBOL = Symbol('subSchemaErrors');
  32. }
  33. else {
  34. exports.ERROR_SYMBOL = '@@__subSchemaErrors';
  35. }
  36. function annotateWithChildrenErrors(object, childrenErrors) {
  37. var _a;
  38. if (!childrenErrors || childrenErrors.length === 0) {
  39. // Nothing to see here, move along
  40. return object;
  41. }
  42. if (Array.isArray(object)) {
  43. var byIndex_1 = {};
  44. childrenErrors.forEach(function (error) {
  45. if (!error.path) {
  46. return;
  47. }
  48. var index = error.path[1];
  49. var current = byIndex_1[index] || [];
  50. current.push(__assign(__assign({}, error), { path: error.path.slice(1) }));
  51. byIndex_1[index] = current;
  52. });
  53. return object.map(function (item, index) { return annotateWithChildrenErrors(item, byIndex_1[index]); });
  54. }
  55. return __assign(__assign({}, object), (_a = {}, _a[exports.ERROR_SYMBOL] = childrenErrors.map(function (error) { return (__assign(__assign({}, error), (error.path ? { path: error.path.slice(1) } : {}))); }), _a));
  56. }
  57. exports.annotateWithChildrenErrors = annotateWithChildrenErrors;
  58. function getErrorsFromParent(object, fieldName) {
  59. var errors = (object && object[exports.ERROR_SYMBOL]) || [];
  60. var childrenErrors = [];
  61. for (var _i = 0, errors_1 = errors; _i < errors_1.length; _i++) {
  62. var error = errors_1[_i];
  63. if (!error.path || (error.path.length === 1 && error.path[0] === fieldName)) {
  64. return {
  65. kind: 'OWN',
  66. error: error
  67. };
  68. }
  69. else if (error.path[0] === fieldName) {
  70. childrenErrors.push(error);
  71. }
  72. }
  73. return {
  74. kind: 'CHILDREN',
  75. errors: childrenErrors
  76. };
  77. }
  78. exports.getErrorsFromParent = getErrorsFromParent;
  79. var CombinedError = /** @class */ (function (_super) {
  80. __extends(CombinedError, _super);
  81. function CombinedError(message, errors) {
  82. var _this = _super.call(this, message) || this;
  83. _this.errors = errors;
  84. return _this;
  85. }
  86. return CombinedError;
  87. }(Error));
  88. function checkResultAndHandleErrors(result, info, responseKey) {
  89. if (!responseKey) {
  90. responseKey = getResponseKeyFromInfo_1.getResponseKeyFromInfo(info);
  91. }
  92. if (result.errors && (!result.data || result.data[responseKey] == null)) {
  93. // apollo-link-http & http-link-dataloader need the
  94. // result property to be passed through for better error handling.
  95. // If there is only one error, which contains a result property, pass the error through
  96. var newError = result.errors.length === 1 && hasResult(result.errors[0])
  97. ? result.errors[0]
  98. : new CombinedError(concatErrors(result.errors), result.errors);
  99. throw error_1.locatedError(newError, info.fieldNodes, graphql_1.responsePathAsArray(info.path));
  100. }
  101. var resultObject = result.data[responseKey];
  102. if (result.errors) {
  103. resultObject = annotateWithChildrenErrors(resultObject, result.errors);
  104. }
  105. return resultObject;
  106. }
  107. exports.checkResultAndHandleErrors = checkResultAndHandleErrors;
  108. function concatErrors(errors) {
  109. return errors.map(function (error) { return error.message; }).join('\n');
  110. }
  111. function hasResult(error) {
  112. return error.result || error.extensions || (error.originalError && error.originalError.result);
  113. }
  114. //# sourceMappingURL=errors.js.map