@envelop/core 2.4.0-alpha-aca44e1.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
package/cjs/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/esm/create.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.envelop = void 0;
|
|
4
|
+
const enable_if_js_1 = require("./enable-if.js");
|
|
5
|
+
const orchestrator_js_1 = require("./orchestrator.js");
|
|
6
|
+
const traced_orchestrator_js_1 = require("./traced-orchestrator.js");
|
|
7
|
+
function envelop(options) {
|
|
8
|
+
const plugins = options.plugins.filter(enable_if_js_1.isPluginEnabled);
|
|
9
|
+
let orchestrator = (0, orchestrator_js_1.createEnvelopOrchestrator)(plugins);
|
|
10
|
+
if (options.enableInternalTracing) {
|
|
11
|
+
orchestrator = (0, traced_orchestrator_js_1.traceOrchestrator)(orchestrator);
|
|
12
|
+
}
|
|
13
|
+
const getEnveloped = (initialContext = {}) => {
|
|
14
|
+
const typedOrchestrator = orchestrator;
|
|
15
|
+
typedOrchestrator.init(initialContext);
|
|
16
|
+
return {
|
|
17
|
+
parse: typedOrchestrator.parse(initialContext),
|
|
18
|
+
validate: typedOrchestrator.validate(initialContext),
|
|
19
|
+
contextFactory: typedOrchestrator.contextFactory(initialContext),
|
|
20
|
+
execute: typedOrchestrator.execute,
|
|
21
|
+
subscribe: typedOrchestrator.subscribe,
|
|
22
|
+
schema: typedOrchestrator.getCurrentSchema(),
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
getEnveloped._plugins = plugins;
|
|
26
|
+
return getEnveloped;
|
|
27
|
+
}
|
|
28
|
+
exports.envelop = envelop;
|
package/esm/enable-if.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.enableIf = exports.isPluginEnabled = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* This enum is used only internally in order to create nominal type for the disabled plugin
|
|
6
|
+
*/
|
|
7
|
+
var EnableIfBranded;
|
|
8
|
+
(function (EnableIfBranded) {
|
|
9
|
+
EnableIfBranded[EnableIfBranded["DisabledPlugin"] = 0] = "DisabledPlugin";
|
|
10
|
+
})(EnableIfBranded || (EnableIfBranded = {}));
|
|
11
|
+
function isPluginEnabled(t) {
|
|
12
|
+
return t !== EnableIfBranded.DisabledPlugin && t !== null;
|
|
13
|
+
}
|
|
14
|
+
exports.isPluginEnabled = isPluginEnabled;
|
|
15
|
+
/**
|
|
16
|
+
* Utility function to enable a plugin.
|
|
17
|
+
*/
|
|
18
|
+
function enableIf(condition, plugin) {
|
|
19
|
+
if (condition) {
|
|
20
|
+
return typeof plugin === 'function' ? plugin() : plugin;
|
|
21
|
+
}
|
|
22
|
+
return EnableIfBranded.DisabledPlugin;
|
|
23
|
+
}
|
|
24
|
+
exports.enableIf = enableIf;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("@envelop/types"), exports);
|
|
18
|
+
__exportStar(require("./create.js"), exports);
|
|
19
|
+
__exportStar(require("./utils.js"), exports);
|
|
20
|
+
__exportStar(require("./plugins/use-envelop.js"), exports);
|
|
21
|
+
__exportStar(require("./plugins/use-logger.js"), exports);
|
|
22
|
+
__exportStar(require("./plugins/use-timing.js"), exports);
|
|
23
|
+
__exportStar(require("./plugins/use-schema.js"), exports);
|
|
24
|
+
__exportStar(require("./plugins/use-error-handler.js"), exports);
|
|
25
|
+
__exportStar(require("./plugins/use-extend-context.js"), exports);
|
|
26
|
+
__exportStar(require("./plugins/use-payload-formatter.js"), exports);
|
|
27
|
+
__exportStar(require("./plugins/use-masked-errors.js"), exports);
|
|
28
|
+
__exportStar(require("./plugins/use-immediate-introspection.js"), exports);
|
|
29
|
+
__exportStar(require("./enable-if.js"), exports);
|