schemaInstrumentation.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.whenResultIsFinished = exports.enablePluginsForSchemaResolvers = exports.symbolPluginsEnabled = exports.symbolUserFieldResolver = exports.symbolExecutionDispatcherWillResolveField = void 0;
  4. const type_1 = require("graphql/type");
  5. const execution_1 = require("graphql/execution");
  6. exports.symbolExecutionDispatcherWillResolveField = Symbol("apolloServerExecutionDispatcherWillResolveField");
  7. exports.symbolUserFieldResolver = Symbol("apolloServerUserFieldResolver");
  8. exports.symbolPluginsEnabled = Symbol("apolloServerPluginsEnabled");
  9. function enablePluginsForSchemaResolvers(schema) {
  10. if (schema[exports.symbolPluginsEnabled]) {
  11. return schema;
  12. }
  13. Object.defineProperty(schema, exports.symbolPluginsEnabled, {
  14. value: true,
  15. });
  16. forEachField(schema, wrapField);
  17. return schema;
  18. }
  19. exports.enablePluginsForSchemaResolvers = enablePluginsForSchemaResolvers;
  20. function wrapField(field) {
  21. const originalFieldResolve = field.resolve;
  22. field.resolve = (source, args, context, info) => {
  23. const parentPath = info.path.prev;
  24. const willResolveField = context === null || context === void 0 ? void 0 : context[exports.symbolExecutionDispatcherWillResolveField];
  25. const userFieldResolver = context === null || context === void 0 ? void 0 : context[exports.symbolUserFieldResolver];
  26. const didResolveField = typeof willResolveField === 'function' &&
  27. willResolveField({ source, args, context, info });
  28. const resolveObject = info.parentType.resolveObject;
  29. let whenObjectResolved;
  30. if (parentPath && resolveObject) {
  31. if (!parentPath.__fields) {
  32. parentPath.__fields = {};
  33. }
  34. parentPath.__fields[info.fieldName] = info.fieldNodes;
  35. whenObjectResolved = parentPath.__whenObjectResolved;
  36. if (!whenObjectResolved) {
  37. whenObjectResolved = Promise.resolve().then(() => {
  38. return resolveObject(source, parentPath.__fields, context, info);
  39. });
  40. parentPath.__whenObjectResolved = whenObjectResolved;
  41. }
  42. }
  43. const fieldResolver = originalFieldResolve || userFieldResolver || execution_1.defaultFieldResolver;
  44. try {
  45. let result;
  46. if (whenObjectResolved) {
  47. result = whenObjectResolved.then((resolvedObject) => {
  48. return fieldResolver(resolvedObject, args, context, info);
  49. });
  50. }
  51. else {
  52. result = fieldResolver(source, args, context, info);
  53. }
  54. if (typeof didResolveField === "function") {
  55. whenResultIsFinished(result, didResolveField);
  56. }
  57. return result;
  58. }
  59. catch (error) {
  60. if (typeof didResolveField === "function") {
  61. didResolveField(error);
  62. }
  63. throw error;
  64. }
  65. };
  66. ;
  67. }
  68. function isPromise(x) {
  69. return x && typeof x.then === 'function';
  70. }
  71. function whenResultIsFinished(result, callback) {
  72. if (isPromise(result)) {
  73. result.then((r) => callback(null, r), (err) => callback(err));
  74. }
  75. else if (Array.isArray(result)) {
  76. if (result.some(isPromise)) {
  77. Promise.all(result).then((r) => callback(null, r), (err) => callback(err));
  78. }
  79. else {
  80. callback(null, result);
  81. }
  82. }
  83. else {
  84. callback(null, result);
  85. }
  86. }
  87. exports.whenResultIsFinished = whenResultIsFinished;
  88. function forEachField(schema, fn) {
  89. const typeMap = schema.getTypeMap();
  90. Object.entries(typeMap).forEach(([typeName, type]) => {
  91. if (!type_1.getNamedType(type).name.startsWith('__') &&
  92. type instanceof type_1.GraphQLObjectType) {
  93. const fields = type.getFields();
  94. Object.entries(fields).forEach(([fieldName, field]) => {
  95. fn(field, typeName, fieldName);
  96. });
  97. }
  98. });
  99. }
  100. //# sourceMappingURL=schemaInstrumentation.js.map