@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,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;
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorAsyncIterator = exports.finalAsyncIterator = exports.handleStreamOrSingleExecutionResult = exports.isAsyncIterable = exports.makeExecute = exports.mapAsyncIterator = exports.makeSubscribe = exports.isIntrospectionOperationString = exports.isIntrospectionDocument = exports.isIntrospectionOperation = exports.isOperationDefinition = exports.envelopIsIntrospectionSymbol = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
exports.envelopIsIntrospectionSymbol = Symbol('ENVELOP_IS_INTROSPECTION');
|
|
6
|
+
function isOperationDefinition(def) {
|
|
7
|
+
return def.kind === graphql_1.Kind.OPERATION_DEFINITION;
|
|
8
|
+
}
|
|
9
|
+
exports.isOperationDefinition = isOperationDefinition;
|
|
10
|
+
function isIntrospectionOperation(operation) {
|
|
11
|
+
if (operation.kind === 'OperationDefinition') {
|
|
12
|
+
let hasIntrospectionField = false;
|
|
13
|
+
(0, graphql_1.visit)(operation, {
|
|
14
|
+
Field: node => {
|
|
15
|
+
if (node.name.value === '__schema') {
|
|
16
|
+
hasIntrospectionField = true;
|
|
17
|
+
return graphql_1.BREAK;
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
return hasIntrospectionField;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
exports.isIntrospectionOperation = isIntrospectionOperation;
|
|
26
|
+
function isIntrospectionDocument(document) {
|
|
27
|
+
const operations = document.definitions.filter(isOperationDefinition);
|
|
28
|
+
return operations.some(op => isIntrospectionOperation(op));
|
|
29
|
+
}
|
|
30
|
+
exports.isIntrospectionDocument = isIntrospectionDocument;
|
|
31
|
+
function isIntrospectionOperationString(operation) {
|
|
32
|
+
return (typeof operation === 'string' ? operation : operation.body).indexOf('__schema') !== -1;
|
|
33
|
+
}
|
|
34
|
+
exports.isIntrospectionOperationString = isIntrospectionOperationString;
|
|
35
|
+
function getSubscribeArgs(args) {
|
|
36
|
+
return args.length === 1
|
|
37
|
+
? args[0]
|
|
38
|
+
: {
|
|
39
|
+
schema: args[0],
|
|
40
|
+
document: args[1],
|
|
41
|
+
rootValue: args[2],
|
|
42
|
+
contextValue: args[3],
|
|
43
|
+
variableValues: args[4],
|
|
44
|
+
operationName: args[5],
|
|
45
|
+
fieldResolver: args[6],
|
|
46
|
+
subscribeFieldResolver: args[7],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Utility function for making a subscribe function that handles polymorphic arguments.
|
|
51
|
+
*/
|
|
52
|
+
const makeSubscribe = (subscribeFn) => ((...polyArgs) => subscribeFn(getSubscribeArgs(polyArgs)));
|
|
53
|
+
exports.makeSubscribe = makeSubscribe;
|
|
54
|
+
function mapAsyncIterator(source, mapper) {
|
|
55
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
56
|
+
async function mapResult(result) {
|
|
57
|
+
var _a;
|
|
58
|
+
if (result.done) {
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
return { value: await mapper(result.value), done: false };
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
try {
|
|
66
|
+
await ((_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator));
|
|
67
|
+
}
|
|
68
|
+
catch (_error) {
|
|
69
|
+
/* ignore error */
|
|
70
|
+
}
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const stream = {
|
|
75
|
+
[Symbol.asyncIterator]() {
|
|
76
|
+
return stream;
|
|
77
|
+
},
|
|
78
|
+
async next() {
|
|
79
|
+
return await mapResult(await iterator.next());
|
|
80
|
+
},
|
|
81
|
+
async return() {
|
|
82
|
+
var _a;
|
|
83
|
+
const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
|
|
84
|
+
return promise ? await mapResult(await promise) : { value: undefined, done: true };
|
|
85
|
+
},
|
|
86
|
+
async throw(error) {
|
|
87
|
+
var _a;
|
|
88
|
+
const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
|
|
89
|
+
if (promise) {
|
|
90
|
+
return await mapResult(await promise);
|
|
91
|
+
}
|
|
92
|
+
// if the source has no throw method we just re-throw error
|
|
93
|
+
// usually throw is not called anyways
|
|
94
|
+
throw error;
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
return stream;
|
|
98
|
+
}
|
|
99
|
+
exports.mapAsyncIterator = mapAsyncIterator;
|
|
100
|
+
function getExecuteArgs(args) {
|
|
101
|
+
return args.length === 1
|
|
102
|
+
? args[0]
|
|
103
|
+
: {
|
|
104
|
+
schema: args[0],
|
|
105
|
+
document: args[1],
|
|
106
|
+
rootValue: args[2],
|
|
107
|
+
contextValue: args[3],
|
|
108
|
+
variableValues: args[4],
|
|
109
|
+
operationName: args[5],
|
|
110
|
+
fieldResolver: args[6],
|
|
111
|
+
typeResolver: args[7],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Utility function for making a execute function that handles polymorphic arguments.
|
|
116
|
+
*/
|
|
117
|
+
const makeExecute = (executeFn) => ((...polyArgs) => executeFn(getExecuteArgs(polyArgs)));
|
|
118
|
+
exports.makeExecute = makeExecute;
|
|
119
|
+
/**
|
|
120
|
+
* Returns true if the provided object implements the AsyncIterator protocol via
|
|
121
|
+
* implementing a `Symbol.asyncIterator` method.
|
|
122
|
+
*
|
|
123
|
+
* Source: https://github.com/graphql/graphql-js/blob/main/src/jsutils/isAsyncIterable.ts
|
|
124
|
+
*/
|
|
125
|
+
function isAsyncIterable(maybeAsyncIterable) {
|
|
126
|
+
return (typeof maybeAsyncIterable === 'object' &&
|
|
127
|
+
maybeAsyncIterable != null &&
|
|
128
|
+
typeof maybeAsyncIterable[Symbol.asyncIterator] === 'function');
|
|
129
|
+
}
|
|
130
|
+
exports.isAsyncIterable = isAsyncIterable;
|
|
131
|
+
/**
|
|
132
|
+
* A utility function for handling `onExecuteDone` hook result, for simplifying the handling of AsyncIterable returned from `execute`.
|
|
133
|
+
*
|
|
134
|
+
* @param payload The payload send to `onExecuteDone` hook function
|
|
135
|
+
* @param fn The handler to be executed on each result
|
|
136
|
+
* @returns a subscription for streamed results, or undefined in case of an non-async
|
|
137
|
+
*/
|
|
138
|
+
function handleStreamOrSingleExecutionResult(payload, fn) {
|
|
139
|
+
if (isAsyncIterable(payload.result)) {
|
|
140
|
+
return { onNext: fn };
|
|
141
|
+
}
|
|
142
|
+
fn({
|
|
143
|
+
args: payload.args,
|
|
144
|
+
result: payload.result,
|
|
145
|
+
setResult: payload.setResult,
|
|
146
|
+
});
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
exports.handleStreamOrSingleExecutionResult = handleStreamOrSingleExecutionResult;
|
|
150
|
+
function finalAsyncIterator(source, onFinal) {
|
|
151
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
152
|
+
let isDone = false;
|
|
153
|
+
const stream = {
|
|
154
|
+
[Symbol.asyncIterator]() {
|
|
155
|
+
return stream;
|
|
156
|
+
},
|
|
157
|
+
async next() {
|
|
158
|
+
const result = await iterator.next();
|
|
159
|
+
if (result.done && isDone === false) {
|
|
160
|
+
isDone = true;
|
|
161
|
+
onFinal();
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
},
|
|
165
|
+
async return() {
|
|
166
|
+
var _a;
|
|
167
|
+
const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
|
|
168
|
+
if (isDone === false) {
|
|
169
|
+
isDone = true;
|
|
170
|
+
onFinal();
|
|
171
|
+
}
|
|
172
|
+
return promise ? await promise : { done: true, value: undefined };
|
|
173
|
+
},
|
|
174
|
+
async throw(error) {
|
|
175
|
+
var _a;
|
|
176
|
+
const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
|
|
177
|
+
if (promise) {
|
|
178
|
+
return await promise;
|
|
179
|
+
}
|
|
180
|
+
// if the source has no throw method we just re-throw error
|
|
181
|
+
// usually throw is not called anyways
|
|
182
|
+
throw error;
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
return stream;
|
|
186
|
+
}
|
|
187
|
+
exports.finalAsyncIterator = finalAsyncIterator;
|
|
188
|
+
function errorAsyncIterator(source, onError) {
|
|
189
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
190
|
+
const stream = {
|
|
191
|
+
[Symbol.asyncIterator]() {
|
|
192
|
+
return stream;
|
|
193
|
+
},
|
|
194
|
+
async next() {
|
|
195
|
+
try {
|
|
196
|
+
return await iterator.next();
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
onError(error);
|
|
200
|
+
return { done: true, value: undefined };
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
async return() {
|
|
204
|
+
var _a;
|
|
205
|
+
const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
|
|
206
|
+
return promise ? await promise : { done: true, value: undefined };
|
|
207
|
+
},
|
|
208
|
+
async throw(error) {
|
|
209
|
+
var _a;
|
|
210
|
+
const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
|
|
211
|
+
if (promise) {
|
|
212
|
+
return await promise;
|
|
213
|
+
}
|
|
214
|
+
// if the source has no throw method we just re-throw error
|
|
215
|
+
// usually throw is not called anyways
|
|
216
|
+
throw error;
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
return stream;
|
|
220
|
+
}
|
|
221
|
+
exports.errorAsyncIterator = errorAsyncIterator;
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/core",
|
|
3
|
-
"version": "2.4.0-alpha-
|
|
3
|
+
"version": "2.4.0-alpha-e9434aa.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@envelop/types": "2.
|
|
9
|
+
"@envelop/types": "2.3.0-alpha-e9434aa.0"
|
|
10
10
|
},
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
13
|
-
"url": "https://github.com/
|
|
13
|
+
"url": "https://github.com/n1ru4l/envelop.git",
|
|
14
14
|
"directory": "packages/core"
|
|
15
15
|
},
|
|
16
16
|
"keywords": [
|
|
@@ -24,21 +24,42 @@
|
|
|
24
24
|
],
|
|
25
25
|
"author": "Dotan Simha <dotansimha@gmail.com>",
|
|
26
26
|
"license": "MIT",
|
|
27
|
-
"main": "index.js",
|
|
28
|
-
"module": "index.
|
|
29
|
-
"typings": "index.d.ts",
|
|
27
|
+
"main": "cjs/index.js",
|
|
28
|
+
"module": "esm/index.js",
|
|
29
|
+
"typings": "typings/index.d.ts",
|
|
30
30
|
"typescript": {
|
|
31
|
-
"definition": "index.d.ts"
|
|
31
|
+
"definition": "typings/index.d.ts"
|
|
32
32
|
},
|
|
33
|
+
"type": "module",
|
|
33
34
|
"exports": {
|
|
34
35
|
".": {
|
|
35
|
-
"require":
|
|
36
|
-
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./typings/index.d.ts",
|
|
38
|
+
"default": "./cjs/index.js"
|
|
39
|
+
},
|
|
40
|
+
"import": {
|
|
41
|
+
"types": "./typings/index.d.ts",
|
|
42
|
+
"default": "./esm/index.js"
|
|
43
|
+
},
|
|
44
|
+
"default": {
|
|
45
|
+
"types": "./typings/index.d.ts",
|
|
46
|
+
"default": "./esm/index.js"
|
|
47
|
+
}
|
|
37
48
|
},
|
|
38
49
|
"./*": {
|
|
39
|
-
"require":
|
|
40
|
-
|
|
50
|
+
"require": {
|
|
51
|
+
"types": "./typings/*.d.ts",
|
|
52
|
+
"default": "./cjs/*.js"
|
|
53
|
+
},
|
|
54
|
+
"import": {
|
|
55
|
+
"types": "./typings/*.d.ts",
|
|
56
|
+
"default": "./esm/*.js"
|
|
57
|
+
},
|
|
58
|
+
"default": {
|
|
59
|
+
"types": "./typings/*.d.ts",
|
|
60
|
+
"default": "./esm/*.js"
|
|
61
|
+
}
|
|
41
62
|
},
|
|
42
63
|
"./package.json": "./package.json"
|
|
43
64
|
}
|
|
44
|
-
}
|
|
65
|
+
}
|