1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.endTracingContext = exports.startTracingContext = exports.apolloTracingGraphQLMiddleware = exports.apolloTracingContext = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const traceHRStartTime = Symbol("HR Start Time");
|
6 | const traceWallStartTime = Symbol("Wall Start Time");
|
7 | exports.apolloTracingContext = Symbol("Apollo Tracing Context");
|
8 | function durationHrTimeToNanos(hrtime) {
|
9 | return hrtime[0] * 1e9 + hrtime[1];
|
10 | }
|
11 | async function apolloTracingGraphQLMiddleware(resolve, parent, args, context, info) {
|
12 | const startOffset = durationHrTimeToNanos(process.hrtime(context[exports.apolloTracingContext][traceHRStartTime]));
|
13 | try {
|
14 | return await resolve(parent, args, context, info);
|
15 | }
|
16 | finally {
|
17 | context[exports.apolloTracingContext].execution.resolvers.push({
|
18 | path: [...graphql_1.responsePathAsArray(info.path)],
|
19 | parentType: info.parentType.toString(),
|
20 | fieldName: info.fieldName,
|
21 | returnType: info.returnType.toString(),
|
22 | startOffset,
|
23 | duration: durationHrTimeToNanos(process.hrtime(context[exports.apolloTracingContext][traceHRStartTime])) - startOffset
|
24 | });
|
25 | }
|
26 | }
|
27 | exports.apolloTracingGraphQLMiddleware = apolloTracingGraphQLMiddleware;
|
28 | function startTracingContext() {
|
29 | const wallStartTime = new Date();
|
30 | return {
|
31 | [traceHRStartTime]: process.hrtime(),
|
32 | [traceWallStartTime]: wallStartTime,
|
33 | version: 1,
|
34 | startTime: wallStartTime.toISOString(),
|
35 | endTime: "",
|
36 | duration: 0,
|
37 | execution: {
|
38 | resolvers: []
|
39 | }
|
40 | };
|
41 | }
|
42 | exports.startTracingContext = startTracingContext;
|
43 | function endTracingContext(context) {
|
44 | context.endTime = new Date().toISOString();
|
45 | context.duration = durationHrTimeToNanos(process.hrtime(context[traceHRStartTime]));
|
46 | }
|
47 | exports.endTracingContext = endTracingContext;
|
48 |
|
\ | No newline at end of file |