index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ApolloServerPluginInlineTraceDisabled = exports.ApolloServerPluginInlineTrace = void 0;
  4. const apollo_reporting_protobuf_1 = require("apollo-reporting-protobuf");
  5. const traceTreeBuilder_1 = require("../traceTreeBuilder");
  6. const schemaIsFederated_1 = require("../schemaIsFederated");
  7. function ApolloServerPluginInlineTrace(options = Object.create(null)) {
  8. let enabled = options.__onlyIfSchemaIsFederated ? null : true;
  9. return {
  10. __internal_plugin_id__() {
  11. return 'InlineTrace';
  12. },
  13. serverWillStart({ schema, logger }) {
  14. if (enabled === null) {
  15. enabled = schemaIsFederated_1.schemaIsFederated(schema);
  16. if (enabled) {
  17. logger.info('Enabling inline tracing for this federated service. To disable, use ' +
  18. 'ApolloServerPluginInlineTraceDisabled.');
  19. }
  20. }
  21. },
  22. requestDidStart({ request: { http } }) {
  23. if (!enabled) {
  24. return;
  25. }
  26. const treeBuilder = new traceTreeBuilder_1.TraceTreeBuilder({
  27. rewriteError: options.rewriteError,
  28. });
  29. if ((http === null || http === void 0 ? void 0 : http.headers.get('apollo-federation-include-trace')) !== 'ftv1') {
  30. return;
  31. }
  32. treeBuilder.startTiming();
  33. return {
  34. executionDidStart: () => ({
  35. willResolveField({ info }) {
  36. return treeBuilder.willResolveField(info);
  37. },
  38. }),
  39. didEncounterErrors({ errors }) {
  40. treeBuilder.didEncounterErrors(errors);
  41. },
  42. willSendResponse({ response }) {
  43. treeBuilder.stopTiming();
  44. const encodedUint8Array = apollo_reporting_protobuf_1.Trace.encode(treeBuilder.trace).finish();
  45. const encodedBuffer = Buffer.from(encodedUint8Array, encodedUint8Array.byteOffset, encodedUint8Array.byteLength);
  46. const extensions = response.extensions || (response.extensions = Object.create(null));
  47. if (typeof extensions.ftv1 !== 'undefined') {
  48. throw new Error('The `ftv1` extension was already present.');
  49. }
  50. extensions.ftv1 = encodedBuffer.toString('base64');
  51. },
  52. };
  53. },
  54. };
  55. }
  56. exports.ApolloServerPluginInlineTrace = ApolloServerPluginInlineTrace;
  57. function ApolloServerPluginInlineTraceDisabled() {
  58. return {
  59. __internal_plugin_id__() {
  60. return 'InlineTrace';
  61. },
  62. };
  63. }
  64. exports.ApolloServerPluginInlineTraceDisabled = ApolloServerPluginInlineTraceDisabled;
  65. //# sourceMappingURL=index.js.map