@envelop/core 3.0.0-alpha-d0d0776.0 → 3.0.0-alpha-ccec0fc.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.
Files changed (53) hide show
  1. package/cjs/create.js +28 -0
  2. package/cjs/enable-if.js +24 -0
  3. package/cjs/index.js +16 -0
  4. package/cjs/orchestrator.js +452 -0
  5. package/cjs/package.json +1 -0
  6. package/cjs/plugins/use-envelop.js +13 -0
  7. package/cjs/plugins/use-error-handler.js +21 -0
  8. package/cjs/plugins/use-extend-context.js +9 -0
  9. package/cjs/plugins/use-immediate-introspection.js +50 -0
  10. package/cjs/plugins/use-logger.js +45 -0
  11. package/cjs/plugins/use-masked-errors.js +90 -0
  12. package/cjs/plugins/use-payload-formatter.js +21 -0
  13. package/cjs/plugins/use-schema.js +29 -0
  14. package/cjs/plugins/use-timing.js +138 -0
  15. package/cjs/traced-orchestrator.js +136 -0
  16. package/cjs/traced-schema.js +68 -0
  17. package/cjs/utils.js +221 -0
  18. package/esm/create.js +24 -0
  19. package/esm/enable-if.js +19 -0
  20. package/esm/index.js +13 -0
  21. package/esm/orchestrator.js +448 -0
  22. package/esm/plugins/use-envelop.js +9 -0
  23. package/esm/plugins/use-error-handler.js +17 -0
  24. package/esm/plugins/use-extend-context.js +5 -0
  25. package/esm/plugins/use-immediate-introspection.js +46 -0
  26. package/esm/plugins/use-logger.js +41 -0
  27. package/esm/plugins/use-masked-errors.js +84 -0
  28. package/esm/plugins/use-payload-formatter.js +17 -0
  29. package/esm/plugins/use-schema.js +23 -0
  30. package/esm/plugins/use-timing.js +134 -0
  31. package/esm/traced-orchestrator.js +132 -0
  32. package/esm/traced-schema.js +64 -0
  33. package/esm/utils.js +207 -0
  34. package/package.json +25 -18
  35. package/{create.d.ts → typings/create.d.ts} +1 -1
  36. package/{enable-if.d.ts → typings/enable-if.d.ts} +0 -0
  37. package/typings/index.d.ts +13 -0
  38. package/{orchestrator.d.ts → typings/orchestrator.d.ts} +1 -1
  39. package/{plugins → typings/plugins}/use-envelop.d.ts +0 -0
  40. package/{plugins → typings/plugins}/use-error-handler.d.ts +1 -1
  41. package/{plugins → typings/plugins}/use-extend-context.d.ts +0 -0
  42. package/{plugins → typings/plugins}/use-immediate-introspection.d.ts +0 -0
  43. package/{plugins → typings/plugins}/use-logger.d.ts +1 -1
  44. package/{plugins → typings/plugins}/use-masked-errors.d.ts +2 -2
  45. package/{plugins → typings/plugins}/use-payload-formatter.d.ts +1 -1
  46. package/{plugins → typings/plugins}/use-schema.d.ts +1 -1
  47. package/{plugins → typings/plugins}/use-timing.d.ts +3 -3
  48. package/{traced-orchestrator.d.ts → typings/traced-orchestrator.d.ts} +1 -1
  49. package/{traced-schema.d.ts → typings/traced-schema.d.ts} +1 -1
  50. package/{utils.d.ts → typings/utils.d.ts} +1 -1
  51. package/index.d.ts +0 -13
  52. package/index.js +0 -1295
  53. package/index.mjs +0 -1257
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-tools/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,24 @@
1
+ import { isPluginEnabled } from './enable-if.js';
2
+ import { createEnvelopOrchestrator } from './orchestrator.js';
3
+ import { traceOrchestrator } from './traced-orchestrator.js';
4
+ export function envelop(options) {
5
+ const plugins = options.plugins.filter(isPluginEnabled);
6
+ let orchestrator = createEnvelopOrchestrator(plugins);
7
+ if (options.enableInternalTracing) {
8
+ orchestrator = traceOrchestrator(orchestrator);
9
+ }
10
+ const getEnveloped = (initialContext = {}) => {
11
+ const typedOrchestrator = orchestrator;
12
+ typedOrchestrator.init(initialContext);
13
+ return {
14
+ parse: typedOrchestrator.parse(initialContext),
15
+ validate: typedOrchestrator.validate(initialContext),
16
+ contextFactory: typedOrchestrator.contextFactory(initialContext),
17
+ execute: typedOrchestrator.execute,
18
+ subscribe: typedOrchestrator.subscribe,
19
+ schema: typedOrchestrator.getCurrentSchema(),
20
+ };
21
+ };
22
+ getEnveloped._plugins = plugins;
23
+ return getEnveloped;
24
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * This enum is used only internally in order to create nominal type for the disabled plugin
3
+ */
4
+ var EnableIfBranded;
5
+ (function (EnableIfBranded) {
6
+ EnableIfBranded[EnableIfBranded["DisabledPlugin"] = 0] = "DisabledPlugin";
7
+ })(EnableIfBranded || (EnableIfBranded = {}));
8
+ export function isPluginEnabled(t) {
9
+ return t !== EnableIfBranded.DisabledPlugin && t !== null;
10
+ }
11
+ /**
12
+ * Utility function to enable a plugin.
13
+ */
14
+ export function enableIf(condition, plugin) {
15
+ if (condition) {
16
+ return typeof plugin === 'function' ? plugin() : plugin;
17
+ }
18
+ return EnableIfBranded.DisabledPlugin;
19
+ }
package/esm/index.js ADDED
@@ -0,0 +1,13 @@
1
+ export * from '@envelop/types';
2
+ export * from './create.js';
3
+ export * from './utils.js';
4
+ export * from './plugins/use-envelop.js';
5
+ export * from './plugins/use-logger.js';
6
+ export * from './plugins/use-timing.js';
7
+ export * from './plugins/use-schema.js';
8
+ export * from './plugins/use-error-handler.js';
9
+ export * from './plugins/use-extend-context.js';
10
+ export * from './plugins/use-payload-formatter.js';
11
+ export * from './plugins/use-masked-errors.js';
12
+ export * from './plugins/use-immediate-introspection.js';
13
+ export * from './enable-if.js';