-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathexpress-graphql.js
66 lines (58 loc) · 1.95 KB
/
express-graphql.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
var semver = require('semver');
const shimmer = require('../shimmer');
module.exports = function (expressGraphql, agent, { version, enabled }) {
if (!enabled) {
return expressGraphql;
}
if (semver.satisfies(version, '>=0.10.0 <0.13.0')) {
// https://github.com/graphql/express-graphql/pull/626 changed `graphqlHTTP`
// to no longer be the top-level export:
// {
// graphqlHTTP: [Function: graphqlHTTP],
// getGraphQLParams: [AsyncFunction: getGraphQLParams]
// }
shimmer.wrap(expressGraphql, 'graphqlHTTP', wrapGraphqlHTTP);
return expressGraphql;
} else if (
semver.satisfies(version, '>=0.6.1 <0.10.0') &&
typeof expressGraphql === 'function'
) {
// Up to and including 0.9.x, `require('express-graphql')` is:
// [Function: graphqlHTTP] {
// getGraphQLParams: [AsyncFunction: getGraphQLParams]
// }
const wrappedGraphqlHTTP = wrapGraphqlHTTP(expressGraphql);
for (const key of Object.keys(expressGraphql)) {
wrappedGraphqlHTTP[key] = expressGraphql[key];
}
return wrappedGraphqlHTTP;
} else {
agent.logger.debug(
'express-graphql@%s not supported: skipping instrumentation',
version,
);
return expressGraphql;
}
function wrapGraphqlHTTP(origGraphqlHTTP) {
return function wrappedGraphqlHTTP() {
var orig = origGraphqlHTTP.apply(this, arguments);
if (typeof orig !== 'function') {
return orig;
}
// Express is very particular with the number of arguments!
return function (req, res) {
var trans = agent._instrumentation.currTransaction();
if (trans) {
trans._graphqlRoute = true;
}
return orig.apply(this, arguments);
};
};
}
};