@graphql-tools/stitch 10.2.0 → 10.2.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @graphql-tools/stitch
2
2
 
3
+ ## 10.2.1
4
+ ### Patch Changes
5
+
6
+
7
+
8
+ - [#2484](https://github.com/graphql-hive/gateway/pull/2484) [`a589a2d`](https://github.com/graphql-hive/gateway/commit/a589a2d9797e67623b28439d0bd30a17e3a247d6) Thanks [@jdolle](https://github.com/jdolle)! - Reduce peak memory consumption for `stitchSchemas` function
9
+
3
10
  ## 10.2.0
4
11
  ### Minor Changes
5
12
 
package/dist/index.cjs CHANGED
@@ -3220,80 +3220,46 @@ function onTypeConflictToCandidateSelector(onTypeConflict) {
3220
3220
  });
3221
3221
  }
3222
3222
 
3223
- function stitchSchemas({
3224
- subschemas = [],
3225
- types = [],
3226
- typeDefs = [],
3227
- onTypeConflict,
3228
- mergeTypes = true,
3229
- typeMergingOptions,
3230
- subschemaConfigTransforms = [],
3231
- resolvers = {},
3232
- inheritResolversFromInterfaces = false,
3233
- resolverValidationOptions = {},
3234
- updateResolversInPlace = true,
3235
- schemaExtensions,
3236
- ...rest
3237
- }) {
3238
- const mergeDirectives = rest.mergeDirectives ?? true;
3239
- const transformedSubschemas = [];
3240
- const subschemaMap = /* @__PURE__ */ new Map();
3241
- const originalSubschemaMap = /* @__PURE__ */ new Map();
3242
- for (const subschema of subschemas) {
3243
- for (const transformedSubschemaConfig of applySubschemaConfigTransforms(
3244
- subschemaConfigTransforms,
3245
- subschema,
3246
- subschemaMap,
3247
- originalSubschemaMap
3248
- )) {
3249
- transformedSubschemas.push(transformedSubschemaConfig);
3250
- }
3251
- }
3252
- const directiveMap = /* @__PURE__ */ Object.create(null);
3253
- for (const directive of graphql.specifiedDirectives) {
3254
- directiveMap[directive.name] = directive;
3255
- }
3256
- const schemaDefs = /* @__PURE__ */ Object.create(null);
3257
- const [typeCandidates, rootTypeNameMap, extensions] = buildTypeCandidates({
3258
- subschemas: transformedSubschemas,
3259
- originalSubschemaMap,
3260
- types,
3261
- typeDefs: typeDefs || [],
3262
- parseOptions: rest,
3263
- directiveMap,
3264
- schemaDefs,
3265
- mergeDirectives
3266
- });
3267
- let stitchingInfo = createStitchingInfo(
3268
- subschemaMap,
3269
- typeCandidates,
3270
- mergeTypes
3271
- );
3272
- const { typeMap: newTypeMap, directives: newDirectives } = buildTypes({
3223
+ function stitchSchemas(options) {
3224
+ const {
3225
+ resolvers = {},
3226
+ inheritResolversFromInterfaces = false,
3227
+ resolverValidationOptions = {},
3228
+ updateResolversInPlace = true,
3229
+ schemaExtensions,
3230
+ ...rest
3231
+ } = options;
3232
+ let {
3273
3233
  typeCandidates,
3274
- directives: Object.values(directiveMap),
3234
+ newTypeMap,
3235
+ newDirectives,
3236
+ rootTypeNameMap,
3275
3237
  stitchingInfo,
3276
- rootTypeNames: Object.values(rootTypeNameMap),
3277
- onTypeConflict,
3278
- mergeTypes,
3279
- typeMergingOptions
3280
- });
3281
- if (!mergeDirectives) {
3282
- stripCustomDirectiveUsages(Object.values(newTypeMap));
3283
- }
3284
- let schema$1 = new graphql.GraphQLSchema({
3285
- query: newTypeMap[rootTypeNameMap.query],
3286
- mutation: newTypeMap[rootTypeNameMap.mutation],
3287
- subscription: newTypeMap[rootTypeNameMap.subscription],
3288
- types: Object.values(newTypeMap),
3289
- directives: newDirectives,
3290
- astNode: schemaDefs.schemaDef,
3291
- extensionASTNodes: schemaDefs.schemaExtensions,
3292
- extensions: null,
3293
- assumeValid: rest.assumeValid
3294
- });
3295
- for (const extension of extensions) {
3296
- schema$1 = graphql.extendSchema(schema$1, extension, {
3238
+ schemaDefs,
3239
+ extensions,
3240
+ mergeDirectives
3241
+ } = prepareStitchingData(options);
3242
+ let schema$1;
3243
+ {
3244
+ const finalTypes = Object.values(newTypeMap);
3245
+ if (!mergeDirectives) {
3246
+ stripCustomDirectiveUsages(finalTypes);
3247
+ }
3248
+ schema$1 = new graphql.GraphQLSchema({
3249
+ query: newTypeMap[rootTypeNameMap.query],
3250
+ mutation: newTypeMap[rootTypeNameMap.mutation],
3251
+ subscription: newTypeMap[rootTypeNameMap.subscription],
3252
+ types: finalTypes,
3253
+ directives: newDirectives,
3254
+ astNode: schemaDefs.schemaDef,
3255
+ extensionASTNodes: schemaDefs.schemaExtensions,
3256
+ extensions: null,
3257
+ assumeValid: rest.assumeValid
3258
+ });
3259
+ }
3260
+ if (extensions.length > 0) {
3261
+ const mergedExtensions = graphql.concatAST(extensions);
3262
+ schema$1 = graphql.extendSchema(schema$1, mergedExtensions, {
3297
3263
  commentDescriptions: true
3298
3264
  });
3299
3265
  }
@@ -3317,10 +3283,8 @@ function stitchSchemas({
3317
3283
  }
3318
3284
  addStitchingInfo(schema$1, stitchingInfo);
3319
3285
  if (schemaExtensions) {
3320
- if (Array.isArray(schemaExtensions)) {
3321
- schemaExtensions = merge.mergeExtensions(schemaExtensions);
3322
- }
3323
- merge.applyExtensions(schema$1, schemaExtensions);
3286
+ const mergedSchemaExtensions = Array.isArray(schemaExtensions) ? merge.mergeExtensions(schemaExtensions) : schemaExtensions;
3287
+ merge.applyExtensions(schema$1, mergedSchemaExtensions);
3324
3288
  }
3325
3289
  return schema$1;
3326
3290
  }
@@ -3479,6 +3443,71 @@ function applySubschemaConfigTransforms(subschemaConfigTransforms, subschemaOrSu
3479
3443
  }
3480
3444
  return transformedSubschemas;
3481
3445
  }
3446
+ function prepareStitchingData(options) {
3447
+ const {
3448
+ subschemas = [],
3449
+ types = [],
3450
+ typeDefs = [],
3451
+ onTypeConflict,
3452
+ mergeTypes = true,
3453
+ typeMergingOptions,
3454
+ subschemaConfigTransforms = [],
3455
+ ...rest
3456
+ } = options;
3457
+ const mergeDirectives = rest.mergeDirectives ?? true;
3458
+ const transformedSubschemas = [];
3459
+ const subschemaMap = /* @__PURE__ */ new Map();
3460
+ const originalSubschemaMap = /* @__PURE__ */ new Map();
3461
+ for (const subschema of subschemas) {
3462
+ for (const transformedSubschemaConfig of applySubschemaConfigTransforms(
3463
+ subschemaConfigTransforms,
3464
+ subschema,
3465
+ subschemaMap,
3466
+ originalSubschemaMap
3467
+ )) {
3468
+ transformedSubschemas.push(transformedSubschemaConfig);
3469
+ }
3470
+ }
3471
+ const directiveMap = /* @__PURE__ */ Object.create(null);
3472
+ for (const directive of graphql.specifiedDirectives) {
3473
+ directiveMap[directive.name] = directive;
3474
+ }
3475
+ const schemaDefs = /* @__PURE__ */ Object.create(null);
3476
+ const [typeCandidates, rootTypeNameMap, extensions] = buildTypeCandidates({
3477
+ subschemas: transformedSubschemas,
3478
+ originalSubschemaMap,
3479
+ types,
3480
+ typeDefs: typeDefs || [],
3481
+ parseOptions: rest,
3482
+ directiveMap,
3483
+ schemaDefs,
3484
+ mergeDirectives
3485
+ });
3486
+ const stitchingInfo = createStitchingInfo(
3487
+ subschemaMap,
3488
+ typeCandidates,
3489
+ mergeTypes
3490
+ );
3491
+ const { typeMap: newTypeMap, directives: newDirectives } = buildTypes({
3492
+ typeCandidates,
3493
+ directives: Object.values(directiveMap),
3494
+ stitchingInfo,
3495
+ rootTypeNames: Object.values(rootTypeNameMap),
3496
+ onTypeConflict,
3497
+ mergeTypes,
3498
+ typeMergingOptions
3499
+ });
3500
+ return {
3501
+ typeCandidates,
3502
+ newTypeMap,
3503
+ newDirectives,
3504
+ rootTypeNameMap,
3505
+ stitchingInfo,
3506
+ schemaDefs,
3507
+ extensions,
3508
+ mergeDirectives
3509
+ };
3510
+ }
3482
3511
 
3483
3512
  const forwardArgsToSelectionSet = (selectionSet, mapping) => {
3484
3513
  const selectionSetDef = utils.parseSelectionSet(selectionSet, { noLocation: true });
package/dist/index.d.cts CHANGED
@@ -85,7 +85,7 @@ declare module '@graphql-tools/utils' {
85
85
  }
86
86
  }
87
87
 
88
- declare function stitchSchemas<TContext extends Record<string, any> = Record<string, any>>({ subschemas, types, typeDefs, onTypeConflict, mergeTypes, typeMergingOptions, subschemaConfigTransforms, resolvers, inheritResolversFromInterfaces, resolverValidationOptions, updateResolversInPlace, schemaExtensions, ...rest }: IStitchSchemasOptions<TContext>): GraphQLSchema;
88
+ declare function stitchSchemas<TContext extends Record<string, any> = Record<string, any>>(options: IStitchSchemasOptions<TContext>): GraphQLSchema;
89
89
 
90
90
  declare function createMergedTypeResolver<TContext extends Record<string, any> = any>(mergedTypeResolverOptions: MergedTypeResolverOptions, mergedType?: GraphQLOutputType | string): MergedTypeResolver<TContext> | undefined;
91
91
 
package/dist/index.d.ts CHANGED
@@ -85,7 +85,7 @@ declare module '@graphql-tools/utils' {
85
85
  }
86
86
  }
87
87
 
88
- declare function stitchSchemas<TContext extends Record<string, any> = Record<string, any>>({ subschemas, types, typeDefs, onTypeConflict, mergeTypes, typeMergingOptions, subschemaConfigTransforms, resolvers, inheritResolversFromInterfaces, resolverValidationOptions, updateResolversInPlace, schemaExtensions, ...rest }: IStitchSchemasOptions<TContext>): GraphQLSchema;
88
+ declare function stitchSchemas<TContext extends Record<string, any> = Record<string, any>>(options: IStitchSchemasOptions<TContext>): GraphQLSchema;
89
89
 
90
90
  declare function createMergedTypeResolver<TContext extends Record<string, any> = any>(mergedTypeResolverOptions: MergedTypeResolverOptions, mergedType?: GraphQLOutputType | string): MergedTypeResolver<TContext> | undefined;
91
91
 
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import { mergeType, mergeInputType, mergeInterface, mergeUnion, mergeEnum, merge
3
3
  import { extendResolversFromInterfaces, addResolversToSchema, assertResolversPresent, makeExecutableSchema } from '@graphql-tools/schema';
4
4
  import { mergeDeep, collectSubFields, memoize3, memoize2, memoize1, parseSelectionSet, collectFields, isSome, getImplementingTypes, getRootTypeNames, filterSchema, getDescription, createNamedStub, createStub, getRootTypeMap, getRootTypes, inspect, rewireTypes, getOperationASTFromRequest, getDefinedRootType, getArgumentValues } from '@graphql-tools/utils';
5
5
  import { handleMaybePromise } from '@whatwg-node/promise-helpers';
6
- import { getNamedType, Kind, isAbstractType, TypeNameMetaFieldDef, isObjectType, GraphQLList, isLeafType, isInputObjectType, isUnionType, print, isInterfaceType, visit, isScalarType, isCompositeType, isNonNullType, isEnumType, isListType, getNullableType, isNullableType, GraphQLObjectType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLScalarType, GraphQLDirective, valueFromASTUntyped, getDirectiveValues, GraphQLDeprecatedDirective, DirectiveLocation, isNamedType, isIntrospectionType, isDirective, isSpecifiedScalarType, specifiedDirectives, GraphQLSchema, extendSchema, isSpecifiedDirective } from 'graphql';
6
+ import { getNamedType, Kind, isAbstractType, TypeNameMetaFieldDef, isObjectType, GraphQLList, isLeafType, isInputObjectType, isUnionType, print, isInterfaceType, visit, isScalarType, isCompositeType, isNonNullType, isEnumType, isListType, getNullableType, isNullableType, GraphQLObjectType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLScalarType, GraphQLDirective, valueFromASTUntyped, getDirectiveValues, GraphQLDeprecatedDirective, DirectiveLocation, isNamedType, isIntrospectionType, isDirective, isSpecifiedScalarType, GraphQLSchema, concatAST, extendSchema, specifiedDirectives, isSpecifiedDirective } from 'graphql';
7
7
  import { batchDelegateToSchema } from '@graphql-tools/batch-delegate';
8
8
  import { FilterTypes, TransformCompositeFields, wrapSchema } from '@graphql-tools/wrap';
9
9
  import { getFragmentsFromDocument } from '@graphql-tools/executor';
@@ -3218,80 +3218,46 @@ function onTypeConflictToCandidateSelector(onTypeConflict) {
3218
3218
  });
3219
3219
  }
3220
3220
 
3221
- function stitchSchemas({
3222
- subschemas = [],
3223
- types = [],
3224
- typeDefs = [],
3225
- onTypeConflict,
3226
- mergeTypes = true,
3227
- typeMergingOptions,
3228
- subschemaConfigTransforms = [],
3229
- resolvers = {},
3230
- inheritResolversFromInterfaces = false,
3231
- resolverValidationOptions = {},
3232
- updateResolversInPlace = true,
3233
- schemaExtensions,
3234
- ...rest
3235
- }) {
3236
- const mergeDirectives = rest.mergeDirectives ?? true;
3237
- const transformedSubschemas = [];
3238
- const subschemaMap = /* @__PURE__ */ new Map();
3239
- const originalSubschemaMap = /* @__PURE__ */ new Map();
3240
- for (const subschema of subschemas) {
3241
- for (const transformedSubschemaConfig of applySubschemaConfigTransforms(
3242
- subschemaConfigTransforms,
3243
- subschema,
3244
- subschemaMap,
3245
- originalSubschemaMap
3246
- )) {
3247
- transformedSubschemas.push(transformedSubschemaConfig);
3248
- }
3249
- }
3250
- const directiveMap = /* @__PURE__ */ Object.create(null);
3251
- for (const directive of specifiedDirectives) {
3252
- directiveMap[directive.name] = directive;
3253
- }
3254
- const schemaDefs = /* @__PURE__ */ Object.create(null);
3255
- const [typeCandidates, rootTypeNameMap, extensions] = buildTypeCandidates({
3256
- subschemas: transformedSubschemas,
3257
- originalSubschemaMap,
3258
- types,
3259
- typeDefs: typeDefs || [],
3260
- parseOptions: rest,
3261
- directiveMap,
3262
- schemaDefs,
3263
- mergeDirectives
3264
- });
3265
- let stitchingInfo = createStitchingInfo(
3266
- subschemaMap,
3267
- typeCandidates,
3268
- mergeTypes
3269
- );
3270
- const { typeMap: newTypeMap, directives: newDirectives } = buildTypes({
3221
+ function stitchSchemas(options) {
3222
+ const {
3223
+ resolvers = {},
3224
+ inheritResolversFromInterfaces = false,
3225
+ resolverValidationOptions = {},
3226
+ updateResolversInPlace = true,
3227
+ schemaExtensions,
3228
+ ...rest
3229
+ } = options;
3230
+ let {
3271
3231
  typeCandidates,
3272
- directives: Object.values(directiveMap),
3232
+ newTypeMap,
3233
+ newDirectives,
3234
+ rootTypeNameMap,
3273
3235
  stitchingInfo,
3274
- rootTypeNames: Object.values(rootTypeNameMap),
3275
- onTypeConflict,
3276
- mergeTypes,
3277
- typeMergingOptions
3278
- });
3279
- if (!mergeDirectives) {
3280
- stripCustomDirectiveUsages(Object.values(newTypeMap));
3281
- }
3282
- let schema = new GraphQLSchema({
3283
- query: newTypeMap[rootTypeNameMap.query],
3284
- mutation: newTypeMap[rootTypeNameMap.mutation],
3285
- subscription: newTypeMap[rootTypeNameMap.subscription],
3286
- types: Object.values(newTypeMap),
3287
- directives: newDirectives,
3288
- astNode: schemaDefs.schemaDef,
3289
- extensionASTNodes: schemaDefs.schemaExtensions,
3290
- extensions: null,
3291
- assumeValid: rest.assumeValid
3292
- });
3293
- for (const extension of extensions) {
3294
- schema = extendSchema(schema, extension, {
3236
+ schemaDefs,
3237
+ extensions,
3238
+ mergeDirectives
3239
+ } = prepareStitchingData(options);
3240
+ let schema;
3241
+ {
3242
+ const finalTypes = Object.values(newTypeMap);
3243
+ if (!mergeDirectives) {
3244
+ stripCustomDirectiveUsages(finalTypes);
3245
+ }
3246
+ schema = new GraphQLSchema({
3247
+ query: newTypeMap[rootTypeNameMap.query],
3248
+ mutation: newTypeMap[rootTypeNameMap.mutation],
3249
+ subscription: newTypeMap[rootTypeNameMap.subscription],
3250
+ types: finalTypes,
3251
+ directives: newDirectives,
3252
+ astNode: schemaDefs.schemaDef,
3253
+ extensionASTNodes: schemaDefs.schemaExtensions,
3254
+ extensions: null,
3255
+ assumeValid: rest.assumeValid
3256
+ });
3257
+ }
3258
+ if (extensions.length > 0) {
3259
+ const mergedExtensions = concatAST(extensions);
3260
+ schema = extendSchema(schema, mergedExtensions, {
3295
3261
  commentDescriptions: true
3296
3262
  });
3297
3263
  }
@@ -3315,10 +3281,8 @@ function stitchSchemas({
3315
3281
  }
3316
3282
  addStitchingInfo(schema, stitchingInfo);
3317
3283
  if (schemaExtensions) {
3318
- if (Array.isArray(schemaExtensions)) {
3319
- schemaExtensions = mergeExtensions(schemaExtensions);
3320
- }
3321
- applyExtensions(schema, schemaExtensions);
3284
+ const mergedSchemaExtensions = Array.isArray(schemaExtensions) ? mergeExtensions(schemaExtensions) : schemaExtensions;
3285
+ applyExtensions(schema, mergedSchemaExtensions);
3322
3286
  }
3323
3287
  return schema;
3324
3288
  }
@@ -3477,6 +3441,71 @@ function applySubschemaConfigTransforms(subschemaConfigTransforms, subschemaOrSu
3477
3441
  }
3478
3442
  return transformedSubschemas;
3479
3443
  }
3444
+ function prepareStitchingData(options) {
3445
+ const {
3446
+ subschemas = [],
3447
+ types = [],
3448
+ typeDefs = [],
3449
+ onTypeConflict,
3450
+ mergeTypes = true,
3451
+ typeMergingOptions,
3452
+ subschemaConfigTransforms = [],
3453
+ ...rest
3454
+ } = options;
3455
+ const mergeDirectives = rest.mergeDirectives ?? true;
3456
+ const transformedSubschemas = [];
3457
+ const subschemaMap = /* @__PURE__ */ new Map();
3458
+ const originalSubschemaMap = /* @__PURE__ */ new Map();
3459
+ for (const subschema of subschemas) {
3460
+ for (const transformedSubschemaConfig of applySubschemaConfigTransforms(
3461
+ subschemaConfigTransforms,
3462
+ subschema,
3463
+ subschemaMap,
3464
+ originalSubschemaMap
3465
+ )) {
3466
+ transformedSubschemas.push(transformedSubschemaConfig);
3467
+ }
3468
+ }
3469
+ const directiveMap = /* @__PURE__ */ Object.create(null);
3470
+ for (const directive of specifiedDirectives) {
3471
+ directiveMap[directive.name] = directive;
3472
+ }
3473
+ const schemaDefs = /* @__PURE__ */ Object.create(null);
3474
+ const [typeCandidates, rootTypeNameMap, extensions] = buildTypeCandidates({
3475
+ subschemas: transformedSubschemas,
3476
+ originalSubschemaMap,
3477
+ types,
3478
+ typeDefs: typeDefs || [],
3479
+ parseOptions: rest,
3480
+ directiveMap,
3481
+ schemaDefs,
3482
+ mergeDirectives
3483
+ });
3484
+ const stitchingInfo = createStitchingInfo(
3485
+ subschemaMap,
3486
+ typeCandidates,
3487
+ mergeTypes
3488
+ );
3489
+ const { typeMap: newTypeMap, directives: newDirectives } = buildTypes({
3490
+ typeCandidates,
3491
+ directives: Object.values(directiveMap),
3492
+ stitchingInfo,
3493
+ rootTypeNames: Object.values(rootTypeNameMap),
3494
+ onTypeConflict,
3495
+ mergeTypes,
3496
+ typeMergingOptions
3497
+ });
3498
+ return {
3499
+ typeCandidates,
3500
+ newTypeMap,
3501
+ newDirectives,
3502
+ rootTypeNameMap,
3503
+ stitchingInfo,
3504
+ schemaDefs,
3505
+ extensions,
3506
+ mergeDirectives
3507
+ };
3508
+ }
3480
3509
 
3481
3510
  const forwardArgsToSelectionSet = (selectionSet, mapping) => {
3482
3511
  const selectionSetDef = parseSelectionSet(selectionSet, { noLocation: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/stitch",
3
- "version": "10.2.0",
3
+ "version": "10.2.1",
4
4
  "type": "module",
5
5
  "description": "A set of utils for faster development of GraphQL tools",
6
6
  "repository": {