@graphql-mesh/runtime 1.0.0-alpha-20220804093904-8e2e41f7f → 1.0.0-alpha-20230420220344-25b6b92bf
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/constants.js +5 -0
- package/cjs/get-mesh.js +278 -0
- package/cjs/in-context-sdk.js +238 -0
- package/cjs/index.js +6 -0
- package/cjs/package.json +1 -0
- package/cjs/types.js +3 -0
- package/cjs/useSubschema.js +110 -0
- package/esm/constants.js +2 -0
- package/esm/get-mesh.js +273 -0
- package/esm/in-context-sdk.js +234 -0
- package/esm/index.js +3 -0
- package/esm/types.js +1 -0
- package/esm/useSubschema.js +106 -0
- package/package.json +33 -23
- package/typings/constants.d.ts +2 -0
- package/typings/get-mesh.d.cts +22 -0
- package/typings/get-mesh.d.ts +22 -0
- package/typings/in-context-sdk.d.cts +3 -0
- package/typings/in-context-sdk.d.ts +3 -0
- package/typings/index.d.cts +3 -0
- package/typings/index.d.ts +3 -0
- package/typings/types.d.cts +42 -0
- package/typings/types.d.ts +42 -0
- package/typings/useSubschema.d.cts +3 -0
- package/typings/useSubschema.d.ts +3 -0
- package/get-mesh.d.ts +0 -18
- package/in-context-sdk.d.ts +0 -3
- package/index.d.ts +0 -3
- package/index.js +0 -434
- package/index.mjs +0 -429
- package/types.d.ts +0 -42
- package/useSubschema.d.ts +0 -7
- /package/{constants.d.ts → typings/constants.d.cts} +0 -0
package/cjs/constants.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MESH_API_CONTEXT_SYMBOL = exports.MESH_CONTEXT_SYMBOL = void 0;
|
|
4
|
+
exports.MESH_CONTEXT_SYMBOL = Symbol('isMeshContext');
|
|
5
|
+
exports.MESH_API_CONTEXT_SYMBOL = Symbol('isMeshAPIContext');
|
package/cjs/get-mesh.js
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMesh = exports.wrapFetchWithPlugins = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const core_1 = require("@envelop/core");
|
|
6
|
+
const extended_validation_1 = require("@envelop/extended-validation");
|
|
7
|
+
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
|
|
8
|
+
const utils_1 = require("@graphql-mesh/utils");
|
|
9
|
+
const delegate_1 = require("@graphql-tools/delegate");
|
|
10
|
+
const utils_2 = require("@graphql-tools/utils");
|
|
11
|
+
const fetch_1 = require("@whatwg-node/fetch");
|
|
12
|
+
const constants_js_1 = require("./constants.js");
|
|
13
|
+
const in_context_sdk_js_1 = require("./in-context-sdk.js");
|
|
14
|
+
const useSubschema_js_1 = require("./useSubschema.js");
|
|
15
|
+
const memoizedGetEnvelopedFactory = (0, utils_2.memoize1)(function getEnvelopedFactory(plugins) {
|
|
16
|
+
return (0, core_1.envelop)({
|
|
17
|
+
plugins,
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
const memoizedGetOperationType = (0, utils_2.memoize1)((document) => {
|
|
21
|
+
const operationAST = (0, graphql_1.getOperationAST)(document, undefined);
|
|
22
|
+
if (!operationAST) {
|
|
23
|
+
throw new Error('Must provide document with a valid operation');
|
|
24
|
+
}
|
|
25
|
+
return operationAST.operation;
|
|
26
|
+
});
|
|
27
|
+
function wrapFetchWithPlugins(plugins) {
|
|
28
|
+
return async function wrappedFetchFn(url, options, context, info) {
|
|
29
|
+
if (url != null && typeof url !== 'string') {
|
|
30
|
+
throw new TypeError(`First parameter(url) of 'fetch' must be a string, got ${(0, utils_2.inspect)(url)}`);
|
|
31
|
+
}
|
|
32
|
+
if (options != null && typeof options !== 'object') {
|
|
33
|
+
throw new TypeError(`Second parameter(options) of 'fetch' must be an object, got ${(0, utils_2.inspect)(options)}`);
|
|
34
|
+
}
|
|
35
|
+
if (context != null && typeof context !== 'object') {
|
|
36
|
+
throw new TypeError(`Third parameter(context) of 'fetch' must be an object, got ${(0, utils_2.inspect)(context)}`);
|
|
37
|
+
}
|
|
38
|
+
if (info != null && typeof info !== 'object') {
|
|
39
|
+
throw new TypeError(`Fourth parameter(info) of 'fetch' must be an object, got ${(0, utils_2.inspect)(info)}`);
|
|
40
|
+
}
|
|
41
|
+
let fetchFn;
|
|
42
|
+
const doneHooks = [];
|
|
43
|
+
for (const plugin of plugins) {
|
|
44
|
+
if ((plugin === null || plugin === void 0 ? void 0 : plugin.onFetch) != null) {
|
|
45
|
+
const doneHook = await plugin.onFetch({
|
|
46
|
+
fetchFn,
|
|
47
|
+
setFetchFn(newFetchFn) {
|
|
48
|
+
fetchFn = newFetchFn;
|
|
49
|
+
},
|
|
50
|
+
url,
|
|
51
|
+
options,
|
|
52
|
+
context,
|
|
53
|
+
info,
|
|
54
|
+
});
|
|
55
|
+
if (doneHook) {
|
|
56
|
+
doneHooks.push(doneHook);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
let response = await fetchFn(url, options, context, info);
|
|
61
|
+
for (const doneHook of doneHooks) {
|
|
62
|
+
await doneHook({
|
|
63
|
+
response,
|
|
64
|
+
setResponse(newResponse) {
|
|
65
|
+
response = newResponse;
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return response;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
exports.wrapFetchWithPlugins = wrapFetchWithPlugins;
|
|
73
|
+
// Use in-context-sdk for tracing
|
|
74
|
+
function createProxyingResolverFactory(apiName, rootTypeMap) {
|
|
75
|
+
return function createProxyingResolver({ operation }) {
|
|
76
|
+
const rootType = rootTypeMap.get(operation);
|
|
77
|
+
return function proxyingResolver(root, args, context, info) {
|
|
78
|
+
var _a, _b;
|
|
79
|
+
if (!((_b = (_a = context === null || context === void 0 ? void 0 : context[apiName]) === null || _a === void 0 ? void 0 : _a[rootType.name]) === null || _b === void 0 ? void 0 : _b[info.fieldName])) {
|
|
80
|
+
throw new Error(`${info.fieldName} couldn't find in ${rootType.name} of ${apiName} as a ${operation}`);
|
|
81
|
+
}
|
|
82
|
+
return context[apiName][rootType.name][info.fieldName]({ root, args, context, info });
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async function getMesh(options) {
|
|
87
|
+
const rawSources = [];
|
|
88
|
+
const { pubsub = new utils_1.PubSub(), cache, logger = new utils_1.DefaultLogger('🕸️ Mesh'), additionalEnvelopPlugins = [], sources, merger, additionalResolvers = [], additionalTypeDefs = [], transforms = [], fetchFn = fetch_1.fetch, } = options;
|
|
89
|
+
const getMeshLogger = logger.child('GetMesh');
|
|
90
|
+
getMeshLogger.debug(`Getting subschemas from source handlers`);
|
|
91
|
+
let failed = false;
|
|
92
|
+
const initialPluginList = [
|
|
93
|
+
// TODO: Not a good practise to expect users to be a Yoga user
|
|
94
|
+
(0, core_1.useExtendContext)(({ request, req }) => {
|
|
95
|
+
// Maybe Node-like environment
|
|
96
|
+
if (req === null || req === void 0 ? void 0 : req.headers) {
|
|
97
|
+
return {
|
|
98
|
+
headers: (0, utils_1.getHeadersObj)(req.headers),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
// Fetch environment
|
|
102
|
+
if (request === null || request === void 0 ? void 0 : request.headers) {
|
|
103
|
+
return {
|
|
104
|
+
headers: (0, utils_1.getHeadersObj)(request.headers),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return {};
|
|
108
|
+
}),
|
|
109
|
+
(0, core_1.useExtendContext)(() => ({
|
|
110
|
+
pubsub,
|
|
111
|
+
cache,
|
|
112
|
+
logger,
|
|
113
|
+
[constants_js_1.MESH_CONTEXT_SYMBOL]: true,
|
|
114
|
+
})),
|
|
115
|
+
{
|
|
116
|
+
onFetch({ setFetchFn }) {
|
|
117
|
+
setFetchFn(fetchFn);
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
onParse({ setParseFn }) {
|
|
122
|
+
setParseFn(utils_1.parseWithCache);
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
...additionalEnvelopPlugins,
|
|
126
|
+
];
|
|
127
|
+
const wrappedFetchFn = wrapFetchWithPlugins(initialPluginList);
|
|
128
|
+
await Promise.allSettled(sources.map(async (apiSource) => {
|
|
129
|
+
const apiName = apiSource.name;
|
|
130
|
+
const sourceLogger = logger.child(apiName);
|
|
131
|
+
sourceLogger.debug(`Generating the schema`);
|
|
132
|
+
try {
|
|
133
|
+
const source = await apiSource.handler.getMeshSource({
|
|
134
|
+
fetchFn: wrappedFetchFn,
|
|
135
|
+
});
|
|
136
|
+
sourceLogger.debug(`The schema has been generated successfully`);
|
|
137
|
+
let apiSchema = source.schema;
|
|
138
|
+
sourceLogger.debug(`Analyzing transforms`);
|
|
139
|
+
let transforms;
|
|
140
|
+
const { wrapTransforms, noWrapTransforms } = (0, utils_1.groupTransforms)(apiSource.transforms);
|
|
141
|
+
if (!(wrapTransforms === null || wrapTransforms === void 0 ? void 0 : wrapTransforms.length) && (noWrapTransforms === null || noWrapTransforms === void 0 ? void 0 : noWrapTransforms.length)) {
|
|
142
|
+
sourceLogger.debug(`${noWrapTransforms.length} bare transforms found and applying`);
|
|
143
|
+
apiSchema = (0, utils_1.applySchemaTransforms)(apiSchema, source, null, noWrapTransforms);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
transforms = apiSource.transforms;
|
|
147
|
+
}
|
|
148
|
+
const rootTypeMap = (0, utils_2.getRootTypeMap)(apiSchema);
|
|
149
|
+
rawSources.push({
|
|
150
|
+
name: apiName,
|
|
151
|
+
schema: apiSchema,
|
|
152
|
+
executor: source.executor,
|
|
153
|
+
transforms,
|
|
154
|
+
contextVariables: source.contextVariables || {},
|
|
155
|
+
handler: apiSource.handler,
|
|
156
|
+
batch: 'batch' in source ? source.batch : true,
|
|
157
|
+
merge: source.merge,
|
|
158
|
+
createProxyingResolver: createProxyingResolverFactory(apiName, rootTypeMap),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
sourceLogger.error(`Failed to generate the schema`, e);
|
|
163
|
+
failed = true;
|
|
164
|
+
}
|
|
165
|
+
}));
|
|
166
|
+
if (failed) {
|
|
167
|
+
throw new Error(`Schemas couldn't be generated successfully. Check for the logs by running Mesh${cross_helpers_1.process.env.DEBUG == null
|
|
168
|
+
? ' with DEBUG=1 environmental variable to get more verbose output'
|
|
169
|
+
: ''}.`);
|
|
170
|
+
}
|
|
171
|
+
getMeshLogger.debug(`Schemas have been generated by the source handlers`);
|
|
172
|
+
getMeshLogger.debug(`Merging schemas using the defined merging strategy.`);
|
|
173
|
+
const unifiedSubschema = await merger.getUnifiedSchema({
|
|
174
|
+
rawSources,
|
|
175
|
+
typeDefs: additionalTypeDefs,
|
|
176
|
+
resolvers: additionalResolvers,
|
|
177
|
+
});
|
|
178
|
+
unifiedSubschema.transforms = unifiedSubschema.transforms || [];
|
|
179
|
+
for (const rootLevelTransform of transforms) {
|
|
180
|
+
if (rootLevelTransform.noWrap) {
|
|
181
|
+
if (rootLevelTransform.transformSchema) {
|
|
182
|
+
unifiedSubschema.schema = rootLevelTransform.transformSchema(unifiedSubschema.schema, unifiedSubschema);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
unifiedSubschema.transforms.push(rootLevelTransform);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
let inContextSDK$;
|
|
190
|
+
const subschema = new delegate_1.Subschema(unifiedSubschema);
|
|
191
|
+
const plugins = [
|
|
192
|
+
(0, core_1.useEngine)({
|
|
193
|
+
validate: graphql_1.validate,
|
|
194
|
+
specifiedRules: graphql_1.specifiedRules,
|
|
195
|
+
}),
|
|
196
|
+
(0, useSubschema_js_1.useSubschema)(subschema),
|
|
197
|
+
(0, core_1.useExtendContext)(() => {
|
|
198
|
+
if (!inContextSDK$) {
|
|
199
|
+
const onDelegateHooks = [];
|
|
200
|
+
for (const plugin of initialPluginList) {
|
|
201
|
+
if ((plugin === null || plugin === void 0 ? void 0 : plugin.onDelegate) != null) {
|
|
202
|
+
onDelegateHooks.push(plugin.onDelegate);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
inContextSDK$ = (0, in_context_sdk_js_1.getInContextSDK)(subschema.transformedSchema, rawSources, logger, onDelegateHooks);
|
|
206
|
+
}
|
|
207
|
+
return inContextSDK$;
|
|
208
|
+
}),
|
|
209
|
+
(0, extended_validation_1.useExtendedValidation)({
|
|
210
|
+
rules: [extended_validation_1.OneOfInputObjectsRule],
|
|
211
|
+
}),
|
|
212
|
+
...initialPluginList,
|
|
213
|
+
];
|
|
214
|
+
const EMPTY_ROOT_VALUE = {};
|
|
215
|
+
const EMPTY_CONTEXT_VALUE = {};
|
|
216
|
+
const EMPTY_VARIABLES_VALUE = {};
|
|
217
|
+
function createExecutor(globalContext = EMPTY_CONTEXT_VALUE) {
|
|
218
|
+
const getEnveloped = memoizedGetEnvelopedFactory(plugins);
|
|
219
|
+
const { schema, parse, execute, subscribe, contextFactory } = getEnveloped(globalContext);
|
|
220
|
+
return async function meshExecutor(documentOrSDL, variableValues = EMPTY_VARIABLES_VALUE, contextValue = EMPTY_CONTEXT_VALUE, rootValue = EMPTY_ROOT_VALUE, operationName) {
|
|
221
|
+
const document = typeof documentOrSDL === 'string' ? parse(documentOrSDL) : documentOrSDL;
|
|
222
|
+
const executeFn = memoizedGetOperationType(document) === 'subscription' ? subscribe : execute;
|
|
223
|
+
return executeFn({
|
|
224
|
+
schema,
|
|
225
|
+
document,
|
|
226
|
+
contextValue: await contextFactory(contextValue),
|
|
227
|
+
rootValue,
|
|
228
|
+
variableValues: variableValues,
|
|
229
|
+
operationName,
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function sdkRequesterFactory(globalContext) {
|
|
234
|
+
const executor = createExecutor(globalContext);
|
|
235
|
+
return async function sdkRequester(...args) {
|
|
236
|
+
const result = await executor(...args);
|
|
237
|
+
if ((0, utils_2.isAsyncIterable)(result)) {
|
|
238
|
+
return (0, utils_2.mapAsyncIterator)(result, extractDataOrThrowErrors);
|
|
239
|
+
}
|
|
240
|
+
return extractDataOrThrowErrors(result);
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function meshDestroy() {
|
|
244
|
+
return pubsub.publish('destroy', undefined);
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
get schema() {
|
|
248
|
+
return subschema.transformedSchema;
|
|
249
|
+
},
|
|
250
|
+
rawSources,
|
|
251
|
+
cache,
|
|
252
|
+
pubsub,
|
|
253
|
+
destroy: meshDestroy,
|
|
254
|
+
logger,
|
|
255
|
+
plugins,
|
|
256
|
+
get getEnveloped() {
|
|
257
|
+
return memoizedGetEnvelopedFactory(plugins);
|
|
258
|
+
},
|
|
259
|
+
createExecutor,
|
|
260
|
+
get execute() {
|
|
261
|
+
return createExecutor();
|
|
262
|
+
},
|
|
263
|
+
get subscribe() {
|
|
264
|
+
return createExecutor();
|
|
265
|
+
},
|
|
266
|
+
sdkRequesterFactory,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
exports.getMesh = getMesh;
|
|
270
|
+
function extractDataOrThrowErrors(result) {
|
|
271
|
+
if (result.errors) {
|
|
272
|
+
if (result.errors.length === 1) {
|
|
273
|
+
throw result.errors[0];
|
|
274
|
+
}
|
|
275
|
+
throw new utils_2.AggregateError(result.errors);
|
|
276
|
+
}
|
|
277
|
+
return result.data;
|
|
278
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getInContextSDK = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const utils_1 = require("@graphql-mesh/utils");
|
|
6
|
+
const batch_delegate_1 = require("@graphql-tools/batch-delegate");
|
|
7
|
+
const delegate_1 = require("@graphql-tools/delegate");
|
|
8
|
+
const utils_2 = require("@graphql-tools/utils");
|
|
9
|
+
const wrap_1 = require("@graphql-tools/wrap");
|
|
10
|
+
const constants_js_1 = require("./constants.js");
|
|
11
|
+
async function getInContextSDK(unifiedSchema, rawSources, logger, onDelegateHooks) {
|
|
12
|
+
const inContextSDK = {};
|
|
13
|
+
const sourceMap = unifiedSchema.extensions.sourceMap;
|
|
14
|
+
for (const rawSource of rawSources) {
|
|
15
|
+
const rawSourceLogger = logger.child(`${rawSource.name}`);
|
|
16
|
+
const rawSourceContext = {
|
|
17
|
+
rawSource,
|
|
18
|
+
[constants_js_1.MESH_API_CONTEXT_SYMBOL]: true,
|
|
19
|
+
};
|
|
20
|
+
// TODO: Somehow rawSource reference got lost in somewhere
|
|
21
|
+
let rawSourceSubSchemaConfig;
|
|
22
|
+
const stitchingInfo = unifiedSchema.extensions.stitchingInfo;
|
|
23
|
+
if (stitchingInfo) {
|
|
24
|
+
for (const [subschemaConfig, subschema] of stitchingInfo.subschemaMap) {
|
|
25
|
+
if (subschemaConfig.name === rawSource.name) {
|
|
26
|
+
rawSourceSubSchemaConfig = subschema;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
rawSourceSubSchemaConfig = rawSource;
|
|
33
|
+
}
|
|
34
|
+
// If there is a single source, there is no unifiedSchema
|
|
35
|
+
const transformedSchema = sourceMap.get(rawSource);
|
|
36
|
+
const rootTypes = {
|
|
37
|
+
query: transformedSchema.getQueryType(),
|
|
38
|
+
mutation: transformedSchema.getMutationType(),
|
|
39
|
+
subscription: transformedSchema.getSubscriptionType(),
|
|
40
|
+
};
|
|
41
|
+
rawSourceLogger.debug(`Generating In Context SDK`);
|
|
42
|
+
for (const operationType in rootTypes) {
|
|
43
|
+
const rootType = rootTypes[operationType];
|
|
44
|
+
if (rootType) {
|
|
45
|
+
rawSourceContext[rootType.name] = {};
|
|
46
|
+
const rootTypeFieldMap = rootType.getFields();
|
|
47
|
+
for (const fieldName in rootTypeFieldMap) {
|
|
48
|
+
const rootTypeField = rootTypeFieldMap[fieldName];
|
|
49
|
+
const inContextSdkLogger = rawSourceLogger.child(`InContextSDK.${rootType.name}.${fieldName}`);
|
|
50
|
+
const namedReturnType = (0, graphql_1.getNamedType)(rootTypeField.type);
|
|
51
|
+
const shouldHaveSelectionSet = !(0, graphql_1.isLeafType)(namedReturnType);
|
|
52
|
+
rawSourceContext[rootType.name][fieldName] = async ({ root, args, context, info = {
|
|
53
|
+
fieldName,
|
|
54
|
+
fieldNodes: [],
|
|
55
|
+
returnType: namedReturnType,
|
|
56
|
+
parentType: rootType,
|
|
57
|
+
path: {
|
|
58
|
+
typename: rootType.name,
|
|
59
|
+
key: fieldName,
|
|
60
|
+
prev: undefined,
|
|
61
|
+
},
|
|
62
|
+
schema: transformedSchema,
|
|
63
|
+
fragments: {},
|
|
64
|
+
rootValue: root,
|
|
65
|
+
operation: {
|
|
66
|
+
kind: graphql_1.Kind.OPERATION_DEFINITION,
|
|
67
|
+
operation: operationType,
|
|
68
|
+
selectionSet: {
|
|
69
|
+
kind: graphql_1.Kind.SELECTION_SET,
|
|
70
|
+
selections: [],
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
variableValues: {},
|
|
74
|
+
}, selectionSet, key, argsFromKeys, valuesFromResults, }) => {
|
|
75
|
+
inContextSdkLogger.debug(`Called with`, {
|
|
76
|
+
args,
|
|
77
|
+
key,
|
|
78
|
+
});
|
|
79
|
+
const commonDelegateOptions = {
|
|
80
|
+
schema: rawSourceSubSchemaConfig,
|
|
81
|
+
rootValue: root,
|
|
82
|
+
operation: operationType,
|
|
83
|
+
fieldName,
|
|
84
|
+
context,
|
|
85
|
+
transformedSchema,
|
|
86
|
+
info,
|
|
87
|
+
transforms: [],
|
|
88
|
+
};
|
|
89
|
+
// If there isn't an extraction of a value
|
|
90
|
+
if (typeof selectionSet !== 'function') {
|
|
91
|
+
commonDelegateOptions.returnType = rootTypeField.type;
|
|
92
|
+
}
|
|
93
|
+
if (shouldHaveSelectionSet) {
|
|
94
|
+
let selectionCount = 0;
|
|
95
|
+
for (const fieldNode of info.fieldNodes) {
|
|
96
|
+
if (fieldNode.selectionSet != null) {
|
|
97
|
+
selectionCount += fieldNode.selectionSet.selections.length;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (selectionCount === 0) {
|
|
101
|
+
if (!selectionSet) {
|
|
102
|
+
throw new Error(`You have to provide 'selectionSet' for context.${rawSource.name}.${rootType.name}.${fieldName}`);
|
|
103
|
+
}
|
|
104
|
+
commonDelegateOptions.info = {
|
|
105
|
+
...info,
|
|
106
|
+
fieldNodes: [
|
|
107
|
+
{
|
|
108
|
+
...info.fieldNodes[0],
|
|
109
|
+
selectionSet: {
|
|
110
|
+
kind: graphql_1.Kind.SELECTION_SET,
|
|
111
|
+
selections: [
|
|
112
|
+
{
|
|
113
|
+
kind: graphql_1.Kind.FIELD,
|
|
114
|
+
name: {
|
|
115
|
+
kind: graphql_1.Kind.NAME,
|
|
116
|
+
value: '__typename',
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
...info.fieldNodes.slice(1),
|
|
123
|
+
],
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (key && argsFromKeys) {
|
|
128
|
+
const batchDelegationOptions = {
|
|
129
|
+
...commonDelegateOptions,
|
|
130
|
+
key,
|
|
131
|
+
argsFromKeys,
|
|
132
|
+
valuesFromResults,
|
|
133
|
+
};
|
|
134
|
+
if (selectionSet) {
|
|
135
|
+
const selectionSetFactory = normalizeSelectionSetParamOrFactory(selectionSet);
|
|
136
|
+
const path = [fieldName];
|
|
137
|
+
const wrapQueryTransform = new wrap_1.WrapQuery(path, selectionSetFactory, identical);
|
|
138
|
+
batchDelegationOptions.transforms = [wrapQueryTransform];
|
|
139
|
+
}
|
|
140
|
+
const onDelegateHookDones = [];
|
|
141
|
+
for (const onDelegateHook of onDelegateHooks) {
|
|
142
|
+
const onDelegateDone = await onDelegateHook({
|
|
143
|
+
...batchDelegationOptions,
|
|
144
|
+
sourceName: rawSource.name,
|
|
145
|
+
typeName: rootType.name,
|
|
146
|
+
fieldName,
|
|
147
|
+
});
|
|
148
|
+
if (onDelegateDone) {
|
|
149
|
+
onDelegateHookDones.push(onDelegateDone);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
let result = await (0, batch_delegate_1.batchDelegateToSchema)(batchDelegationOptions);
|
|
153
|
+
for (const onDelegateHookDone of onDelegateHookDones) {
|
|
154
|
+
await onDelegateHookDone({
|
|
155
|
+
result,
|
|
156
|
+
setResult(newResult) {
|
|
157
|
+
result = newResult;
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const regularDelegateOptions = {
|
|
165
|
+
...commonDelegateOptions,
|
|
166
|
+
args,
|
|
167
|
+
};
|
|
168
|
+
if (selectionSet) {
|
|
169
|
+
const selectionSetFactory = normalizeSelectionSetParamOrFactory(selectionSet);
|
|
170
|
+
const path = [fieldName];
|
|
171
|
+
const wrapQueryTransform = new wrap_1.WrapQuery(path, selectionSetFactory, valuesFromResults || identical);
|
|
172
|
+
regularDelegateOptions.transforms = [wrapQueryTransform];
|
|
173
|
+
}
|
|
174
|
+
const onDelegateHookDones = [];
|
|
175
|
+
for (const onDelegateHook of onDelegateHooks) {
|
|
176
|
+
const onDelegateDone = await onDelegateHook({
|
|
177
|
+
...regularDelegateOptions,
|
|
178
|
+
sourceName: rawSource.name,
|
|
179
|
+
typeName: rootType.name,
|
|
180
|
+
fieldName,
|
|
181
|
+
});
|
|
182
|
+
if (onDelegateDone) {
|
|
183
|
+
onDelegateHookDones.push(onDelegateDone);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
let result = await (0, delegate_1.delegateToSchema)(regularDelegateOptions);
|
|
187
|
+
for (const onDelegateHookDone of onDelegateHookDones) {
|
|
188
|
+
await onDelegateHookDone({
|
|
189
|
+
result,
|
|
190
|
+
setResult(newResult) {
|
|
191
|
+
result = newResult;
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
inContextSDK[rawSource.name] = rawSourceContext;
|
|
202
|
+
}
|
|
203
|
+
return inContextSDK;
|
|
204
|
+
}
|
|
205
|
+
exports.getInContextSDK = getInContextSDK;
|
|
206
|
+
function getSelectionSetFromDocumentNode(documentNode) {
|
|
207
|
+
const operationDefinition = documentNode.definitions.find(definition => definition.kind === graphql_1.Kind.OPERATION_DEFINITION);
|
|
208
|
+
if (!operationDefinition) {
|
|
209
|
+
throw new Error('DocumentNode must contain an OperationDefinitionNode');
|
|
210
|
+
}
|
|
211
|
+
return operationDefinition.selectionSet;
|
|
212
|
+
}
|
|
213
|
+
function normalizeSelectionSetParam(selectionSetParam) {
|
|
214
|
+
if (typeof selectionSetParam === 'string') {
|
|
215
|
+
const documentNode = (0, utils_1.parseWithCache)(selectionSetParam);
|
|
216
|
+
return getSelectionSetFromDocumentNode(documentNode);
|
|
217
|
+
}
|
|
218
|
+
if ((0, utils_2.isDocumentNode)(selectionSetParam)) {
|
|
219
|
+
return getSelectionSetFromDocumentNode(selectionSetParam);
|
|
220
|
+
}
|
|
221
|
+
return selectionSetParam;
|
|
222
|
+
}
|
|
223
|
+
const normalizeSelectionSetParamFactory = (0, utils_2.memoize1)(function normalizeSelectionSetParamFactory(selectionSetParamFactory) {
|
|
224
|
+
const memoizedSelectionSetFactory = (0, utils_2.memoize1)(selectionSetParamFactory);
|
|
225
|
+
return function selectionSetFactory(subtree) {
|
|
226
|
+
const selectionSetParam = memoizedSelectionSetFactory(subtree);
|
|
227
|
+
return normalizeSelectionSetParam(selectionSetParam);
|
|
228
|
+
};
|
|
229
|
+
});
|
|
230
|
+
function normalizeSelectionSetParamOrFactory(selectionSetParamOrFactory) {
|
|
231
|
+
if (typeof selectionSetParamOrFactory === 'function') {
|
|
232
|
+
return normalizeSelectionSetParamFactory(selectionSetParamOrFactory);
|
|
233
|
+
}
|
|
234
|
+
return () => normalizeSelectionSetParam(selectionSetParamOrFactory);
|
|
235
|
+
}
|
|
236
|
+
function identical(val) {
|
|
237
|
+
return val;
|
|
238
|
+
}
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./get-mesh.js"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./types.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./useSubschema.js"), exports);
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/types.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSubschema = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const core_1 = require("@envelop/core");
|
|
6
|
+
const utils_1 = require("@graphql-mesh/utils");
|
|
7
|
+
const batch_execute_1 = require("@graphql-tools/batch-execute");
|
|
8
|
+
const delegate_1 = require("@graphql-tools/delegate");
|
|
9
|
+
const utils_2 = require("@graphql-tools/utils");
|
|
10
|
+
var IntrospectionQueryType;
|
|
11
|
+
(function (IntrospectionQueryType) {
|
|
12
|
+
IntrospectionQueryType["FEDERATION"] = "FEDERATION";
|
|
13
|
+
IntrospectionQueryType["REGULAR"] = "REGULAR";
|
|
14
|
+
})(IntrospectionQueryType || (IntrospectionQueryType = {}));
|
|
15
|
+
function getIntrospectionOperationType(operationAST) {
|
|
16
|
+
let introspectionQueryType = null;
|
|
17
|
+
(0, graphql_1.visit)(operationAST, {
|
|
18
|
+
Field: node => {
|
|
19
|
+
if (node.name.value === '__schema' || node.name.value === '__type') {
|
|
20
|
+
introspectionQueryType = IntrospectionQueryType.REGULAR;
|
|
21
|
+
return graphql_1.BREAK;
|
|
22
|
+
}
|
|
23
|
+
if (node.name.value === '_service') {
|
|
24
|
+
introspectionQueryType = IntrospectionQueryType.FEDERATION;
|
|
25
|
+
return graphql_1.BREAK;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
return introspectionQueryType;
|
|
30
|
+
}
|
|
31
|
+
function getExecuteFn(subschema) {
|
|
32
|
+
return async function subschemaExecute(args) {
|
|
33
|
+
var _a;
|
|
34
|
+
const originalRequest = {
|
|
35
|
+
document: args.document,
|
|
36
|
+
variables: args.variableValues,
|
|
37
|
+
operationName: (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined,
|
|
38
|
+
rootValue: args.rootValue,
|
|
39
|
+
context: args.contextValue,
|
|
40
|
+
};
|
|
41
|
+
const operationAST = (0, utils_2.getOperationASTFromRequest)(originalRequest);
|
|
42
|
+
// TODO: We need more elegant solution
|
|
43
|
+
const introspectionQueryType = getIntrospectionOperationType(operationAST);
|
|
44
|
+
if (introspectionQueryType === IntrospectionQueryType.FEDERATION) {
|
|
45
|
+
const executionResult = {
|
|
46
|
+
data: {
|
|
47
|
+
_service: {
|
|
48
|
+
sdl: (0, utils_2.printSchemaWithDirectives)(args.schema),
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
return executionResult;
|
|
53
|
+
}
|
|
54
|
+
else if (introspectionQueryType === IntrospectionQueryType.REGULAR) {
|
|
55
|
+
return (0, graphql_1.execute)(args);
|
|
56
|
+
}
|
|
57
|
+
const delegationContext = {
|
|
58
|
+
subschema,
|
|
59
|
+
subschemaConfig: subschema,
|
|
60
|
+
targetSchema: args.schema,
|
|
61
|
+
operation: operationAST.operation,
|
|
62
|
+
fieldName: '',
|
|
63
|
+
context: args.contextValue,
|
|
64
|
+
rootValue: args.rootValue,
|
|
65
|
+
transforms: subschema.transforms,
|
|
66
|
+
transformedSchema: subschema.transformedSchema,
|
|
67
|
+
skipTypeMerging: true,
|
|
68
|
+
returnType: (0, utils_2.getDefinedRootType)(args.schema, operationAST.operation),
|
|
69
|
+
};
|
|
70
|
+
let executor = subschema.executor;
|
|
71
|
+
if (executor == null) {
|
|
72
|
+
executor = (0, delegate_1.createDefaultExecutor)(subschema.schema);
|
|
73
|
+
}
|
|
74
|
+
if (subschema.batch) {
|
|
75
|
+
executor = (0, batch_execute_1.createBatchingExecutor)(executor);
|
|
76
|
+
}
|
|
77
|
+
const transformationContext = {};
|
|
78
|
+
const transformedRequest = (0, utils_1.applyRequestTransforms)(originalRequest, delegationContext, transformationContext, subschema.transforms);
|
|
79
|
+
const originalResult = await executor(transformedRequest);
|
|
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
|
+
}
|
|
87
|
+
// Creates an envelop plugin to execute a subschema inside Envelop
|
|
88
|
+
function useSubschema(subschema) {
|
|
89
|
+
const executeFn = getExecuteFn(subschema);
|
|
90
|
+
const plugin = {
|
|
91
|
+
onPluginInit({ setSchema }) {
|
|
92
|
+
// To prevent unwanted warnings from stitching
|
|
93
|
+
if (!('_transformedSchema' in subschema)) {
|
|
94
|
+
subschema.transformedSchema = (0, delegate_1.applySchemaTransforms)(subschema.schema, subschema);
|
|
95
|
+
}
|
|
96
|
+
subschema.transformedSchema.extensions =
|
|
97
|
+
subschema.transformedSchema.extensions || subschema.schema.extensions || {};
|
|
98
|
+
Object.assign(subschema.transformedSchema.extensions, subschema.schema.extensions);
|
|
99
|
+
setSchema(subschema.transformedSchema);
|
|
100
|
+
},
|
|
101
|
+
onExecute({ setExecuteFn }) {
|
|
102
|
+
setExecuteFn(executeFn);
|
|
103
|
+
},
|
|
104
|
+
onSubscribe({ setSubscribeFn }) {
|
|
105
|
+
setSubscribeFn(executeFn);
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
return plugin;
|
|
109
|
+
}
|
|
110
|
+
exports.useSubschema = useSubschema;
|
package/esm/constants.js
ADDED