index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.plugin = void 0;
  4. const graphql_1 = require("graphql");
  5. const { name: PACKAGE_NAME } = require("../package.json");
  6. exports.plugin = (_futureOptions = {}) => () => ({
  7. requestDidStart() {
  8. const startWallTime = new Date();
  9. let endWallTime;
  10. const startHrTime = process.hrtime();
  11. let duration;
  12. const resolverCalls = [];
  13. return {
  14. executionDidStart: () => ({
  15. executionDidEnd: () => {
  16. duration = process.hrtime(startHrTime);
  17. endWallTime = new Date();
  18. },
  19. willResolveField({ info }) {
  20. const resolverCall = {
  21. path: info.path,
  22. fieldName: info.fieldName,
  23. parentType: info.parentType,
  24. returnType: info.returnType,
  25. startOffset: process.hrtime(startHrTime),
  26. };
  27. resolverCalls.push(resolverCall);
  28. return () => {
  29. resolverCall.endOffset = process.hrtime(startHrTime);
  30. };
  31. },
  32. }),
  33. willSendResponse({ response }) {
  34. if (typeof endWallTime === 'undefined' ||
  35. typeof duration === 'undefined') {
  36. return;
  37. }
  38. const extensions = response.extensions || (response.extensions = Object.create(null));
  39. if (typeof extensions.tracing !== 'undefined') {
  40. throw new Error(PACKAGE_NAME + ": Could not add `tracing` to " +
  41. "`extensions` since `tracing` was unexpectedly already present.");
  42. }
  43. extensions.tracing = {
  44. version: 1,
  45. startTime: startWallTime.toISOString(),
  46. endTime: endWallTime.toISOString(),
  47. duration: durationHrTimeToNanos(duration),
  48. execution: {
  49. resolvers: resolverCalls.map(resolverCall => {
  50. const startOffset = durationHrTimeToNanos(resolverCall.startOffset);
  51. const duration = resolverCall.endOffset
  52. ? durationHrTimeToNanos(resolverCall.endOffset) - startOffset
  53. : 0;
  54. return {
  55. path: [...graphql_1.responsePathAsArray(resolverCall.path)],
  56. parentType: resolverCall.parentType.toString(),
  57. fieldName: resolverCall.fieldName,
  58. returnType: resolverCall.returnType.toString(),
  59. startOffset,
  60. duration,
  61. };
  62. }),
  63. },
  64. };
  65. },
  66. };
  67. },
  68. });
  69. function durationHrTimeToNanos(hrtime) {
  70. return hrtime[0] * 1e9 + hrtime[1];
  71. }
  72. //# sourceMappingURL=index.js.map