index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.enableGraphQLExtensions = exports.GraphQLExtensionStack = exports.GraphQLExtension = void 0;
  4. const graphql_1 = require("graphql");
  5. class GraphQLExtension {
  6. }
  7. exports.GraphQLExtension = GraphQLExtension;
  8. class GraphQLExtensionStack {
  9. constructor(extensions) {
  10. this.extensions = extensions;
  11. }
  12. requestDidStart(o) {
  13. return this.handleDidStart(ext => ext.requestDidStart && ext.requestDidStart(o));
  14. }
  15. parsingDidStart(o) {
  16. return this.handleDidStart(ext => ext.parsingDidStart && ext.parsingDidStart(o));
  17. }
  18. validationDidStart() {
  19. return this.handleDidStart(ext => ext.validationDidStart && ext.validationDidStart());
  20. }
  21. executionDidStart(o) {
  22. if (o.executionArgs.fieldResolver) {
  23. this.fieldResolver = o.executionArgs.fieldResolver;
  24. }
  25. return this.handleDidStart(ext => ext.executionDidStart && ext.executionDidStart(o));
  26. }
  27. didEncounterErrors(errors) {
  28. this.extensions.forEach(extension => {
  29. if (extension.didEncounterErrors) {
  30. extension.didEncounterErrors(errors);
  31. }
  32. });
  33. }
  34. willSendResponse(o) {
  35. let reference = o;
  36. [...this.extensions].reverse().forEach(extension => {
  37. if (extension.willSendResponse) {
  38. const result = extension.willSendResponse(reference);
  39. if (result) {
  40. reference = result;
  41. }
  42. }
  43. });
  44. return reference;
  45. }
  46. willResolveField(source, args, context, info) {
  47. const handlers = this.extensions
  48. .map(extension => extension.willResolveField &&
  49. extension.willResolveField(source, args, context, info))
  50. .filter(x => x)
  51. .reverse();
  52. return (error, result) => {
  53. for (const handler of handlers) {
  54. handler(error, result);
  55. }
  56. };
  57. }
  58. format() {
  59. return this.extensions
  60. .map(extension => extension.format && extension.format())
  61. .filter(x => x).reduce((extensions, [key, value]) => Object.assign(extensions, { [key]: value }), {});
  62. }
  63. handleDidStart(startInvoker) {
  64. const endHandlers = [];
  65. this.extensions.forEach(extension => {
  66. try {
  67. const endHandler = startInvoker(extension);
  68. if (endHandler) {
  69. endHandlers.push(endHandler);
  70. }
  71. }
  72. catch (error) {
  73. console.error(error);
  74. }
  75. });
  76. return (...errors) => {
  77. endHandlers.reverse();
  78. for (const endHandler of endHandlers) {
  79. try {
  80. endHandler(...errors);
  81. }
  82. catch (error) {
  83. console.error(error);
  84. }
  85. }
  86. };
  87. }
  88. }
  89. exports.GraphQLExtensionStack = GraphQLExtensionStack;
  90. function enableGraphQLExtensions(schema) {
  91. if (schema._extensionsEnabled) {
  92. return schema;
  93. }
  94. schema._extensionsEnabled = true;
  95. forEachField(schema, wrapField);
  96. return schema;
  97. }
  98. exports.enableGraphQLExtensions = enableGraphQLExtensions;
  99. function wrapField(field) {
  100. const fieldResolver = field.resolve;
  101. field.resolve = (source, args, context, info) => {
  102. const parentPath = info.path.prev;
  103. const extensionStack = context && context._extensionStack;
  104. const handler = (extensionStack &&
  105. extensionStack.willResolveField(source, args, context, info)) ||
  106. ((_err, _result) => {
  107. });
  108. const resolveObject = info.parentType.resolveObject;
  109. let whenObjectResolved;
  110. if (parentPath && resolveObject) {
  111. if (!parentPath.__fields) {
  112. parentPath.__fields = {};
  113. }
  114. parentPath.__fields[info.fieldName] = info.fieldNodes;
  115. whenObjectResolved = parentPath.__whenObjectResolved;
  116. if (!whenObjectResolved) {
  117. whenObjectResolved = Promise.resolve().then(() => {
  118. return resolveObject(source, parentPath.__fields, context, info);
  119. });
  120. parentPath.__whenObjectResolved = whenObjectResolved;
  121. }
  122. }
  123. try {
  124. const actualFieldResolver = fieldResolver ||
  125. (extensionStack && extensionStack.fieldResolver) ||
  126. graphql_1.defaultFieldResolver;
  127. let result;
  128. if (whenObjectResolved) {
  129. result = whenObjectResolved.then((resolvedObject) => {
  130. return actualFieldResolver(resolvedObject, args, context, info);
  131. });
  132. }
  133. else {
  134. result = actualFieldResolver(source, args, context, info);
  135. }
  136. whenResultIsFinished(result, handler);
  137. return result;
  138. }
  139. catch (error) {
  140. handler(error);
  141. throw error;
  142. }
  143. };
  144. }
  145. function isPromise(x) {
  146. return x && typeof x.then === 'function';
  147. }
  148. function whenResultIsFinished(result, callback) {
  149. if (isPromise(result)) {
  150. result.then((r) => callback(null, r), (err) => callback(err));
  151. }
  152. else if (Array.isArray(result)) {
  153. if (result.some(isPromise)) {
  154. Promise.all(result).then((r) => callback(null, r), (err) => callback(err));
  155. }
  156. else {
  157. callback(null, result);
  158. }
  159. }
  160. else {
  161. callback(null, result);
  162. }
  163. }
  164. function forEachField(schema, fn) {
  165. const typeMap = schema.getTypeMap();
  166. Object.keys(typeMap).forEach(typeName => {
  167. const type = typeMap[typeName];
  168. if (!graphql_1.getNamedType(type).name.startsWith('__') &&
  169. type instanceof graphql_1.GraphQLObjectType) {
  170. const fields = type.getFields();
  171. Object.keys(fields).forEach(fieldName => {
  172. const field = fields[fieldName];
  173. fn(field, typeName, fieldName);
  174. });
  175. }
  176. });
  177. }
  178. //# sourceMappingURL=index.js.map