pluginTestHarness.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const type_1 = require("graphql/type");
  13. const schemaInstrumentation_1 = require("./schemaInstrumentation");
  14. const apollo_server_caching_1 = require("apollo-server-caching");
  15. const dispatcher_1 = require("./dispatcher");
  16. const schemaHash_1 = require("./schemaHash");
  17. const graphql_1 = require("graphql");
  18. function pluginTestHarness({ pluginInstance, schema, logger, graphqlRequest, overallCachePolicy, executor, context = Object.create(null) }) {
  19. var _a;
  20. return __awaiter(this, void 0, void 0, function* () {
  21. if (!schema) {
  22. schema = new type_1.GraphQLSchema({
  23. query: new type_1.GraphQLObjectType({
  24. name: 'RootQueryType',
  25. fields: {
  26. hello: {
  27. type: type_1.GraphQLString,
  28. resolve() {
  29. return 'hello world';
  30. }
  31. }
  32. }
  33. })
  34. });
  35. }
  36. const schemaHash = schemaHash_1.generateSchemaHash(schema);
  37. let serverListener;
  38. if (typeof pluginInstance.serverWillStart === 'function') {
  39. const maybeServerListener = yield pluginInstance.serverWillStart({
  40. logger: logger || console,
  41. schema,
  42. schemaHash,
  43. serverlessFramework: false,
  44. apollo: {
  45. key: 'some-key',
  46. graphRef: 'graph@current',
  47. graphId: 'graph',
  48. graphVariant: 'current',
  49. },
  50. engine: {},
  51. });
  52. if (maybeServerListener && maybeServerListener.serverWillStop) {
  53. serverListener = maybeServerListener;
  54. }
  55. }
  56. const requestContext = {
  57. logger: logger || console,
  58. schema,
  59. schemaHash: schemaHash_1.generateSchemaHash(schema),
  60. request: graphqlRequest,
  61. metrics: Object.create(null),
  62. source: graphqlRequest.query,
  63. cache: new apollo_server_caching_1.InMemoryLRUCache(),
  64. context,
  65. };
  66. if (requestContext.source === undefined) {
  67. throw new Error("No source provided for test");
  68. }
  69. requestContext.overallCachePolicy = overallCachePolicy;
  70. if (typeof pluginInstance.requestDidStart !== "function") {
  71. throw new Error("This test harness expects this to be defined.");
  72. }
  73. const listener = pluginInstance.requestDidStart(requestContext);
  74. const dispatcher = new dispatcher_1.Dispatcher(listener ? [listener] : []);
  75. const executionListeners = [];
  76. yield dispatcher.invokeHookAsync('didResolveSource', requestContext);
  77. if (!requestContext.document) {
  78. yield dispatcher.invokeDidStartHook('parsingDidStart', requestContext);
  79. try {
  80. requestContext.document = graphql_1.parse(requestContext.source, undefined);
  81. }
  82. catch (syntaxError) {
  83. const errorOrErrors = syntaxError;
  84. requestContext.errors = Array.isArray(errorOrErrors)
  85. ? errorOrErrors
  86. : [errorOrErrors];
  87. yield dispatcher.invokeHookAsync('didEncounterErrors', requestContext);
  88. return requestContext;
  89. }
  90. const validationDidEnd = yield dispatcher.invokeDidStartHook('validationDidStart', requestContext);
  91. const validationErrors = graphql_1.validate(requestContext.schema, requestContext.document);
  92. if (validationErrors.length !== 0) {
  93. requestContext.errors = validationErrors;
  94. validationDidEnd(validationErrors);
  95. yield dispatcher.invokeHookAsync('didEncounterErrors', requestContext);
  96. return requestContext;
  97. }
  98. else {
  99. validationDidEnd();
  100. }
  101. }
  102. const operation = graphql_1.getOperationAST(requestContext.document, requestContext.request.operationName);
  103. requestContext.operation = operation || undefined;
  104. requestContext.operationName =
  105. (operation && operation.name && operation.name.value) || null;
  106. yield dispatcher.invokeHookAsync('didResolveOperation', requestContext);
  107. dispatcher.invokeHookSync('executionDidStart', requestContext).forEach(executionListener => {
  108. if (typeof executionListener === 'function') {
  109. executionListeners.push({
  110. executionDidEnd: executionListener,
  111. });
  112. }
  113. else if (typeof executionListener === 'object') {
  114. executionListeners.push(executionListener);
  115. }
  116. });
  117. const executionDispatcher = new dispatcher_1.Dispatcher(executionListeners);
  118. const invokeWillResolveField = (...args) => executionDispatcher.invokeDidStartHook('willResolveField', ...args);
  119. Object.defineProperty(requestContext.context, schemaInstrumentation_1.symbolExecutionDispatcherWillResolveField, { value: invokeWillResolveField });
  120. schemaInstrumentation_1.enablePluginsForSchemaResolvers(schema);
  121. try {
  122. requestContext.response = yield executor(requestContext);
  123. executionDispatcher.reverseInvokeHookSync("executionDidEnd");
  124. }
  125. catch (executionErr) {
  126. executionDispatcher.reverseInvokeHookSync("executionDidEnd", executionErr);
  127. }
  128. yield dispatcher.invokeHookAsync("willSendResponse", requestContext);
  129. yield ((_a = serverListener === null || serverListener === void 0 ? void 0 : serverListener.serverWillStop) === null || _a === void 0 ? void 0 : _a.call(serverListener));
  130. return requestContext;
  131. });
  132. }
  133. exports.default = pluginTestHarness;
  134. //# sourceMappingURL=pluginTestHarness.js.map