@graphql-mesh/config 2.0.0 → 2.0.1-alpha-b8d465bad.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.
@@ -0,0 +1,2 @@
1
+ import { DocumentNode } from 'graphql';
2
+ export declare function getAdditionalResolversFromTypeDefs(additionalTypeDefs: DocumentNode[]): (string | import("../../types/src/config").AdditionalStitchingResolverObject | import("../../types/src/config").AdditionalStitchingBatchResolverObject | import("../../types/src/config").AdditionalSubscriptionObject)[];
package/index.js CHANGED
@@ -14,6 +14,8 @@ const store = require('@graphql-mesh/store');
14
14
  const pascalCase = require('pascal-case');
15
15
  const camelCase = require('camel-case');
16
16
  const core = require('@envelop/core');
17
+ const crossUndiciFetch = require('cross-undici-fetch');
18
+ const fetchache = require('fetchache');
17
19
 
18
20
  async function getPackage({ name, type, importFn, cwd, additionalPrefixes = [], }) {
19
21
  const casedName = paramCase.paramCase(name);
@@ -165,6 +167,73 @@ async function resolveLogger(loggerConfig, importFn, cwd, additionalPackagePrefi
165
167
  };
166
168
  }
167
169
 
170
+ function parseObject(ast) {
171
+ const value = Object.create(null);
172
+ ast.fields.forEach(field => {
173
+ // eslint-disable-next-line no-use-before-define
174
+ value[field.name.value] = parseLiteral(field.value);
175
+ });
176
+ return value;
177
+ }
178
+ function parseLiteral(ast) {
179
+ switch (ast.kind) {
180
+ case graphql.Kind.STRING:
181
+ case graphql.Kind.BOOLEAN:
182
+ return ast.value;
183
+ case graphql.Kind.INT:
184
+ case graphql.Kind.FLOAT:
185
+ return parseFloat(ast.value);
186
+ case graphql.Kind.OBJECT:
187
+ return parseObject(ast);
188
+ case graphql.Kind.LIST:
189
+ return ast.values.map(n => parseLiteral(n));
190
+ case graphql.Kind.NULL:
191
+ return null;
192
+ }
193
+ }
194
+ function getAdditionalResolversFromTypeDefs(additionalTypeDefs) {
195
+ const additionalResolversFromTypeDefs = [];
196
+ function handleFieldNode(targetTypeName, fieldNode) {
197
+ var _a;
198
+ if ((_a = fieldNode.directives) === null || _a === void 0 ? void 0 : _a.length) {
199
+ const resolveToDef = fieldNode.directives.find(d => d.name.value === 'resolveTo');
200
+ if (resolveToDef != null) {
201
+ const resolveToArgumentMap = {};
202
+ for (const resolveToArg of resolveToDef.arguments) {
203
+ const resolveToArgName = resolveToArg.name.value;
204
+ resolveToArgumentMap[resolveToArgName] = parseLiteral(resolveToArg.value);
205
+ }
206
+ additionalResolversFromTypeDefs.push({
207
+ targetTypeName,
208
+ targetFieldName: fieldNode.name.value,
209
+ ...resolveToArgumentMap,
210
+ });
211
+ }
212
+ }
213
+ }
214
+ additionalTypeDefs === null || additionalTypeDefs === void 0 ? void 0 : additionalTypeDefs.forEach(typeDefs => {
215
+ graphql.visit(typeDefs, {
216
+ ObjectTypeDefinition(objectNode) {
217
+ var _a;
218
+ (_a = objectNode.fields) === null || _a === void 0 ? void 0 : _a.forEach(fieldNode => handleFieldNode(objectNode.name.value, fieldNode));
219
+ },
220
+ ObjectTypeExtension(objectNode) {
221
+ var _a;
222
+ (_a = objectNode.fields) === null || _a === void 0 ? void 0 : _a.forEach(fieldNode => handleFieldNode(objectNode.name.value, fieldNode));
223
+ },
224
+ InterfaceTypeDefinition(interfaceNode) {
225
+ var _a;
226
+ (_a = interfaceNode.fields) === null || _a === void 0 ? void 0 : _a.forEach(fieldNode => handleFieldNode(interfaceNode.name.value, fieldNode));
227
+ },
228
+ InterfaceTypeExtension(interfaceNode) {
229
+ var _a;
230
+ (_a = interfaceNode.fields) === null || _a === void 0 ? void 0 : _a.forEach(fieldNode => handleFieldNode(interfaceNode.name.value, fieldNode));
231
+ },
232
+ });
233
+ });
234
+ return additionalResolversFromTypeDefs;
235
+ }
236
+
168
237
  const ENVELOP_CORE_PLUGINS_MAP = {
169
238
  maskedErrors: {
170
239
  moduleName: '@envelop/core',
@@ -226,6 +295,22 @@ async function processConfig(config, options) {
226
295
  const { cache, importCode: cacheImportCode, code: cacheCode, } = await resolveCache(config.cache, importFn, rootStore, dir, pubsub, additionalPackagePrefixes);
227
296
  importCodes.push(cacheImportCode);
228
297
  codes.push(cacheCode);
298
+ if (config.customFetch) {
299
+ importCodes.push(`import fetchFn from ${JSON.stringify(config.customFetch)}`);
300
+ }
301
+ else {
302
+ importCodes.push(`import { fetchFactory } from 'fetchache';`);
303
+ importCodes.push(`import { fetch, Request, Response } from 'cross-undici-fetch';`);
304
+ codes.push('const fetchFn = fetchFactory({ cache, fetch, Request, Response });');
305
+ }
306
+ const fetchFn = config.customFetch
307
+ ? await importFn(config.customFetch, true)
308
+ : fetchache.fetchFactory({
309
+ cache,
310
+ fetch: crossUndiciFetch.fetch,
311
+ Request: crossUndiciFetch.Request,
312
+ Response: crossUndiciFetch.Response,
313
+ });
229
314
  const sourcesStore = rootStore.child('sources');
230
315
  codes.push(`const sourcesStore = rootStore.child('sources');`);
231
316
  const { logger, importCode: loggerImportCode, code: loggerCode, } = await resolveLogger(config.logger, importFn, dir, additionalPackagePrefixes, options === null || options === void 0 ? void 0 : options.initialLoggerPrefix);
@@ -261,7 +346,8 @@ async function processConfig(config, options) {
261
346
  pubsub,
262
347
  store: sourcesStore.child(${JSON.stringify(source.name)}),
263
348
  logger: logger.child(${JSON.stringify(source.name)}),
264
- importFn
349
+ importFn,
350
+ fetchFn,
265
351
  });`);
266
352
  }
267
353
  return new HandlerCtor({
@@ -273,6 +359,7 @@ async function processConfig(config, options) {
273
359
  store: sourcesStore.child(source.name),
274
360
  logger: logger.child(source.name),
275
361
  importFn,
362
+ fetchFn,
276
363
  });
277
364
  }),
278
365
  Promise.all((source.transforms || []).map(async (t, transformIndex) => {
@@ -461,6 +548,43 @@ async function processConfig(config, options) {
461
548
  codes.push(`const additionalResolvers = [] as any[]`);
462
549
  }
463
550
  }
551
+ if (additionalTypeDefs === null || additionalTypeDefs === void 0 ? void 0 : additionalTypeDefs.length) {
552
+ const additionalResolversConfigFromTypeDefs = getAdditionalResolversFromTypeDefs(additionalTypeDefs);
553
+ if (additionalResolversConfigFromTypeDefs === null || additionalResolversConfigFromTypeDefs === void 0 ? void 0 : additionalResolversConfigFromTypeDefs.length) {
554
+ const resolveToDirectiveDefinition = /* GraphQL */ `
555
+ scalar ResolveToSourceArgs
556
+ directive @resolveTo(
557
+ requiredSelectionSet: String
558
+ sourceName: String!
559
+ sourceTypeName: String!
560
+ sourceFieldName: String!
561
+ sourceSelectionSet: String
562
+ sourceArgs: ResolveToSourceArgs
563
+ keyField: String
564
+ keysArg: String
565
+ pubsubTopic: String
566
+ filterBy: String
567
+ additionalArgs: ResolveToSourceArgs
568
+ result: String
569
+ resultType: String
570
+ ) on FIELD_DEFINITION
571
+ `;
572
+ const resolvedAdditionalResolvers = await utils$1.resolveAdditionalResolvers(dir, additionalResolversConfigFromTypeDefs, importFn, pubsub);
573
+ additionalTypeDefs.unshift(graphql.parse(resolveToDirectiveDefinition));
574
+ additionalResolvers.push(...resolvedAdditionalResolvers);
575
+ if (options.generateCode && resolvedAdditionalResolvers.length) {
576
+ importCodes.push(`import { resolveAdditionalResolvers } from '@graphql-mesh/utils';`);
577
+ codes.push(`additionalTypeDefs.unshift(parse(/* GraphQL */\`${resolveToDirectiveDefinition}\`))`);
578
+ codes.push(`const additionalResolversFromTypeDefs = await resolveAdditionalResolvers(
579
+ baseDir,
580
+ ${JSON.stringify(additionalResolversConfigFromTypeDefs)},
581
+ importFn,
582
+ pubsub
583
+ );`);
584
+ codes.push(`additionalResolvers.push(additionalResolversFromTypeDefs)`);
585
+ }
586
+ }
587
+ }
464
588
  if (config.additionalEnvelopPlugins) {
465
589
  codes.push(`const importedAdditionalEnvelopPlugins = await import(${JSON.stringify(crossHelpers.path.join('..', config.additionalEnvelopPlugins).split('\\').join('/'))}).then(m => m.default || m);`);
466
590
  const importedAdditionalEnvelopPlugins = await importFn(crossHelpers.path.isAbsolute(config.additionalEnvelopPlugins)
package/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { path, process } from '@graphql-mesh/cross-helpers';
2
- import { print, concatAST, visit } from 'graphql';
2
+ import { visit, Kind, print, parse, concatAST } from 'graphql';
3
3
  import { printSchemaWithDirectives } from '@graphql-tools/utils';
4
4
  import { paramCase } from 'param-case';
5
5
  import { loadTypedefs, loadDocuments } from '@graphql-tools/load';
@@ -10,6 +10,8 @@ import { MeshStore, FsStoreStorageAdapter, InMemoryStoreStorageAdapter } from '@
10
10
  import { pascalCase } from 'pascal-case';
11
11
  import { camelCase } from 'camel-case';
12
12
  import { useMaskedErrors, useImmediateIntrospection } from '@envelop/core';
13
+ import { fetch, Request, Response } from 'cross-undici-fetch';
14
+ import { fetchFactory } from 'fetchache';
13
15
 
14
16
  async function getPackage({ name, type, importFn, cwd, additionalPrefixes = [], }) {
15
17
  const casedName = paramCase(name);
@@ -161,6 +163,73 @@ async function resolveLogger(loggerConfig, importFn, cwd, additionalPackagePrefi
161
163
  };
162
164
  }
163
165
 
166
+ function parseObject(ast) {
167
+ const value = Object.create(null);
168
+ ast.fields.forEach(field => {
169
+ // eslint-disable-next-line no-use-before-define
170
+ value[field.name.value] = parseLiteral(field.value);
171
+ });
172
+ return value;
173
+ }
174
+ function parseLiteral(ast) {
175
+ switch (ast.kind) {
176
+ case Kind.STRING:
177
+ case Kind.BOOLEAN:
178
+ return ast.value;
179
+ case Kind.INT:
180
+ case Kind.FLOAT:
181
+ return parseFloat(ast.value);
182
+ case Kind.OBJECT:
183
+ return parseObject(ast);
184
+ case Kind.LIST:
185
+ return ast.values.map(n => parseLiteral(n));
186
+ case Kind.NULL:
187
+ return null;
188
+ }
189
+ }
190
+ function getAdditionalResolversFromTypeDefs(additionalTypeDefs) {
191
+ const additionalResolversFromTypeDefs = [];
192
+ function handleFieldNode(targetTypeName, fieldNode) {
193
+ var _a;
194
+ if ((_a = fieldNode.directives) === null || _a === void 0 ? void 0 : _a.length) {
195
+ const resolveToDef = fieldNode.directives.find(d => d.name.value === 'resolveTo');
196
+ if (resolveToDef != null) {
197
+ const resolveToArgumentMap = {};
198
+ for (const resolveToArg of resolveToDef.arguments) {
199
+ const resolveToArgName = resolveToArg.name.value;
200
+ resolveToArgumentMap[resolveToArgName] = parseLiteral(resolveToArg.value);
201
+ }
202
+ additionalResolversFromTypeDefs.push({
203
+ targetTypeName,
204
+ targetFieldName: fieldNode.name.value,
205
+ ...resolveToArgumentMap,
206
+ });
207
+ }
208
+ }
209
+ }
210
+ additionalTypeDefs === null || additionalTypeDefs === void 0 ? void 0 : additionalTypeDefs.forEach(typeDefs => {
211
+ visit(typeDefs, {
212
+ ObjectTypeDefinition(objectNode) {
213
+ var _a;
214
+ (_a = objectNode.fields) === null || _a === void 0 ? void 0 : _a.forEach(fieldNode => handleFieldNode(objectNode.name.value, fieldNode));
215
+ },
216
+ ObjectTypeExtension(objectNode) {
217
+ var _a;
218
+ (_a = objectNode.fields) === null || _a === void 0 ? void 0 : _a.forEach(fieldNode => handleFieldNode(objectNode.name.value, fieldNode));
219
+ },
220
+ InterfaceTypeDefinition(interfaceNode) {
221
+ var _a;
222
+ (_a = interfaceNode.fields) === null || _a === void 0 ? void 0 : _a.forEach(fieldNode => handleFieldNode(interfaceNode.name.value, fieldNode));
223
+ },
224
+ InterfaceTypeExtension(interfaceNode) {
225
+ var _a;
226
+ (_a = interfaceNode.fields) === null || _a === void 0 ? void 0 : _a.forEach(fieldNode => handleFieldNode(interfaceNode.name.value, fieldNode));
227
+ },
228
+ });
229
+ });
230
+ return additionalResolversFromTypeDefs;
231
+ }
232
+
164
233
  const ENVELOP_CORE_PLUGINS_MAP = {
165
234
  maskedErrors: {
166
235
  moduleName: '@envelop/core',
@@ -222,6 +291,22 @@ async function processConfig(config, options) {
222
291
  const { cache, importCode: cacheImportCode, code: cacheCode, } = await resolveCache(config.cache, importFn, rootStore, dir, pubsub, additionalPackagePrefixes);
223
292
  importCodes.push(cacheImportCode);
224
293
  codes.push(cacheCode);
294
+ if (config.customFetch) {
295
+ importCodes.push(`import fetchFn from ${JSON.stringify(config.customFetch)}`);
296
+ }
297
+ else {
298
+ importCodes.push(`import { fetchFactory } from 'fetchache';`);
299
+ importCodes.push(`import { fetch, Request, Response } from 'cross-undici-fetch';`);
300
+ codes.push('const fetchFn = fetchFactory({ cache, fetch, Request, Response });');
301
+ }
302
+ const fetchFn = config.customFetch
303
+ ? await importFn(config.customFetch, true)
304
+ : fetchFactory({
305
+ cache,
306
+ fetch,
307
+ Request,
308
+ Response,
309
+ });
225
310
  const sourcesStore = rootStore.child('sources');
226
311
  codes.push(`const sourcesStore = rootStore.child('sources');`);
227
312
  const { logger, importCode: loggerImportCode, code: loggerCode, } = await resolveLogger(config.logger, importFn, dir, additionalPackagePrefixes, options === null || options === void 0 ? void 0 : options.initialLoggerPrefix);
@@ -257,7 +342,8 @@ async function processConfig(config, options) {
257
342
  pubsub,
258
343
  store: sourcesStore.child(${JSON.stringify(source.name)}),
259
344
  logger: logger.child(${JSON.stringify(source.name)}),
260
- importFn
345
+ importFn,
346
+ fetchFn,
261
347
  });`);
262
348
  }
263
349
  return new HandlerCtor({
@@ -269,6 +355,7 @@ async function processConfig(config, options) {
269
355
  store: sourcesStore.child(source.name),
270
356
  logger: logger.child(source.name),
271
357
  importFn,
358
+ fetchFn,
272
359
  });
273
360
  }),
274
361
  Promise.all((source.transforms || []).map(async (t, transformIndex) => {
@@ -457,6 +544,43 @@ async function processConfig(config, options) {
457
544
  codes.push(`const additionalResolvers = [] as any[]`);
458
545
  }
459
546
  }
547
+ if (additionalTypeDefs === null || additionalTypeDefs === void 0 ? void 0 : additionalTypeDefs.length) {
548
+ const additionalResolversConfigFromTypeDefs = getAdditionalResolversFromTypeDefs(additionalTypeDefs);
549
+ if (additionalResolversConfigFromTypeDefs === null || additionalResolversConfigFromTypeDefs === void 0 ? void 0 : additionalResolversConfigFromTypeDefs.length) {
550
+ const resolveToDirectiveDefinition = /* GraphQL */ `
551
+ scalar ResolveToSourceArgs
552
+ directive @resolveTo(
553
+ requiredSelectionSet: String
554
+ sourceName: String!
555
+ sourceTypeName: String!
556
+ sourceFieldName: String!
557
+ sourceSelectionSet: String
558
+ sourceArgs: ResolveToSourceArgs
559
+ keyField: String
560
+ keysArg: String
561
+ pubsubTopic: String
562
+ filterBy: String
563
+ additionalArgs: ResolveToSourceArgs
564
+ result: String
565
+ resultType: String
566
+ ) on FIELD_DEFINITION
567
+ `;
568
+ const resolvedAdditionalResolvers = await resolveAdditionalResolvers(dir, additionalResolversConfigFromTypeDefs, importFn, pubsub);
569
+ additionalTypeDefs.unshift(parse(resolveToDirectiveDefinition));
570
+ additionalResolvers.push(...resolvedAdditionalResolvers);
571
+ if (options.generateCode && resolvedAdditionalResolvers.length) {
572
+ importCodes.push(`import { resolveAdditionalResolvers } from '@graphql-mesh/utils';`);
573
+ codes.push(`additionalTypeDefs.unshift(parse(/* GraphQL */\`${resolveToDirectiveDefinition}\`))`);
574
+ codes.push(`const additionalResolversFromTypeDefs = await resolveAdditionalResolvers(
575
+ baseDir,
576
+ ${JSON.stringify(additionalResolversConfigFromTypeDefs)},
577
+ importFn,
578
+ pubsub
579
+ );`);
580
+ codes.push(`additionalResolvers.push(additionalResolversFromTypeDefs)`);
581
+ }
582
+ }
583
+ }
460
584
  if (config.additionalEnvelopPlugins) {
461
585
  codes.push(`const importedAdditionalEnvelopPlugins = await import(${JSON.stringify(path.join('..', config.additionalEnvelopPlugins).split('\\').join('/'))}).then(m => m.default || m);`);
462
586
  const importedAdditionalEnvelopPlugins = await importFn(path.isAbsolute(config.additionalEnvelopPlugins)
package/package.json CHANGED
@@ -1,25 +1,27 @@
1
1
  {
2
2
  "name": "@graphql-mesh/config",
3
- "version": "2.0.0",
3
+ "version": "2.0.1-alpha-b8d465bad.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@graphql-mesh/runtime": "^0.38.0",
6
+ "@graphql-mesh/runtime": "0.38.1-alpha-b8d465bad.0",
7
7
  "graphql": "*"
8
8
  },
9
9
  "dependencies": {
10
10
  "@envelop/core": "2.3.3",
11
- "@graphql-mesh/cache-localforage": "0.6.13",
11
+ "@graphql-mesh/cache-localforage": "0.6.14-alpha-b8d465bad.0",
12
12
  "@graphql-mesh/cross-helpers": "0.1.6",
13
- "@graphql-mesh/merger-bare": "0.13.55",
14
- "@graphql-mesh/merger-stitching": "0.15.57",
15
- "@graphql-mesh/store": "0.8.16",
16
- "@graphql-mesh/types": "0.75.0",
17
- "@graphql-mesh/utils": "0.35.7",
13
+ "@graphql-mesh/merger-bare": "0.13.56-alpha-b8d465bad.0",
14
+ "@graphql-mesh/merger-stitching": "0.15.58-alpha-b8d465bad.0",
15
+ "@graphql-mesh/store": "0.8.17-alpha-b8d465bad.0",
16
+ "@graphql-mesh/types": "0.75.1-alpha-b8d465bad.0",
17
+ "@graphql-mesh/utils": "0.35.8-alpha-b8d465bad.0",
18
18
  "@graphql-tools/code-file-loader": "7.2.18",
19
19
  "@graphql-tools/graphql-file-loader": "7.3.15",
20
20
  "@graphql-tools/load": "7.5.14",
21
21
  "@graphql-tools/utils": "8.6.13",
22
22
  "camel-case": "4.1.2",
23
+ "cross-undici-fetch": "0.4.8",
24
+ "fetchache": "0.1.2",
23
25
  "param-case": "3.0.4",
24
26
  "pascal-case": "3.1.2",
25
27
  "tslib": "^2.4.0"