@graphql-codegen/visitor-plugin-common 6.2.2 → 6.2.3

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.
@@ -687,13 +687,30 @@ class SelectionSetToObject {
687
687
  return parentName;
688
688
  }
689
689
  const schemaType = this._schema.getType(typeName);
690
- // Check if current selection set has fragments (e.g., "... AppNotificationFragment" or "... on AppNotification")
691
- const hasFragment = this._selectionSet?.selections?.some(selection => selection.kind === graphql_1.Kind.INLINE_FRAGMENT || selection.kind === graphql_1.Kind.FRAGMENT_SPREAD) ?? false;
690
+ // Check if current selection set has type-narrowing fragments.
691
+ // - Inline fragments are always type-narrowing
692
+ // - Fragment spreads are only type-narrowing if they are on a different type than the current parent schema type
693
+ // (e.g. spreading `fragment Foo on Pet` while processing `Pet` is not type-narrowing).
694
+ const hasTypeNarrowingFragments = this._selectionSet?.selections?.some(selection => {
695
+ if (selection.kind === graphql_1.Kind.INLINE_FRAGMENT) {
696
+ return true;
697
+ }
698
+ if (selection.kind === graphql_1.Kind.FRAGMENT_SPREAD) {
699
+ const spreadFragment = this._loadedFragments.find(lf => lf.name === selection.name.value);
700
+ // If we can't resolve fragment metadata (or the current parent type), treat it as type-narrowing.
701
+ // This avoids incorrectly using interface-rooted names in cases that are actually concrete-targeting.
702
+ return !spreadFragment || !this._parentSchemaType || spreadFragment.onType !== this._parentSchemaType.name;
703
+ }
704
+ return false;
705
+ }) ?? false;
692
706
  // When the parent schema type is an interface:
693
707
  // - If we're processing inline fragments, use the concrete type name
694
708
  // - If we're processing the interface directly, use the interface name
695
709
  // - If we're in a named fragment, always use the concrete type name
696
- if ((0, graphql_1.isObjectType)(schemaType) && this._parentSchemaType && (0, graphql_1.isInterfaceType)(this._parentSchemaType) && !hasFragment) {
710
+ if ((0, graphql_1.isObjectType)(schemaType) &&
711
+ this._parentSchemaType &&
712
+ (0, graphql_1.isInterfaceType)(this._parentSchemaType) &&
713
+ !hasTypeNarrowingFragments) {
697
714
  return `${parentName}_${this._parentSchemaType.name}`;
698
715
  }
699
716
  return `${parentName}_${typeName}`;
@@ -683,13 +683,30 @@ export class SelectionSetToObject {
683
683
  return parentName;
684
684
  }
685
685
  const schemaType = this._schema.getType(typeName);
686
- // Check if current selection set has fragments (e.g., "... AppNotificationFragment" or "... on AppNotification")
687
- const hasFragment = this._selectionSet?.selections?.some(selection => selection.kind === Kind.INLINE_FRAGMENT || selection.kind === Kind.FRAGMENT_SPREAD) ?? false;
686
+ // Check if current selection set has type-narrowing fragments.
687
+ // - Inline fragments are always type-narrowing
688
+ // - Fragment spreads are only type-narrowing if they are on a different type than the current parent schema type
689
+ // (e.g. spreading `fragment Foo on Pet` while processing `Pet` is not type-narrowing).
690
+ const hasTypeNarrowingFragments = this._selectionSet?.selections?.some(selection => {
691
+ if (selection.kind === Kind.INLINE_FRAGMENT) {
692
+ return true;
693
+ }
694
+ if (selection.kind === Kind.FRAGMENT_SPREAD) {
695
+ const spreadFragment = this._loadedFragments.find(lf => lf.name === selection.name.value);
696
+ // If we can't resolve fragment metadata (or the current parent type), treat it as type-narrowing.
697
+ // This avoids incorrectly using interface-rooted names in cases that are actually concrete-targeting.
698
+ return !spreadFragment || !this._parentSchemaType || spreadFragment.onType !== this._parentSchemaType.name;
699
+ }
700
+ return false;
701
+ }) ?? false;
688
702
  // When the parent schema type is an interface:
689
703
  // - If we're processing inline fragments, use the concrete type name
690
704
  // - If we're processing the interface directly, use the interface name
691
705
  // - If we're in a named fragment, always use the concrete type name
692
- if (isObjectType(schemaType) && this._parentSchemaType && isInterfaceType(this._parentSchemaType) && !hasFragment) {
706
+ if (isObjectType(schemaType) &&
707
+ this._parentSchemaType &&
708
+ isInterfaceType(this._parentSchemaType) &&
709
+ !hasTypeNarrowingFragments) {
693
710
  return `${parentName}_${this._parentSchemaType.name}`;
694
711
  }
695
712
  return `${parentName}_${typeName}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "6.2.2",
3
+ "version": "6.2.3",
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"
6
6
  },