-
Notifications
You must be signed in to change notification settings - Fork 16
/
traceResolvers.js
43 lines (36 loc) · 1.08 KB
/
traceResolvers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { applyMiddlewareToDeclaredResolvers } = require('graphql-middleware');
const AWSXRay = require('aws-xray-sdk-core');
const isPromise = require('is-promise');
function fieldPathFromInfo (info) {
let path = info.path;
const segments = [];
while (path) {
segments.unshift(path.key);
path = path.prev;
}
return segments.join('.');
}
const tracer = function (resolver, parent, args, ctx, info) {
let result;
const fieldPath = fieldPathFromInfo(info);
AWSXRay.captureAsyncFunc(`GraphQL ${fieldPath}`, function (subsegment) {
result = resolver();
// When AWS_XRAY_CONTEXT_MISSING is set to LOG_MISSING and no context was
// found, then the subsegment will be null and nothing should be done
if (subsegment) {
if (isPromise(result)) {
result.then(function () {
subsegment.close();
}).catch(function (error) {
subsegment.close(error);
});
} else {
subsegment.close();
}
}
});
return result;
};
module.exports = function (schema) {
applyMiddlewareToDeclaredResolvers(schema, tracer);
};