UNPKG

2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.endTracingContext = exports.startTracingContext = exports.apolloTracingGraphQLMiddleware = exports.apolloTracingContext = void 0;
4const graphql_1 = require("graphql");
5const traceHRStartTime = Symbol("HR Start Time");
6const traceWallStartTime = Symbol("Wall Start Time");
7exports.apolloTracingContext = Symbol("Apollo Tracing Context");
8function durationHrTimeToNanos(hrtime) {
9 return hrtime[0] * 1e9 + hrtime[1];
10}
11async 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}
27exports.apolloTracingGraphQLMiddleware = apolloTracingGraphQLMiddleware;
28function 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}
42exports.startTracingContext = startTracingContext;
43function endTracingContext(context) {
44 context.endTime = new Date().toISOString();
45 context.duration = durationHrTimeToNanos(process.hrtime(context[traceHRStartTime]));
46}
47exports.endTracingContext = endTracingContext;
48//# sourceMappingURL=apolloTracing.js.map
\No newline at end of file