@envelop/core 2.4.0-alpha-c0cc559.0 → 2.4.0-alpha-e9434aa.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/create.js +28 -0
- package/cjs/enable-if.js +24 -0
- package/cjs/index.js +16 -0
- package/cjs/orchestrator.js +451 -0
- package/cjs/package.json +1 -0
- package/cjs/plugins/use-envelop.js +13 -0
- package/cjs/plugins/use-error-handler.js +21 -0
- package/cjs/plugins/use-extend-context.js +9 -0
- package/cjs/plugins/use-immediate-introspection.js +50 -0
- package/cjs/plugins/use-logger.js +45 -0
- package/cjs/plugins/use-masked-errors.js +90 -0
- package/cjs/plugins/use-payload-formatter.js +21 -0
- package/cjs/plugins/use-schema.js +29 -0
- package/cjs/plugins/use-timing.js +138 -0
- package/cjs/traced-orchestrator.js +134 -0
- package/cjs/traced-schema.js +68 -0
- package/cjs/utils.js +221 -0
- package/esm/create.js +28 -0
- package/esm/enable-if.js +24 -0
- package/esm/index.js +29 -0
- package/esm/orchestrator.js +451 -0
- package/esm/plugins/use-envelop.js +13 -0
- package/esm/plugins/use-error-handler.js +21 -0
- package/esm/plugins/use-extend-context.js +9 -0
- package/esm/plugins/use-immediate-introspection.js +50 -0
- package/esm/plugins/use-logger.js +45 -0
- package/esm/plugins/use-masked-errors.js +90 -0
- package/esm/plugins/use-payload-formatter.js +21 -0
- package/esm/plugins/use-schema.js +29 -0
- package/esm/plugins/use-timing.js +138 -0
- package/esm/traced-orchestrator.js +134 -0
- package/esm/traced-schema.js +68 -0
- package/esm/utils.js +221 -0
- package/package.json +33 -12
- package/{create.d.ts → typings/create.d.ts} +1 -1
- package/{enable-if.d.ts → typings/enable-if.d.ts} +0 -0
- package/typings/index.d.ts +13 -0
- package/{orchestrator.d.ts → typings/orchestrator.d.ts} +0 -0
- package/{plugins → typings/plugins}/use-envelop.d.ts +0 -0
- package/{plugins → typings/plugins}/use-error-handler.d.ts +0 -0
- package/{plugins → typings/plugins}/use-extend-context.d.ts +0 -0
- package/{plugins → typings/plugins}/use-immediate-introspection.d.ts +0 -0
- package/{plugins → typings/plugins}/use-logger.d.ts +1 -1
- package/{plugins → typings/plugins}/use-masked-errors.d.ts +3 -3
- package/{plugins → typings/plugins}/use-payload-formatter.d.ts +0 -0
- package/{plugins → typings/plugins}/use-schema.d.ts +0 -0
- package/{plugins → typings/plugins}/use-timing.d.ts +2 -2
- package/{traced-orchestrator.d.ts → typings/traced-orchestrator.d.ts} +1 -1
- package/{traced-schema.d.ts → typings/traced-schema.d.ts} +0 -0
- package/{utils.d.ts → typings/utils.d.ts} +0 -13
- package/README.md +0 -15
- package/index.d.ts +0 -13
- package/index.js +0 -1359
- package/index.mjs +0 -1317
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useMaskedErrors = exports.formatError = exports.EnvelopError = exports.DEFAULT_ERROR_MESSAGE = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const utils_js_1 = require("../utils.js");
|
|
6
|
+
exports.DEFAULT_ERROR_MESSAGE = 'Unexpected error.';
|
|
7
|
+
class EnvelopError extends graphql_1.GraphQLError {
|
|
8
|
+
constructor(message, extensions) {
|
|
9
|
+
super(message, undefined, undefined, undefined, undefined, undefined, extensions);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.EnvelopError = EnvelopError;
|
|
13
|
+
const formatError = (err, message, isDev) => {
|
|
14
|
+
var _a, _b, _c, _d;
|
|
15
|
+
if (err instanceof graphql_1.GraphQLError) {
|
|
16
|
+
if (
|
|
17
|
+
/** execution error */
|
|
18
|
+
(err.originalError && err.originalError instanceof EnvelopError === false) ||
|
|
19
|
+
/** validate and parse errors */
|
|
20
|
+
(err.originalError === undefined && err instanceof EnvelopError === false)) {
|
|
21
|
+
return new graphql_1.GraphQLError(message, err.nodes, err.source, err.positions, err.path, undefined, isDev
|
|
22
|
+
? {
|
|
23
|
+
originalError: {
|
|
24
|
+
message: (_b = (_a = err.originalError) === null || _a === void 0 ? void 0 : _a.message) !== null && _b !== void 0 ? _b : err.message,
|
|
25
|
+
stack: (_d = (_c = err.originalError) === null || _c === void 0 ? void 0 : _c.stack) !== null && _d !== void 0 ? _d : err.stack,
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
: undefined);
|
|
29
|
+
}
|
|
30
|
+
return err;
|
|
31
|
+
}
|
|
32
|
+
return new graphql_1.GraphQLError(message);
|
|
33
|
+
};
|
|
34
|
+
exports.formatError = formatError;
|
|
35
|
+
const makeHandleResult = (format, message, isDev) => ({ result, setResult }) => {
|
|
36
|
+
if (result.errors != null) {
|
|
37
|
+
setResult({ ...result, errors: result.errors.map(error => format(error, message, isDev)) });
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const useMaskedErrors = (opts) => {
|
|
41
|
+
var _a, _b;
|
|
42
|
+
const format = (_a = opts === null || opts === void 0 ? void 0 : opts.formatError) !== null && _a !== void 0 ? _a : exports.formatError;
|
|
43
|
+
const message = (opts === null || opts === void 0 ? void 0 : opts.errorMessage) || exports.DEFAULT_ERROR_MESSAGE;
|
|
44
|
+
// eslint-disable-next-line dot-notation
|
|
45
|
+
const isDev = (_b = opts === null || opts === void 0 ? void 0 : opts.isDev) !== null && _b !== void 0 ? _b : (typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false);
|
|
46
|
+
const handleResult = makeHandleResult(format, message, isDev);
|
|
47
|
+
return {
|
|
48
|
+
onParse: (opts === null || opts === void 0 ? void 0 : opts.handleParseErrors) === true
|
|
49
|
+
? function onParse() {
|
|
50
|
+
return function onParseEnd({ result, replaceParseResult }) {
|
|
51
|
+
if (result instanceof Error) {
|
|
52
|
+
replaceParseResult(format(result, message, isDev));
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
: undefined,
|
|
57
|
+
onValidate: (opts === null || opts === void 0 ? void 0 : opts.handleValidationErrors) === true
|
|
58
|
+
? function onValidate() {
|
|
59
|
+
return function onValidateEnd({ valid, result, setResult }) {
|
|
60
|
+
if (valid === false) {
|
|
61
|
+
setResult(result.map(error => format(error, message, isDev)));
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
: undefined,
|
|
66
|
+
onPluginInit(context) {
|
|
67
|
+
context.registerContextErrorHandler(({ error, setError }) => {
|
|
68
|
+
setError((0, exports.formatError)(error, message, isDev));
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
onExecute() {
|
|
72
|
+
return {
|
|
73
|
+
onExecuteDone(payload) {
|
|
74
|
+
return (0, utils_js_1.handleStreamOrSingleExecutionResult)(payload, handleResult);
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
onSubscribe() {
|
|
79
|
+
return {
|
|
80
|
+
onSubscribeResult(payload) {
|
|
81
|
+
return (0, utils_js_1.handleStreamOrSingleExecutionResult)(payload, handleResult);
|
|
82
|
+
},
|
|
83
|
+
onSubscribeError({ error, setError }) {
|
|
84
|
+
setError((0, exports.formatError)(error, message, isDev));
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
exports.useMaskedErrors = useMaskedErrors;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePayloadFormatter = void 0;
|
|
4
|
+
const utils_js_1 = require("../utils.js");
|
|
5
|
+
const makeHandleResult = (formatter) => ({ args, result, setResult, }) => {
|
|
6
|
+
const modified = formatter(result, args);
|
|
7
|
+
if (modified !== false) {
|
|
8
|
+
setResult(modified);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const usePayloadFormatter = (formatter) => ({
|
|
12
|
+
onExecute() {
|
|
13
|
+
const handleResult = makeHandleResult(formatter);
|
|
14
|
+
return {
|
|
15
|
+
onExecuteDone(payload) {
|
|
16
|
+
return (0, utils_js_1.handleStreamOrSingleExecutionResult)(payload, handleResult);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
exports.usePayloadFormatter = usePayloadFormatter;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useAsyncSchema = exports.useLazyLoadedSchema = exports.useSchema = void 0;
|
|
4
|
+
const useSchema = (schema) => {
|
|
5
|
+
return {
|
|
6
|
+
onPluginInit({ setSchema }) {
|
|
7
|
+
setSchema(schema);
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
exports.useSchema = useSchema;
|
|
12
|
+
const useLazyLoadedSchema = (schemaLoader) => {
|
|
13
|
+
return {
|
|
14
|
+
onEnveloped({ setSchema, context }) {
|
|
15
|
+
setSchema(schemaLoader(context));
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
exports.useLazyLoadedSchema = useLazyLoadedSchema;
|
|
20
|
+
const useAsyncSchema = (schemaPromise) => {
|
|
21
|
+
return {
|
|
22
|
+
onPluginInit({ setSchema }) {
|
|
23
|
+
schemaPromise.then(schemaObj => {
|
|
24
|
+
setSchema(schemaObj);
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
exports.useAsyncSchema = useAsyncSchema;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useTiming = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const utils_js_1 = require("../utils.js");
|
|
6
|
+
const HR_TO_NS = 1e9;
|
|
7
|
+
const NS_TO_MS = 1e6;
|
|
8
|
+
const DEFAULT_OPTIONS = {
|
|
9
|
+
onExecutionMeasurement: (args, timing) => console.log(`Operation execution "${args.operationName}" done in ${timing.ms}ms`),
|
|
10
|
+
onSubscriptionMeasurement: (args, timing) => console.log(`Operation subscription "${args.operationName}" done in ${timing.ms}ms`),
|
|
11
|
+
onParsingMeasurement: (source, timing) => console.log(`Parsing "${source}" done in ${timing.ms}ms`),
|
|
12
|
+
onValidationMeasurement: (document, timing) => { var _a, _b; return console.log(`Validation "${((_b = (_a = (0, graphql_1.getOperationAST)(document)) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.value) || '-'}" done in ${timing.ms}ms`); },
|
|
13
|
+
onResolverMeasurement: (info, timing) => console.log(`\tResolver of "${info.parentType.toString()}.${info.fieldName}" done in ${timing.ms}ms`),
|
|
14
|
+
onContextBuildingMeasurement: (timing) => console.log(`Context building done in ${timing.ms}ms`),
|
|
15
|
+
};
|
|
16
|
+
const deltaFrom = (hrtime) => {
|
|
17
|
+
const delta = process.hrtime(hrtime);
|
|
18
|
+
const ns = delta[0] * HR_TO_NS + delta[1];
|
|
19
|
+
return {
|
|
20
|
+
ns,
|
|
21
|
+
get ms() {
|
|
22
|
+
return ns / NS_TO_MS;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
const useTiming = (rawOptions) => {
|
|
27
|
+
const options = {
|
|
28
|
+
...DEFAULT_OPTIONS,
|
|
29
|
+
...(rawOptions || {}),
|
|
30
|
+
};
|
|
31
|
+
const result = {};
|
|
32
|
+
if (options.onContextBuildingMeasurement) {
|
|
33
|
+
result.onContextBuilding = ({ context }) => {
|
|
34
|
+
if (context[utils_js_1.envelopIsIntrospectionSymbol]) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const contextStartTime = process.hrtime();
|
|
38
|
+
return () => {
|
|
39
|
+
options.onContextBuildingMeasurement(deltaFrom(contextStartTime));
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (options.onParsingMeasurement) {
|
|
44
|
+
result.onParse = ({ params, extendContext }) => {
|
|
45
|
+
if (options.skipIntrospection && (0, utils_js_1.isIntrospectionOperationString)(params.source)) {
|
|
46
|
+
extendContext({
|
|
47
|
+
[utils_js_1.envelopIsIntrospectionSymbol]: true,
|
|
48
|
+
});
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const parseStartTime = process.hrtime();
|
|
52
|
+
return () => {
|
|
53
|
+
options.onParsingMeasurement(params.source, deltaFrom(parseStartTime));
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (options.onValidationMeasurement) {
|
|
58
|
+
result.onValidate = ({ params, context }) => {
|
|
59
|
+
if (context[utils_js_1.envelopIsIntrospectionSymbol]) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const validateStartTime = process.hrtime();
|
|
63
|
+
return () => {
|
|
64
|
+
options.onValidationMeasurement(params.documentAST, deltaFrom(validateStartTime));
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (options.onExecutionMeasurement) {
|
|
69
|
+
if (options.onResolverMeasurement) {
|
|
70
|
+
result.onExecute = ({ args }) => {
|
|
71
|
+
if (args.contextValue[utils_js_1.envelopIsIntrospectionSymbol]) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const executeStartTime = process.hrtime();
|
|
75
|
+
return {
|
|
76
|
+
onExecuteDone: () => {
|
|
77
|
+
options.onExecutionMeasurement(args, deltaFrom(executeStartTime));
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
result.onResolverCalled = ({ info }) => {
|
|
82
|
+
const resolverStartTime = process.hrtime();
|
|
83
|
+
return () => {
|
|
84
|
+
options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
result.onExecute = ({ args }) => {
|
|
90
|
+
if (args.contextValue[utils_js_1.envelopIsIntrospectionSymbol]) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const executeStartTime = process.hrtime();
|
|
94
|
+
return {
|
|
95
|
+
onExecuteDone: () => {
|
|
96
|
+
options.onExecutionMeasurement(args, deltaFrom(executeStartTime));
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (options.onSubscriptionMeasurement) {
|
|
103
|
+
if (options.onResolverMeasurement) {
|
|
104
|
+
result.onSubscribe = ({ args }) => {
|
|
105
|
+
if (args.contextValue[utils_js_1.envelopIsIntrospectionSymbol]) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const subscribeStartTime = process.hrtime();
|
|
109
|
+
return {
|
|
110
|
+
onSubscribeResult: () => {
|
|
111
|
+
options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime));
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
result.onResolverCalled = ({ info }) => {
|
|
116
|
+
const resolverStartTime = process.hrtime();
|
|
117
|
+
return () => {
|
|
118
|
+
options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
result.onSubscribe = ({ args }) => {
|
|
124
|
+
if (args.contextValue[utils_js_1.envelopIsIntrospectionSymbol]) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const subscribeStartTime = process.hrtime();
|
|
128
|
+
return {
|
|
129
|
+
onSubscribeResult: () => {
|
|
130
|
+
options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime));
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
};
|
|
138
|
+
exports.useTiming = useTiming;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.traceOrchestrator = void 0;
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
const getTimestamp = typeof globalThis !== 'undefined' && ((_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.performance) === null || _a === void 0 ? void 0 : _a.now) ? () => globalThis.performance.now() : () => Date.now();
|
|
8
|
+
const measure = () => {
|
|
9
|
+
const start = getTimestamp();
|
|
10
|
+
return () => {
|
|
11
|
+
const end = getTimestamp();
|
|
12
|
+
return end - start;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
const tracingSymbol = Symbol('envelopTracing');
|
|
16
|
+
function traceOrchestrator(orchestrator) {
|
|
17
|
+
const createTracer = (name, ctx) => {
|
|
18
|
+
const end = measure();
|
|
19
|
+
return () => {
|
|
20
|
+
ctx[tracingSymbol][name] = end();
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
...orchestrator,
|
|
25
|
+
init: (ctx = {}) => {
|
|
26
|
+
ctx[tracingSymbol] = ctx[tracingSymbol] || {};
|
|
27
|
+
const done = createTracer('init', ctx || {});
|
|
28
|
+
try {
|
|
29
|
+
return orchestrator.init(ctx);
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
done();
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
parse: (ctx = {}) => {
|
|
36
|
+
ctx[tracingSymbol] = ctx[tracingSymbol] || {};
|
|
37
|
+
const actualFn = orchestrator.parse(ctx);
|
|
38
|
+
return (...args) => {
|
|
39
|
+
const done = createTracer('parse', ctx);
|
|
40
|
+
try {
|
|
41
|
+
return actualFn(...args);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
done();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
validate: (ctx = {}) => {
|
|
49
|
+
ctx[tracingSymbol] = ctx[tracingSymbol] || {};
|
|
50
|
+
const actualFn = orchestrator.validate(ctx);
|
|
51
|
+
return (...args) => {
|
|
52
|
+
const done = createTracer('validate', ctx);
|
|
53
|
+
try {
|
|
54
|
+
return actualFn(...args);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
done();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
execute: async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) => {
|
|
62
|
+
const args = argsOrSchema instanceof graphql_1.GraphQLSchema
|
|
63
|
+
? {
|
|
64
|
+
schema: argsOrSchema,
|
|
65
|
+
document: document,
|
|
66
|
+
rootValue,
|
|
67
|
+
contextValue,
|
|
68
|
+
variableValues,
|
|
69
|
+
operationName,
|
|
70
|
+
fieldResolver,
|
|
71
|
+
typeResolver,
|
|
72
|
+
}
|
|
73
|
+
: argsOrSchema;
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
75
|
+
// @ts-ignore GraphQL.js types contextValue as unknown
|
|
76
|
+
const done = createTracer('execute', args.contextValue || {});
|
|
77
|
+
try {
|
|
78
|
+
const result = await orchestrator.execute(args);
|
|
79
|
+
done();
|
|
80
|
+
if (!(0, utils_js_1.isAsyncIterable)(result)) {
|
|
81
|
+
result.extensions = result.extensions || {};
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
83
|
+
// @ts-ignore GraphQL.js types contextValue as unknown
|
|
84
|
+
result.extensions.envelopTracing = args.contextValue[tracingSymbol];
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
// eslint-disable-next-line no-console
|
|
88
|
+
console.warn(`"traceOrchestrator" encountered a AsyncIterator which is not supported yet, so tracing data is not available for the operation.`);
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
done();
|
|
94
|
+
throw e;
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
subscribe: async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) => {
|
|
98
|
+
const args = argsOrSchema instanceof graphql_1.GraphQLSchema
|
|
99
|
+
? {
|
|
100
|
+
schema: argsOrSchema,
|
|
101
|
+
document: document,
|
|
102
|
+
rootValue,
|
|
103
|
+
contextValue,
|
|
104
|
+
variableValues,
|
|
105
|
+
operationName,
|
|
106
|
+
fieldResolver,
|
|
107
|
+
subscribeFieldResolver,
|
|
108
|
+
}
|
|
109
|
+
: argsOrSchema;
|
|
110
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
111
|
+
// @ts-ignore GraphQL.js types contextValue as unknown
|
|
112
|
+
const done = createTracer('subscribe', args.contextValue || {});
|
|
113
|
+
try {
|
|
114
|
+
return await orchestrator.subscribe(args);
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
done();
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
contextFactory: (ctx = {}) => {
|
|
121
|
+
const actualFn = orchestrator.contextFactory(ctx);
|
|
122
|
+
return async (childCtx) => {
|
|
123
|
+
const done = createTracer('contextFactory', ctx);
|
|
124
|
+
try {
|
|
125
|
+
return await actualFn(childCtx);
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
done();
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
exports.traceOrchestrator = traceOrchestrator;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prepareTracedSchema = exports.resolversHooksSymbol = exports.trackedSchemaSymbol = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
exports.trackedSchemaSymbol = Symbol('TRACKED_SCHEMA');
|
|
6
|
+
exports.resolversHooksSymbol = Symbol('RESOLVERS_HOOKS');
|
|
7
|
+
function prepareTracedSchema(schema) {
|
|
8
|
+
if (!schema || schema[exports.trackedSchemaSymbol]) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
schema[exports.trackedSchemaSymbol] = true;
|
|
12
|
+
const entries = Object.values(schema.getTypeMap());
|
|
13
|
+
for (const type of entries) {
|
|
14
|
+
if (!(0, graphql_1.isIntrospectionType)(type) && (0, graphql_1.isObjectType)(type)) {
|
|
15
|
+
const fields = Object.values(type.getFields());
|
|
16
|
+
for (const field of fields) {
|
|
17
|
+
let resolverFn = (field.resolve || graphql_1.defaultFieldResolver);
|
|
18
|
+
field.resolve = async (root, args, context, info) => {
|
|
19
|
+
if (context && context[exports.resolversHooksSymbol]) {
|
|
20
|
+
const hooks = context[exports.resolversHooksSymbol];
|
|
21
|
+
const afterCalls = [];
|
|
22
|
+
for (const hook of hooks) {
|
|
23
|
+
const afterFn = await hook({
|
|
24
|
+
root,
|
|
25
|
+
args,
|
|
26
|
+
context,
|
|
27
|
+
info,
|
|
28
|
+
resolverFn,
|
|
29
|
+
replaceResolverFn: newFn => {
|
|
30
|
+
resolverFn = newFn;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
afterFn && afterCalls.push(afterFn);
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
let result = await resolverFn(root, args, context, info);
|
|
37
|
+
for (const afterFn of afterCalls) {
|
|
38
|
+
afterFn({
|
|
39
|
+
result,
|
|
40
|
+
setResult: newResult => {
|
|
41
|
+
result = newResult;
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
let resultErr = e;
|
|
49
|
+
for (const afterFn of afterCalls) {
|
|
50
|
+
afterFn({
|
|
51
|
+
result: resultErr,
|
|
52
|
+
setResult: newResult => {
|
|
53
|
+
resultErr = newResult;
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
throw resultErr;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return resolverFn(root, args, context, info);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.prepareTracedSchema = prepareTracedSchema;
|