@graphql-mesh/runtime 0.96.2 → 0.96.3-alpha-20230904122056-1177de280
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/get-mesh.js +71 -36
- package/cjs/in-context-sdk.js +43 -42
- package/cjs/useSubschema.js +12 -6
- package/cjs/utils.js +24 -0
- package/esm/get-mesh.js +72 -37
- package/esm/in-context-sdk.js +44 -43
- package/esm/useSubschema.js +13 -7
- package/esm/utils.js +20 -0
- package/package.json +1 -1
- package/typings/in-context-sdk.d.cts +1 -1
- package/typings/in-context-sdk.d.ts +1 -1
- package/typings/utils.d.cts +1 -0
- package/typings/utils.d.ts +1 -0
package/cjs/get-mesh.js
CHANGED
|
@@ -12,6 +12,7 @@ const fetch_1 = require("@whatwg-node/fetch");
|
|
|
12
12
|
const constants_js_1 = require("./constants.js");
|
|
13
13
|
const in_context_sdk_js_1 = require("./in-context-sdk.js");
|
|
14
14
|
const useSubschema_js_1 = require("./useSubschema.js");
|
|
15
|
+
const utils_js_1 = require("./utils.js");
|
|
15
16
|
const memoizedGetEnvelopedFactory = (0, utils_2.memoize1)(function getEnvelopedFactory(plugins) {
|
|
16
17
|
return (0, core_1.envelop)({
|
|
17
18
|
plugins,
|
|
@@ -25,7 +26,13 @@ const memoizedGetOperationType = (0, utils_2.memoize1)((document) => {
|
|
|
25
26
|
return operationAST.operation;
|
|
26
27
|
});
|
|
27
28
|
function wrapFetchWithPlugins(plugins) {
|
|
28
|
-
|
|
29
|
+
const onFetchHooks = [];
|
|
30
|
+
for (const plugin of plugins) {
|
|
31
|
+
if (plugin?.onFetch != null) {
|
|
32
|
+
onFetchHooks.push(plugin.onFetch);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return function wrappedFetchFn(url, options, context, info) {
|
|
29
36
|
if (url != null && typeof url !== 'string') {
|
|
30
37
|
throw new TypeError(`First parameter(url) of 'fetch' must be a string, got ${(0, utils_2.inspect)(url)}`);
|
|
31
38
|
}
|
|
@@ -40,33 +47,31 @@ function wrapFetchWithPlugins(plugins) {
|
|
|
40
47
|
}
|
|
41
48
|
let fetchFn;
|
|
42
49
|
const doneHooks = [];
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
function setFetchFn(newFetchFn) {
|
|
51
|
+
fetchFn = newFetchFn;
|
|
52
|
+
}
|
|
53
|
+
const result$ = (0, utils_js_1.iterateAsync)(onFetchHooks, onFetch => onFetch({
|
|
54
|
+
fetchFn,
|
|
55
|
+
setFetchFn,
|
|
56
|
+
url,
|
|
57
|
+
options,
|
|
58
|
+
context,
|
|
59
|
+
info,
|
|
60
|
+
}), doneHooks);
|
|
61
|
+
function handleIterationResult() {
|
|
62
|
+
const response$ = fetchFn(url, options, context, info);
|
|
63
|
+
if (doneHooks.length === 0) {
|
|
64
|
+
return response$;
|
|
58
65
|
}
|
|
66
|
+
if ((0, utils_2.isPromise)(response$)) {
|
|
67
|
+
return response$.then(response => handleOnFetchDone(response, doneHooks));
|
|
68
|
+
}
|
|
69
|
+
return handleOnFetchDone(response$, doneHooks);
|
|
59
70
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
await doneHook({
|
|
63
|
-
response,
|
|
64
|
-
setResponse(newResponse) {
|
|
65
|
-
response = newResponse;
|
|
66
|
-
},
|
|
67
|
-
});
|
|
71
|
+
if ((0, utils_2.isPromise)(result$)) {
|
|
72
|
+
return result$.then(handleIterationResult);
|
|
68
73
|
}
|
|
69
|
-
return
|
|
74
|
+
return handleIterationResult();
|
|
70
75
|
};
|
|
71
76
|
}
|
|
72
77
|
exports.wrapFetchWithPlugins = wrapFetchWithPlugins;
|
|
@@ -182,7 +187,7 @@ async function getMesh(options) {
|
|
|
182
187
|
unifiedSubschema.transforms.push(rootLevelTransform);
|
|
183
188
|
}
|
|
184
189
|
}
|
|
185
|
-
let inContextSDK
|
|
190
|
+
let inContextSDK;
|
|
186
191
|
const subschema = new delegate_1.Subschema(unifiedSubschema);
|
|
187
192
|
const plugins = [
|
|
188
193
|
(0, core_1.useEngine)({
|
|
@@ -192,16 +197,16 @@ async function getMesh(options) {
|
|
|
192
197
|
}),
|
|
193
198
|
(0, useSubschema_js_1.useSubschema)(subschema),
|
|
194
199
|
(0, core_1.useExtendContext)(() => {
|
|
195
|
-
if (!inContextSDK
|
|
200
|
+
if (!inContextSDK) {
|
|
196
201
|
const onDelegateHooks = [];
|
|
197
202
|
for (const plugin of initialPluginList) {
|
|
198
203
|
if (plugin?.onDelegate != null) {
|
|
199
204
|
onDelegateHooks.push(plugin.onDelegate);
|
|
200
205
|
}
|
|
201
206
|
}
|
|
202
|
-
inContextSDK
|
|
207
|
+
inContextSDK = (0, in_context_sdk_js_1.getInContextSDK)(subschema.transformedSchema, rawSources, logger, onDelegateHooks);
|
|
203
208
|
}
|
|
204
|
-
return inContextSDK
|
|
209
|
+
return inContextSDK;
|
|
205
210
|
}),
|
|
206
211
|
(0, extended_validation_1.useExtendedValidation)({
|
|
207
212
|
rules: [extended_validation_1.OneOfInputObjectsRule],
|
|
@@ -214,13 +219,24 @@ async function getMesh(options) {
|
|
|
214
219
|
function createExecutor(globalContext = EMPTY_CONTEXT_VALUE) {
|
|
215
220
|
const getEnveloped = memoizedGetEnvelopedFactory(plugins);
|
|
216
221
|
const { schema, parse, execute, subscribe, contextFactory } = getEnveloped(globalContext);
|
|
217
|
-
return
|
|
222
|
+
return function meshExecutor(documentOrSDL, variableValues = EMPTY_VARIABLES_VALUE, contextValue = EMPTY_CONTEXT_VALUE, rootValue = EMPTY_ROOT_VALUE, operationName) {
|
|
218
223
|
const document = typeof documentOrSDL === 'string' ? parse(documentOrSDL) : documentOrSDL;
|
|
219
224
|
const executeFn = memoizedGetOperationType(document) === 'subscription' ? subscribe : execute;
|
|
225
|
+
const contextValue$ = contextFactory(contextValue);
|
|
226
|
+
if ((0, utils_2.isPromise)(contextValue$)) {
|
|
227
|
+
return contextValue$.then(contextValue => executeFn({
|
|
228
|
+
schema,
|
|
229
|
+
document,
|
|
230
|
+
contextValue,
|
|
231
|
+
rootValue,
|
|
232
|
+
variableValues: variableValues,
|
|
233
|
+
operationName,
|
|
234
|
+
}));
|
|
235
|
+
}
|
|
220
236
|
return executeFn({
|
|
221
237
|
schema,
|
|
222
238
|
document,
|
|
223
|
-
contextValue:
|
|
239
|
+
contextValue: contextValue$,
|
|
224
240
|
rootValue,
|
|
225
241
|
variableValues: variableValues,
|
|
226
242
|
operationName,
|
|
@@ -229,12 +245,12 @@ async function getMesh(options) {
|
|
|
229
245
|
}
|
|
230
246
|
function sdkRequesterFactory(globalContext) {
|
|
231
247
|
const executor = createExecutor(globalContext);
|
|
232
|
-
return
|
|
233
|
-
const result =
|
|
234
|
-
if ((0, utils_2.
|
|
235
|
-
return (
|
|
248
|
+
return function sdkRequester(...args) {
|
|
249
|
+
const result$ = executor(...args);
|
|
250
|
+
if ((0, utils_2.isPromise)(result$)) {
|
|
251
|
+
return result$.then(handleExecutorResultForSdk);
|
|
236
252
|
}
|
|
237
|
-
return
|
|
253
|
+
return handleExecutorResultForSdk(result$);
|
|
238
254
|
};
|
|
239
255
|
}
|
|
240
256
|
function meshDestroy() {
|
|
@@ -264,6 +280,12 @@ async function getMesh(options) {
|
|
|
264
280
|
};
|
|
265
281
|
}
|
|
266
282
|
exports.getMesh = getMesh;
|
|
283
|
+
function handleExecutorResultForSdk(result) {
|
|
284
|
+
if ((0, utils_2.isAsyncIterable)(result)) {
|
|
285
|
+
return (0, utils_2.mapAsyncIterator)(result, extractDataOrThrowErrors);
|
|
286
|
+
}
|
|
287
|
+
return extractDataOrThrowErrors(result);
|
|
288
|
+
}
|
|
267
289
|
function extractDataOrThrowErrors(result) {
|
|
268
290
|
if (result.errors) {
|
|
269
291
|
if (result.errors.length === 1) {
|
|
@@ -273,3 +295,16 @@ function extractDataOrThrowErrors(result) {
|
|
|
273
295
|
}
|
|
274
296
|
return result.data;
|
|
275
297
|
}
|
|
298
|
+
function handleOnFetchDone(response, onFetchDoneHooks) {
|
|
299
|
+
function setResponse(newResponse) {
|
|
300
|
+
response = newResponse;
|
|
301
|
+
}
|
|
302
|
+
const result$ = (0, utils_js_1.iterateAsync)(onFetchDoneHooks, onFetchDone => onFetchDone({
|
|
303
|
+
response,
|
|
304
|
+
setResponse,
|
|
305
|
+
}));
|
|
306
|
+
if ((0, utils_2.isPromise)(result$)) {
|
|
307
|
+
return result$.then(() => response);
|
|
308
|
+
}
|
|
309
|
+
return response;
|
|
310
|
+
}
|
package/cjs/in-context-sdk.js
CHANGED
|
@@ -8,7 +8,8 @@ const delegate_1 = require("@graphql-tools/delegate");
|
|
|
8
8
|
const utils_2 = require("@graphql-tools/utils");
|
|
9
9
|
const wrap_1 = require("@graphql-tools/wrap");
|
|
10
10
|
const constants_js_1 = require("./constants.js");
|
|
11
|
-
|
|
11
|
+
const utils_js_1 = require("./utils.js");
|
|
12
|
+
function getInContextSDK(unifiedSchema, rawSources, logger, onDelegateHooks) {
|
|
12
13
|
const inContextSDK = {};
|
|
13
14
|
const sourceMap = unifiedSchema.extensions.sourceMap;
|
|
14
15
|
for (const rawSource of rawSources) {
|
|
@@ -49,7 +50,7 @@ async function getInContextSDK(unifiedSchema, rawSources, logger, onDelegateHook
|
|
|
49
50
|
const inContextSdkLogger = rawSourceLogger.child(`InContextSDK.${rootType.name}.${fieldName}`);
|
|
50
51
|
const namedReturnType = (0, graphql_1.getNamedType)(rootTypeField.type);
|
|
51
52
|
const shouldHaveSelectionSet = !(0, graphql_1.isLeafType)(namedReturnType);
|
|
52
|
-
rawSourceContext[rootType.name][fieldName] =
|
|
53
|
+
rawSourceContext[rootType.name][fieldName] = ({ root, args, context, info = {
|
|
53
54
|
fieldName,
|
|
54
55
|
fieldNodes: [],
|
|
55
56
|
returnType: namedReturnType,
|
|
@@ -150,27 +151,17 @@ async function getInContextSDK(unifiedSchema, rawSources, logger, onDelegateHook
|
|
|
150
151
|
batchDelegationOptions.transforms = [wrapQueryTransform];
|
|
151
152
|
}
|
|
152
153
|
const onDelegateHookDones = [];
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
let result = await (0, batch_delegate_1.batchDelegateToSchema)(batchDelegationOptions);
|
|
165
|
-
for (const onDelegateHookDone of onDelegateHookDones) {
|
|
166
|
-
await onDelegateHookDone({
|
|
167
|
-
result,
|
|
168
|
-
setResult(newResult) {
|
|
169
|
-
result = newResult;
|
|
170
|
-
},
|
|
171
|
-
});
|
|
154
|
+
const onDelegatePayload = {
|
|
155
|
+
...batchDelegationOptions,
|
|
156
|
+
sourceName: rawSource.name,
|
|
157
|
+
typeName: rootType.name,
|
|
158
|
+
fieldName,
|
|
159
|
+
};
|
|
160
|
+
const onDelegateResult$ = (0, utils_js_1.iterateAsync)(onDelegateHooks, onDelegateHook => onDelegateHook(onDelegatePayload), onDelegateHookDones);
|
|
161
|
+
if ((0, utils_2.isPromise)(onDelegateResult$)) {
|
|
162
|
+
return onDelegateResult$.then(() => handleIterationResult(batch_delegate_1.batchDelegateToSchema, batchDelegationOptions, onDelegateHookDones));
|
|
172
163
|
}
|
|
173
|
-
return
|
|
164
|
+
return handleIterationResult(batch_delegate_1.batchDelegateToSchema, batchDelegationOptions, onDelegateHookDones);
|
|
174
165
|
}
|
|
175
166
|
else {
|
|
176
167
|
const regularDelegateOptions = {
|
|
@@ -184,27 +175,17 @@ async function getInContextSDK(unifiedSchema, rawSources, logger, onDelegateHook
|
|
|
184
175
|
regularDelegateOptions.transforms = [wrapQueryTransform];
|
|
185
176
|
}
|
|
186
177
|
const onDelegateHookDones = [];
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
let result = await (0, delegate_1.delegateToSchema)(regularDelegateOptions);
|
|
199
|
-
for (const onDelegateHookDone of onDelegateHookDones) {
|
|
200
|
-
await onDelegateHookDone({
|
|
201
|
-
result,
|
|
202
|
-
setResult(newResult) {
|
|
203
|
-
result = newResult;
|
|
204
|
-
},
|
|
205
|
-
});
|
|
178
|
+
const onDelegatePayload = {
|
|
179
|
+
...regularDelegateOptions,
|
|
180
|
+
sourceName: rawSource.name,
|
|
181
|
+
typeName: rootType.name,
|
|
182
|
+
fieldName,
|
|
183
|
+
};
|
|
184
|
+
const onDelegateResult$ = (0, utils_js_1.iterateAsync)(onDelegateHooks, onDelegateHook => onDelegateHook(onDelegatePayload), onDelegateHookDones);
|
|
185
|
+
if ((0, utils_2.isPromise)(onDelegateResult$)) {
|
|
186
|
+
return onDelegateResult$.then(() => handleIterationResult(delegate_1.delegateToSchema, regularDelegateOptions, onDelegateHookDones));
|
|
206
187
|
}
|
|
207
|
-
return
|
|
188
|
+
return handleIterationResult(delegate_1.delegateToSchema, regularDelegateOptions, onDelegateHookDones);
|
|
208
189
|
}
|
|
209
190
|
};
|
|
210
191
|
}
|
|
@@ -248,3 +229,23 @@ function normalizeSelectionSetParamOrFactory(selectionSetParamOrFactory) {
|
|
|
248
229
|
function identical(val) {
|
|
249
230
|
return val;
|
|
250
231
|
}
|
|
232
|
+
function handleIterationResult(delegateFn, delegateOptions, onDelegateHookDones) {
|
|
233
|
+
const delegationResult$ = delegateFn(delegateOptions);
|
|
234
|
+
if ((0, utils_2.isPromise)(delegationResult$)) {
|
|
235
|
+
return delegationResult$.then(delegationResult => handleOnDelegateDone(delegationResult, onDelegateHookDones));
|
|
236
|
+
}
|
|
237
|
+
return handleOnDelegateDone(delegationResult$, onDelegateHookDones);
|
|
238
|
+
}
|
|
239
|
+
function handleOnDelegateDone(delegationResult, onDelegateHookDones) {
|
|
240
|
+
function setResult(newResult) {
|
|
241
|
+
delegationResult = newResult;
|
|
242
|
+
}
|
|
243
|
+
const onDelegateDoneResult$ = (0, utils_js_1.iterateAsync)(onDelegateHookDones, onDelegateHookDone => onDelegateHookDone({
|
|
244
|
+
result: delegationResult,
|
|
245
|
+
setResult,
|
|
246
|
+
}));
|
|
247
|
+
if ((0, utils_2.isPromise)(onDelegateDoneResult$)) {
|
|
248
|
+
return onDelegateDoneResult$.then(() => delegationResult);
|
|
249
|
+
}
|
|
250
|
+
return delegationResult;
|
|
251
|
+
}
|
package/cjs/useSubschema.js
CHANGED
|
@@ -28,7 +28,7 @@ function getIntrospectionOperationType(operationAST) {
|
|
|
28
28
|
return introspectionQueryType;
|
|
29
29
|
}
|
|
30
30
|
function getExecuteFn(subschema) {
|
|
31
|
-
return
|
|
31
|
+
return function subschemaExecute(args) {
|
|
32
32
|
const originalRequest = {
|
|
33
33
|
document: args.document,
|
|
34
34
|
variables: args.variableValues,
|
|
@@ -76,12 +76,18 @@ function getExecuteFn(subschema) {
|
|
|
76
76
|
*/
|
|
77
77
|
const transformationContext = {};
|
|
78
78
|
const transformedRequest = (0, utils_1.applyRequestTransforms)(originalRequest, delegationContext, transformationContext, subschema.transforms);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
function handleResult(originalResult) {
|
|
80
|
+
if ((0, utils_2.isAsyncIterable)(originalResult)) {
|
|
81
|
+
return (0, core_1.mapAsyncIterator)(originalResult, singleResult => (0, utils_1.applyResultTransforms)(singleResult, delegationContext, transformationContext, subschema.transforms));
|
|
82
|
+
}
|
|
83
|
+
const transformedResult = (0, utils_1.applyResultTransforms)(originalResult, delegationContext, transformationContext, subschema.transforms);
|
|
84
|
+
return transformedResult;
|
|
85
|
+
}
|
|
86
|
+
const originalResult$ = executor(transformedRequest);
|
|
87
|
+
if ((0, utils_2.isPromise)(originalResult$)) {
|
|
88
|
+
return originalResult$.then(handleResult);
|
|
82
89
|
}
|
|
83
|
-
|
|
84
|
-
return transformedResult;
|
|
90
|
+
return handleResult(originalResult$);
|
|
85
91
|
};
|
|
86
92
|
}
|
|
87
93
|
// Creates an envelop plugin to execute a subschema inside Envelop
|
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.iterateAsync = void 0;
|
|
4
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
5
|
+
function iterateAsync(iterable, callback, results) {
|
|
6
|
+
const iterator = iterable[Symbol.iterator]();
|
|
7
|
+
function iterate() {
|
|
8
|
+
const { done: endOfIterator, value } = iterator.next();
|
|
9
|
+
if (endOfIterator) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const result$ = callback(value);
|
|
13
|
+
if ((0, utils_1.isPromise)(result$)) {
|
|
14
|
+
return result$.then(result => {
|
|
15
|
+
results?.push(result);
|
|
16
|
+
return iterate();
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
results?.push(result$);
|
|
20
|
+
return iterate();
|
|
21
|
+
}
|
|
22
|
+
return iterate();
|
|
23
|
+
}
|
|
24
|
+
exports.iterateAsync = iterateAsync;
|
package/esm/get-mesh.js
CHANGED
|
@@ -4,11 +4,12 @@ import { OneOfInputObjectsRule, useExtendedValidation } from '@envelop/extended-
|
|
|
4
4
|
import { process } from '@graphql-mesh/cross-helpers';
|
|
5
5
|
import { applySchemaTransforms, DefaultLogger, getHeadersObj, groupTransforms, parseWithCache, PubSub, } from '@graphql-mesh/utils';
|
|
6
6
|
import { Subschema } from '@graphql-tools/delegate';
|
|
7
|
-
import { getRootTypeMap, inspect, isAsyncIterable, mapAsyncIterator, memoize1, } from '@graphql-tools/utils';
|
|
7
|
+
import { getRootTypeMap, inspect, isAsyncIterable, isPromise, mapAsyncIterator, memoize1, } from '@graphql-tools/utils';
|
|
8
8
|
import { fetch as defaultFetchFn } from '@whatwg-node/fetch';
|
|
9
9
|
import { MESH_CONTEXT_SYMBOL } from './constants.js';
|
|
10
10
|
import { getInContextSDK } from './in-context-sdk.js';
|
|
11
11
|
import { useSubschema } from './useSubschema.js';
|
|
12
|
+
import { iterateAsync } from './utils.js';
|
|
12
13
|
const memoizedGetEnvelopedFactory = memoize1(function getEnvelopedFactory(plugins) {
|
|
13
14
|
return envelop({
|
|
14
15
|
plugins,
|
|
@@ -22,7 +23,13 @@ const memoizedGetOperationType = memoize1((document) => {
|
|
|
22
23
|
return operationAST.operation;
|
|
23
24
|
});
|
|
24
25
|
export function wrapFetchWithPlugins(plugins) {
|
|
25
|
-
|
|
26
|
+
const onFetchHooks = [];
|
|
27
|
+
for (const plugin of plugins) {
|
|
28
|
+
if (plugin?.onFetch != null) {
|
|
29
|
+
onFetchHooks.push(plugin.onFetch);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return function wrappedFetchFn(url, options, context, info) {
|
|
26
33
|
if (url != null && typeof url !== 'string') {
|
|
27
34
|
throw new TypeError(`First parameter(url) of 'fetch' must be a string, got ${inspect(url)}`);
|
|
28
35
|
}
|
|
@@ -37,33 +44,31 @@ export function wrapFetchWithPlugins(plugins) {
|
|
|
37
44
|
}
|
|
38
45
|
let fetchFn;
|
|
39
46
|
const doneHooks = [];
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
function setFetchFn(newFetchFn) {
|
|
48
|
+
fetchFn = newFetchFn;
|
|
49
|
+
}
|
|
50
|
+
const result$ = iterateAsync(onFetchHooks, onFetch => onFetch({
|
|
51
|
+
fetchFn,
|
|
52
|
+
setFetchFn,
|
|
53
|
+
url,
|
|
54
|
+
options,
|
|
55
|
+
context,
|
|
56
|
+
info,
|
|
57
|
+
}), doneHooks);
|
|
58
|
+
function handleIterationResult() {
|
|
59
|
+
const response$ = fetchFn(url, options, context, info);
|
|
60
|
+
if (doneHooks.length === 0) {
|
|
61
|
+
return response$;
|
|
55
62
|
}
|
|
63
|
+
if (isPromise(response$)) {
|
|
64
|
+
return response$.then(response => handleOnFetchDone(response, doneHooks));
|
|
65
|
+
}
|
|
66
|
+
return handleOnFetchDone(response$, doneHooks);
|
|
56
67
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
await doneHook({
|
|
60
|
-
response,
|
|
61
|
-
setResponse(newResponse) {
|
|
62
|
-
response = newResponse;
|
|
63
|
-
},
|
|
64
|
-
});
|
|
68
|
+
if (isPromise(result$)) {
|
|
69
|
+
return result$.then(handleIterationResult);
|
|
65
70
|
}
|
|
66
|
-
return
|
|
71
|
+
return handleIterationResult();
|
|
67
72
|
};
|
|
68
73
|
}
|
|
69
74
|
// Use in-context-sdk for tracing
|
|
@@ -178,7 +183,7 @@ export async function getMesh(options) {
|
|
|
178
183
|
unifiedSubschema.transforms.push(rootLevelTransform);
|
|
179
184
|
}
|
|
180
185
|
}
|
|
181
|
-
let inContextSDK
|
|
186
|
+
let inContextSDK;
|
|
182
187
|
const subschema = new Subschema(unifiedSubschema);
|
|
183
188
|
const plugins = [
|
|
184
189
|
useEngine({
|
|
@@ -188,16 +193,16 @@ export async function getMesh(options) {
|
|
|
188
193
|
}),
|
|
189
194
|
useSubschema(subschema),
|
|
190
195
|
useExtendContext(() => {
|
|
191
|
-
if (!inContextSDK
|
|
196
|
+
if (!inContextSDK) {
|
|
192
197
|
const onDelegateHooks = [];
|
|
193
198
|
for (const plugin of initialPluginList) {
|
|
194
199
|
if (plugin?.onDelegate != null) {
|
|
195
200
|
onDelegateHooks.push(plugin.onDelegate);
|
|
196
201
|
}
|
|
197
202
|
}
|
|
198
|
-
inContextSDK
|
|
203
|
+
inContextSDK = getInContextSDK(subschema.transformedSchema, rawSources, logger, onDelegateHooks);
|
|
199
204
|
}
|
|
200
|
-
return inContextSDK
|
|
205
|
+
return inContextSDK;
|
|
201
206
|
}),
|
|
202
207
|
useExtendedValidation({
|
|
203
208
|
rules: [OneOfInputObjectsRule],
|
|
@@ -210,13 +215,24 @@ export async function getMesh(options) {
|
|
|
210
215
|
function createExecutor(globalContext = EMPTY_CONTEXT_VALUE) {
|
|
211
216
|
const getEnveloped = memoizedGetEnvelopedFactory(plugins);
|
|
212
217
|
const { schema, parse, execute, subscribe, contextFactory } = getEnveloped(globalContext);
|
|
213
|
-
return
|
|
218
|
+
return function meshExecutor(documentOrSDL, variableValues = EMPTY_VARIABLES_VALUE, contextValue = EMPTY_CONTEXT_VALUE, rootValue = EMPTY_ROOT_VALUE, operationName) {
|
|
214
219
|
const document = typeof documentOrSDL === 'string' ? parse(documentOrSDL) : documentOrSDL;
|
|
215
220
|
const executeFn = memoizedGetOperationType(document) === 'subscription' ? subscribe : execute;
|
|
221
|
+
const contextValue$ = contextFactory(contextValue);
|
|
222
|
+
if (isPromise(contextValue$)) {
|
|
223
|
+
return contextValue$.then(contextValue => executeFn({
|
|
224
|
+
schema,
|
|
225
|
+
document,
|
|
226
|
+
contextValue,
|
|
227
|
+
rootValue,
|
|
228
|
+
variableValues: variableValues,
|
|
229
|
+
operationName,
|
|
230
|
+
}));
|
|
231
|
+
}
|
|
216
232
|
return executeFn({
|
|
217
233
|
schema,
|
|
218
234
|
document,
|
|
219
|
-
contextValue:
|
|
235
|
+
contextValue: contextValue$,
|
|
220
236
|
rootValue,
|
|
221
237
|
variableValues: variableValues,
|
|
222
238
|
operationName,
|
|
@@ -225,12 +241,12 @@ export async function getMesh(options) {
|
|
|
225
241
|
}
|
|
226
242
|
function sdkRequesterFactory(globalContext) {
|
|
227
243
|
const executor = createExecutor(globalContext);
|
|
228
|
-
return
|
|
229
|
-
const result =
|
|
230
|
-
if (
|
|
231
|
-
return
|
|
244
|
+
return function sdkRequester(...args) {
|
|
245
|
+
const result$ = executor(...args);
|
|
246
|
+
if (isPromise(result$)) {
|
|
247
|
+
return result$.then(handleExecutorResultForSdk);
|
|
232
248
|
}
|
|
233
|
-
return
|
|
249
|
+
return handleExecutorResultForSdk(result$);
|
|
234
250
|
};
|
|
235
251
|
}
|
|
236
252
|
function meshDestroy() {
|
|
@@ -259,6 +275,12 @@ export async function getMesh(options) {
|
|
|
259
275
|
sdkRequesterFactory,
|
|
260
276
|
};
|
|
261
277
|
}
|
|
278
|
+
function handleExecutorResultForSdk(result) {
|
|
279
|
+
if (isAsyncIterable(result)) {
|
|
280
|
+
return mapAsyncIterator(result, extractDataOrThrowErrors);
|
|
281
|
+
}
|
|
282
|
+
return extractDataOrThrowErrors(result);
|
|
283
|
+
}
|
|
262
284
|
function extractDataOrThrowErrors(result) {
|
|
263
285
|
if (result.errors) {
|
|
264
286
|
if (result.errors.length === 1) {
|
|
@@ -268,3 +290,16 @@ function extractDataOrThrowErrors(result) {
|
|
|
268
290
|
}
|
|
269
291
|
return result.data;
|
|
270
292
|
}
|
|
293
|
+
function handleOnFetchDone(response, onFetchDoneHooks) {
|
|
294
|
+
function setResponse(newResponse) {
|
|
295
|
+
response = newResponse;
|
|
296
|
+
}
|
|
297
|
+
const result$ = iterateAsync(onFetchDoneHooks, onFetchDone => onFetchDone({
|
|
298
|
+
response,
|
|
299
|
+
setResponse,
|
|
300
|
+
}));
|
|
301
|
+
if (isPromise(result$)) {
|
|
302
|
+
return result$.then(() => response);
|
|
303
|
+
}
|
|
304
|
+
return response;
|
|
305
|
+
}
|
package/esm/in-context-sdk.js
CHANGED
|
@@ -2,10 +2,11 @@ import { getNamedType, isLeafType, Kind, print, } from 'graphql';
|
|
|
2
2
|
import { parseWithCache } from '@graphql-mesh/utils';
|
|
3
3
|
import { batchDelegateToSchema } from '@graphql-tools/batch-delegate';
|
|
4
4
|
import { delegateToSchema, } from '@graphql-tools/delegate';
|
|
5
|
-
import { buildOperationNodeForField, isDocumentNode, memoize1 } from '@graphql-tools/utils';
|
|
5
|
+
import { buildOperationNodeForField, isDocumentNode, isPromise, memoize1, } from '@graphql-tools/utils';
|
|
6
6
|
import { WrapQuery } from '@graphql-tools/wrap';
|
|
7
7
|
import { MESH_API_CONTEXT_SYMBOL } from './constants.js';
|
|
8
|
-
|
|
8
|
+
import { iterateAsync } from './utils.js';
|
|
9
|
+
export function getInContextSDK(unifiedSchema, rawSources, logger, onDelegateHooks) {
|
|
9
10
|
const inContextSDK = {};
|
|
10
11
|
const sourceMap = unifiedSchema.extensions.sourceMap;
|
|
11
12
|
for (const rawSource of rawSources) {
|
|
@@ -46,7 +47,7 @@ export async function getInContextSDK(unifiedSchema, rawSources, logger, onDeleg
|
|
|
46
47
|
const inContextSdkLogger = rawSourceLogger.child(`InContextSDK.${rootType.name}.${fieldName}`);
|
|
47
48
|
const namedReturnType = getNamedType(rootTypeField.type);
|
|
48
49
|
const shouldHaveSelectionSet = !isLeafType(namedReturnType);
|
|
49
|
-
rawSourceContext[rootType.name][fieldName] =
|
|
50
|
+
rawSourceContext[rootType.name][fieldName] = ({ root, args, context, info = {
|
|
50
51
|
fieldName,
|
|
51
52
|
fieldNodes: [],
|
|
52
53
|
returnType: namedReturnType,
|
|
@@ -147,27 +148,17 @@ export async function getInContextSDK(unifiedSchema, rawSources, logger, onDeleg
|
|
|
147
148
|
batchDelegationOptions.transforms = [wrapQueryTransform];
|
|
148
149
|
}
|
|
149
150
|
const onDelegateHookDones = [];
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
let result = await batchDelegateToSchema(batchDelegationOptions);
|
|
162
|
-
for (const onDelegateHookDone of onDelegateHookDones) {
|
|
163
|
-
await onDelegateHookDone({
|
|
164
|
-
result,
|
|
165
|
-
setResult(newResult) {
|
|
166
|
-
result = newResult;
|
|
167
|
-
},
|
|
168
|
-
});
|
|
151
|
+
const onDelegatePayload = {
|
|
152
|
+
...batchDelegationOptions,
|
|
153
|
+
sourceName: rawSource.name,
|
|
154
|
+
typeName: rootType.name,
|
|
155
|
+
fieldName,
|
|
156
|
+
};
|
|
157
|
+
const onDelegateResult$ = iterateAsync(onDelegateHooks, onDelegateHook => onDelegateHook(onDelegatePayload), onDelegateHookDones);
|
|
158
|
+
if (isPromise(onDelegateResult$)) {
|
|
159
|
+
return onDelegateResult$.then(() => handleIterationResult(batchDelegateToSchema, batchDelegationOptions, onDelegateHookDones));
|
|
169
160
|
}
|
|
170
|
-
return
|
|
161
|
+
return handleIterationResult(batchDelegateToSchema, batchDelegationOptions, onDelegateHookDones);
|
|
171
162
|
}
|
|
172
163
|
else {
|
|
173
164
|
const regularDelegateOptions = {
|
|
@@ -181,27 +172,17 @@ export async function getInContextSDK(unifiedSchema, rawSources, logger, onDeleg
|
|
|
181
172
|
regularDelegateOptions.transforms = [wrapQueryTransform];
|
|
182
173
|
}
|
|
183
174
|
const onDelegateHookDones = [];
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
let result = await delegateToSchema(regularDelegateOptions);
|
|
196
|
-
for (const onDelegateHookDone of onDelegateHookDones) {
|
|
197
|
-
await onDelegateHookDone({
|
|
198
|
-
result,
|
|
199
|
-
setResult(newResult) {
|
|
200
|
-
result = newResult;
|
|
201
|
-
},
|
|
202
|
-
});
|
|
175
|
+
const onDelegatePayload = {
|
|
176
|
+
...regularDelegateOptions,
|
|
177
|
+
sourceName: rawSource.name,
|
|
178
|
+
typeName: rootType.name,
|
|
179
|
+
fieldName,
|
|
180
|
+
};
|
|
181
|
+
const onDelegateResult$ = iterateAsync(onDelegateHooks, onDelegateHook => onDelegateHook(onDelegatePayload), onDelegateHookDones);
|
|
182
|
+
if (isPromise(onDelegateResult$)) {
|
|
183
|
+
return onDelegateResult$.then(() => handleIterationResult(delegateToSchema, regularDelegateOptions, onDelegateHookDones));
|
|
203
184
|
}
|
|
204
|
-
return
|
|
185
|
+
return handleIterationResult(delegateToSchema, regularDelegateOptions, onDelegateHookDones);
|
|
205
186
|
}
|
|
206
187
|
};
|
|
207
188
|
}
|
|
@@ -244,3 +225,23 @@ function normalizeSelectionSetParamOrFactory(selectionSetParamOrFactory) {
|
|
|
244
225
|
function identical(val) {
|
|
245
226
|
return val;
|
|
246
227
|
}
|
|
228
|
+
function handleIterationResult(delegateFn, delegateOptions, onDelegateHookDones) {
|
|
229
|
+
const delegationResult$ = delegateFn(delegateOptions);
|
|
230
|
+
if (isPromise(delegationResult$)) {
|
|
231
|
+
return delegationResult$.then(delegationResult => handleOnDelegateDone(delegationResult, onDelegateHookDones));
|
|
232
|
+
}
|
|
233
|
+
return handleOnDelegateDone(delegationResult$, onDelegateHookDones);
|
|
234
|
+
}
|
|
235
|
+
function handleOnDelegateDone(delegationResult, onDelegateHookDones) {
|
|
236
|
+
function setResult(newResult) {
|
|
237
|
+
delegationResult = newResult;
|
|
238
|
+
}
|
|
239
|
+
const onDelegateDoneResult$ = iterateAsync(onDelegateHookDones, onDelegateHookDone => onDelegateHookDone({
|
|
240
|
+
result: delegationResult,
|
|
241
|
+
setResult,
|
|
242
|
+
}));
|
|
243
|
+
if (isPromise(onDelegateDoneResult$)) {
|
|
244
|
+
return onDelegateDoneResult$.then(() => delegationResult);
|
|
245
|
+
}
|
|
246
|
+
return delegationResult;
|
|
247
|
+
}
|
package/esm/useSubschema.js
CHANGED
|
@@ -2,7 +2,7 @@ import { BREAK, execute, visit } from 'graphql';
|
|
|
2
2
|
import { mapAsyncIterator } from '@envelop/core';
|
|
3
3
|
import { applyRequestTransforms, applyResultTransforms } from '@graphql-mesh/utils';
|
|
4
4
|
import { applySchemaTransforms, createDefaultExecutor, } from '@graphql-tools/delegate';
|
|
5
|
-
import { getDefinedRootType, getOperationASTFromRequest, isAsyncIterable, printSchemaWithDirectives, } from '@graphql-tools/utils';
|
|
5
|
+
import { getDefinedRootType, getOperationASTFromRequest, isAsyncIterable, isPromise, printSchemaWithDirectives, } from '@graphql-tools/utils';
|
|
6
6
|
var IntrospectionQueryType;
|
|
7
7
|
(function (IntrospectionQueryType) {
|
|
8
8
|
IntrospectionQueryType["FEDERATION"] = "FEDERATION";
|
|
@@ -25,7 +25,7 @@ function getIntrospectionOperationType(operationAST) {
|
|
|
25
25
|
return introspectionQueryType;
|
|
26
26
|
}
|
|
27
27
|
function getExecuteFn(subschema) {
|
|
28
|
-
return
|
|
28
|
+
return function subschemaExecute(args) {
|
|
29
29
|
const originalRequest = {
|
|
30
30
|
document: args.document,
|
|
31
31
|
variables: args.variableValues,
|
|
@@ -73,12 +73,18 @@ function getExecuteFn(subschema) {
|
|
|
73
73
|
*/
|
|
74
74
|
const transformationContext = {};
|
|
75
75
|
const transformedRequest = applyRequestTransforms(originalRequest, delegationContext, transformationContext, subschema.transforms);
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
function handleResult(originalResult) {
|
|
77
|
+
if (isAsyncIterable(originalResult)) {
|
|
78
|
+
return mapAsyncIterator(originalResult, singleResult => applyResultTransforms(singleResult, delegationContext, transformationContext, subschema.transforms));
|
|
79
|
+
}
|
|
80
|
+
const transformedResult = applyResultTransforms(originalResult, delegationContext, transformationContext, subschema.transforms);
|
|
81
|
+
return transformedResult;
|
|
82
|
+
}
|
|
83
|
+
const originalResult$ = executor(transformedRequest);
|
|
84
|
+
if (isPromise(originalResult$)) {
|
|
85
|
+
return originalResult$.then(handleResult);
|
|
79
86
|
}
|
|
80
|
-
|
|
81
|
-
return transformedResult;
|
|
87
|
+
return handleResult(originalResult$);
|
|
82
88
|
};
|
|
83
89
|
}
|
|
84
90
|
// Creates an envelop plugin to execute a subschema inside Envelop
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { isPromise } from '@graphql-tools/utils';
|
|
2
|
+
export function iterateAsync(iterable, callback, results) {
|
|
3
|
+
const iterator = iterable[Symbol.iterator]();
|
|
4
|
+
function iterate() {
|
|
5
|
+
const { done: endOfIterator, value } = iterator.next();
|
|
6
|
+
if (endOfIterator) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const result$ = callback(value);
|
|
10
|
+
if (isPromise(result$)) {
|
|
11
|
+
return result$.then(result => {
|
|
12
|
+
results?.push(result);
|
|
13
|
+
return iterate();
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
results?.push(result$);
|
|
17
|
+
return iterate();
|
|
18
|
+
}
|
|
19
|
+
return iterate();
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
2
|
import { Logger, OnDelegateHook, RawSourceOutput } from '@graphql-mesh/types';
|
|
3
|
-
export declare function getInContextSDK(unifiedSchema: GraphQLSchema, rawSources: RawSourceOutput[], logger: Logger, onDelegateHooks: OnDelegateHook<any>[]):
|
|
3
|
+
export declare function getInContextSDK(unifiedSchema: GraphQLSchema, rawSources: RawSourceOutput[], logger: Logger, onDelegateHooks: OnDelegateHook<any>[]): Record<string, any>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
2
|
import { Logger, OnDelegateHook, RawSourceOutput } from '@graphql-mesh/types';
|
|
3
|
-
export declare function getInContextSDK(unifiedSchema: GraphQLSchema, rawSources: RawSourceOutput[], logger: Logger, onDelegateHooks: OnDelegateHook<any>[]):
|
|
3
|
+
export declare function getInContextSDK(unifiedSchema: GraphQLSchema, rawSources: RawSourceOutput[], logger: Logger, onDelegateHooks: OnDelegateHook<any>[]): Record<string, any>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function iterateAsync<TInput, TOutput>(iterable: Iterable<TInput>, callback: (input: TInput) => Promise<TOutput> | TOutput, results?: TOutput[]): Promise<void> | void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function iterateAsync<TInput, TOutput>(iterable: Iterable<TInput>, callback: (input: TInput) => Promise<TOutput> | TOutput, results?: TOutput[]): Promise<void> | void;
|