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