UNPKG

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