@graphql-codegen/visitor-plugin-common 7.2.5 → 7.2.6-alpha-20260913024757-23bcbc729bb8c044054d0958237feeea7a45bb2d

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/utils.js CHANGED
@@ -497,7 +497,7 @@ function getFullPathFieldName(selection, parentName) {
497
497
  : selection.name.value;
498
498
  return parentName ? `${parentName}.${fullName}` : fullName;
499
499
  }
500
- const getFieldNames = ({ selections, fieldNames = new Set(), parentName = '', loadedFragments, }) => {
500
+ const getFieldNames = ({ selections, fieldNames = new Set(), parentName = '', loadedFragments, fragmentFieldNamesCache = new Map(), }) => {
501
501
  for (const selection of selections) {
502
502
  switch (selection.kind) {
503
503
  case graphql_1.Kind.FIELD: {
@@ -509,19 +509,30 @@ const getFieldNames = ({ selections, fieldNames = new Set(), parentName = '', lo
509
509
  fieldNames,
510
510
  parentName: fieldName,
511
511
  loadedFragments,
512
+ fragmentFieldNamesCache,
512
513
  });
513
514
  }
514
515
  break;
515
516
  }
516
517
  case graphql_1.Kind.FRAGMENT_SPREAD: {
517
- (0, exports.getFieldNames)({
518
- selections: loadedFragments
519
- .filter(def => def.name === selection.name.value)
520
- .flatMap(s => s.node.selectionSet.selections),
521
- fieldNames,
522
- parentName,
523
- loadedFragments,
524
- });
518
+ const fragmentName = selection.name.value;
519
+ let relativeFieldNames = fragmentFieldNamesCache.get(fragmentName);
520
+ if (!relativeFieldNames) {
521
+ // Compute the fragment's own field names once, relative to its own
522
+ // root (parentName ''), so the result is reusable no matter where
523
+ // this fragment ends up being spread from.
524
+ relativeFieldNames = (0, exports.getFieldNames)({
525
+ selections: loadedFragments
526
+ .filter(def => def.name === fragmentName)
527
+ .flatMap(s => s.node.selectionSet.selections),
528
+ loadedFragments,
529
+ fragmentFieldNamesCache,
530
+ });
531
+ fragmentFieldNamesCache.set(fragmentName, relativeFieldNames);
532
+ }
533
+ for (const relativeFieldName of relativeFieldNames) {
534
+ fieldNames.add(parentName ? `${parentName}.${relativeFieldName}` : relativeFieldName);
535
+ }
525
536
  break;
526
537
  }
527
538
  case graphql_1.Kind.INLINE_FRAGMENT: {
@@ -530,6 +541,7 @@ const getFieldNames = ({ selections, fieldNames = new Set(), parentName = '', lo
530
541
  fieldNames,
531
542
  parentName,
532
543
  loadedFragments,
544
+ fragmentFieldNamesCache,
533
545
  });
534
546
  break;
535
547
  }
package/esm/utils.js CHANGED
@@ -468,7 +468,7 @@ function getFullPathFieldName(selection, parentName) {
468
468
  : selection.name.value;
469
469
  return parentName ? `${parentName}.${fullName}` : fullName;
470
470
  }
471
- export const getFieldNames = ({ selections, fieldNames = new Set(), parentName = '', loadedFragments, }) => {
471
+ export const getFieldNames = ({ selections, fieldNames = new Set(), parentName = '', loadedFragments, fragmentFieldNamesCache = new Map(), }) => {
472
472
  for (const selection of selections) {
473
473
  switch (selection.kind) {
474
474
  case Kind.FIELD: {
@@ -480,19 +480,30 @@ export const getFieldNames = ({ selections, fieldNames = new Set(), parentName =
480
480
  fieldNames,
481
481
  parentName: fieldName,
482
482
  loadedFragments,
483
+ fragmentFieldNamesCache,
483
484
  });
484
485
  }
485
486
  break;
486
487
  }
487
488
  case Kind.FRAGMENT_SPREAD: {
488
- getFieldNames({
489
- selections: loadedFragments
490
- .filter(def => def.name === selection.name.value)
491
- .flatMap(s => s.node.selectionSet.selections),
492
- fieldNames,
493
- parentName,
494
- loadedFragments,
495
- });
489
+ const fragmentName = selection.name.value;
490
+ let relativeFieldNames = fragmentFieldNamesCache.get(fragmentName);
491
+ if (!relativeFieldNames) {
492
+ // Compute the fragment's own field names once, relative to its own
493
+ // root (parentName ''), so the result is reusable no matter where
494
+ // this fragment ends up being spread from.
495
+ relativeFieldNames = getFieldNames({
496
+ selections: loadedFragments
497
+ .filter(def => def.name === fragmentName)
498
+ .flatMap(s => s.node.selectionSet.selections),
499
+ loadedFragments,
500
+ fragmentFieldNamesCache,
501
+ });
502
+ fragmentFieldNamesCache.set(fragmentName, relativeFieldNames);
503
+ }
504
+ for (const relativeFieldName of relativeFieldNames) {
505
+ fieldNames.add(parentName ? `${parentName}.${relativeFieldName}` : relativeFieldName);
506
+ }
496
507
  break;
497
508
  }
498
509
  case Kind.INLINE_FRAGMENT: {
@@ -501,6 +512,7 @@ export const getFieldNames = ({ selections, fieldNames = new Set(), parentName =
501
512
  fieldNames,
502
513
  parentName,
503
514
  loadedFragments,
515
+ fragmentFieldNamesCache,
504
516
  });
505
517
  break;
506
518
  }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "7.2.5",
3
+ "version": "7.2.6-alpha-20260913024757-23bcbc729bb8c044054d0958237feeea7a45bb2d",
4
4
  "peerDependencies": {
5
5
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
6
6
  },
7
7
  "dependencies": {
8
+ "@graphql-codegen/plugin-helpers": "^7.3.0",
8
9
  "@graphql-tools/optimize": "^2.0.0",
9
10
  "@graphql-tools/relay-operation-optimizer": "^7.1.1",
10
11
  "@graphql-tools/utils": "^11.2.0",
@@ -13,8 +14,7 @@
13
14
  "dependency-graph": "^1.0.0",
14
15
  "graphql-tag": "^2.11.0",
15
16
  "parse-filepath": "^1.0.2",
16
- "tslib": "^2.8.0",
17
- "@graphql-codegen/plugin-helpers": "^7.1.0"
17
+ "tslib": "^2.8.0"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
@@ -81,11 +81,12 @@ export declare function groupBy<T>(array: Array<T>, key: (item: T) => string | n
81
81
  };
82
82
  export declare function flatten<T>(array: Array<Array<T>>): Array<T>;
83
83
  export declare function unique<T>(array: Array<T>, key?: (item: T) => string | number): Array<T>;
84
- export declare const getFieldNames: ({ selections, fieldNames, parentName, loadedFragments, }: {
84
+ export declare const getFieldNames: ({ selections, fieldNames, parentName, loadedFragments, fragmentFieldNamesCache, }: {
85
85
  selections: readonly SelectionNode[];
86
86
  fieldNames?: Set<string>;
87
87
  parentName?: string;
88
88
  loadedFragments: LoadedFragment[];
89
+ fragmentFieldNamesCache?: Map<string, ReadonlySet<string>>;
89
90
  }) => Set<string>;
90
91
  export declare const getNodeComment: (node: FieldDefinitionNode | EnumValueDefinitionNode | InputValueDefinitionNode) => string;
91
92
  /**
@@ -81,11 +81,12 @@ export declare function groupBy<T>(array: Array<T>, key: (item: T) => string | n
81
81
  };
82
82
  export declare function flatten<T>(array: Array<Array<T>>): Array<T>;
83
83
  export declare function unique<T>(array: Array<T>, key?: (item: T) => string | number): Array<T>;
84
- export declare const getFieldNames: ({ selections, fieldNames, parentName, loadedFragments, }: {
84
+ export declare const getFieldNames: ({ selections, fieldNames, parentName, loadedFragments, fragmentFieldNamesCache, }: {
85
85
  selections: readonly SelectionNode[];
86
86
  fieldNames?: Set<string>;
87
87
  parentName?: string;
88
88
  loadedFragments: LoadedFragment[];
89
+ fragmentFieldNamesCache?: Map<string, ReadonlySet<string>>;
89
90
  }) => Set<string>;
90
91
  export declare const getNodeComment: (node: FieldDefinitionNode | EnumValueDefinitionNode | InputValueDefinitionNode) => string;
91
92
  /**