index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.inspect = void 0;
  4. // Taken from graphql-js
  5. // https://github.com/graphql/graphql-js/blob/main/src/jsutils/inspect.ts
  6. const MAX_RECURSIVE_DEPTH = 3;
  7. /**
  8. * Used to print values in error messages.
  9. */
  10. function inspect(value) {
  11. return formatValue(value, []);
  12. }
  13. exports.inspect = inspect;
  14. function formatValue(value, seenValues) {
  15. switch (typeof value) {
  16. case 'string':
  17. return JSON.stringify(value);
  18. case 'function':
  19. return value.name ? `[function ${value.name}]` : '[function]';
  20. case 'object':
  21. return formatObjectValue(value, seenValues);
  22. default:
  23. return String(value);
  24. }
  25. }
  26. function formatError(value) {
  27. // eslint-disable-next-line no-constant-condition
  28. if ((value.name = 'GraphQLError')) {
  29. return value.toString();
  30. }
  31. return `${value.name}: ${value.message};\n ${value.stack}`;
  32. }
  33. function formatObjectValue(value, previouslySeenValues) {
  34. if (value === null) {
  35. return 'null';
  36. }
  37. if (value instanceof Error) {
  38. if (value.name === 'AggregateError') {
  39. return (formatError(value) +
  40. '\n' +
  41. formatArray(value.errors, previouslySeenValues));
  42. }
  43. return formatError(value);
  44. }
  45. if (previouslySeenValues.includes(value)) {
  46. return '[Circular]';
  47. }
  48. const seenValues = [...previouslySeenValues, value];
  49. if (isJSONable(value)) {
  50. const jsonValue = value.toJSON();
  51. // check for infinite recursion
  52. if (jsonValue !== value) {
  53. return typeof jsonValue === 'string' ? jsonValue : formatValue(jsonValue, seenValues);
  54. }
  55. }
  56. else if (Array.isArray(value)) {
  57. return formatArray(value, seenValues);
  58. }
  59. return formatObject(value, seenValues);
  60. }
  61. function isJSONable(value) {
  62. return typeof value.toJSON === 'function';
  63. }
  64. function formatObject(object, seenValues) {
  65. const entries = Object.entries(object);
  66. if (entries.length === 0) {
  67. return '{}';
  68. }
  69. if (seenValues.length > MAX_RECURSIVE_DEPTH) {
  70. return '[' + getObjectTag(object) + ']';
  71. }
  72. const properties = entries.map(([key, value]) => key + ': ' + formatValue(value, seenValues));
  73. return '{ ' + properties.join(', ') + ' }';
  74. }
  75. function formatArray(array, seenValues) {
  76. if (array.length === 0) {
  77. return '[]';
  78. }
  79. if (seenValues.length > MAX_RECURSIVE_DEPTH) {
  80. return '[Array]';
  81. }
  82. const len = array.length;
  83. const items = [];
  84. for (let i = 0; i < len; ++i) {
  85. items.push(formatValue(array[i], seenValues));
  86. }
  87. return '[' + items.join(', ') + ']';
  88. }
  89. function getObjectTag(object) {
  90. const tag = Object.prototype.toString
  91. .call(object)
  92. .replace(/^\[object /, '')
  93. .replace(/]$/, '');
  94. if (tag === 'Object' && typeof object.constructor === 'function') {
  95. const name = object.constructor.name;
  96. if (typeof name === 'string' && name !== '') {
  97. return name;
  98. }
  99. }
  100. return tag;
  101. }