@graphql-codegen/visitor-plugin-common 7.2.2 → 7.2.3-alpha-20260727135845-3c5f2a39ff05ff56468cf8eb757df8244f841d0a

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.
@@ -360,22 +360,33 @@ class SelectionSetToObject {
360
360
  }
361
361
  }
362
362
  if (conditionalDirectivesFound) {
363
- // When a FragmentSpreadUsage is marked as conditional,
364
- // it should just be treated like an Inline Fragment
365
- // i.e. every field in the fragment's selection set should be optional
366
- const flattenedSelectionNodes = selectionNodes.reduce((prev, node) => {
367
- if ('kind' in node) {
368
- prev.push(node);
369
- return prev;
363
+ const flattenedSelectionNodes = [];
364
+ // A conditional FragmentSpreadUsage is handled one of two ways.
365
+ for (const conditionalNode of selectionNodes) {
366
+ // 1. Fragment masking: preserve the mask as an optional `Partial`
367
+ // ref rather than inlining the fragment's fields.
368
+ if (this._config.inlineFragmentTypes === 'mask' &&
369
+ 'fragmentName' in conditionalNode) {
370
+ const { fields: partialFields, dependentTypes: partialDependentTypes } = this.buildSelectionSet(schemaType, [conditionalNode], {
371
+ partialTypes: true,
372
+ parentFieldName: parentName,
373
+ });
374
+ prev[typeName].push(this.selectionSetStringFromFields(partialFields));
375
+ dependentTypes.push(...partialDependentTypes);
376
+ continue;
370
377
  }
371
- // When a node is a FragmentSpreadUsage,
372
- // We just "inline" all the field in its selection set. Note: each field has fragmentDirectives which should contain `@skip` or `@inlcude`
373
- // So, `buildSelectionSet` function below can correctly make said fields optional
374
- for (const fragmentSpreadUsageSelectionNode of node.selectionNodes) {
375
- prev.push(fragmentSpreadUsageSelectionNode);
378
+ // 2. Otherwise (`combine` / conditional inline-fragment fields):
379
+ // treat it like an Inline Fragment - inline the fields so
380
+ // `buildSelectionSet` can make each one optional. Note: each field
381
+ // carries `fragmentDirectives` containing `@skip`/`@include`.
382
+ if ('kind' in conditionalNode) {
383
+ flattenedSelectionNodes.push(conditionalNode);
384
+ continue;
376
385
  }
377
- return prev;
378
- }, []);
386
+ for (const fragmentSpreadUsageSelectionNode of conditionalNode.selectionNodes) {
387
+ flattenedSelectionNodes.push(fragmentSpreadUsageSelectionNode);
388
+ }
389
+ }
379
390
  // Top-level INLINE_FRAGMENT / FRAGMENT_SPREAD nodes among the
380
391
  // inlined selections cannot be consumed directly by
381
392
  // `buildSelectionSet`, which only handles FIELD and DIRECTIVE
@@ -690,7 +701,17 @@ class SelectionSetToObject {
690
701
  }
691
702
  else if (this._config.inlineFragmentTypes === 'mask') {
692
703
  fields.push(`{ ' $fragmentRefs'?: { ${fragmentsSpreadUsages
693
- .map(name => `'${name}': ${options.unsetTypes ? `Incremental<${name}>` : name}`)
704
+ .map(name => {
705
+ // A conditional (`@skip`/`@include`) masked spread may be absent,
706
+ // so its data is wrapped in `Partial`.
707
+ if (options.partialTypes) {
708
+ return `'${name}': Partial<${name}>`;
709
+ }
710
+ if (options.unsetTypes) {
711
+ return `'${name}': Incremental<${name}>`;
712
+ }
713
+ return `'${name}': ${name}`;
714
+ })
694
715
  .join(`;`)} } }`);
695
716
  }
696
717
  }
@@ -356,22 +356,33 @@ export class SelectionSetToObject {
356
356
  }
357
357
  }
358
358
  if (conditionalDirectivesFound) {
359
- // When a FragmentSpreadUsage is marked as conditional,
360
- // it should just be treated like an Inline Fragment
361
- // i.e. every field in the fragment's selection set should be optional
362
- const flattenedSelectionNodes = selectionNodes.reduce((prev, node) => {
363
- if ('kind' in node) {
364
- prev.push(node);
365
- return prev;
359
+ const flattenedSelectionNodes = [];
360
+ // A conditional FragmentSpreadUsage is handled one of two ways.
361
+ for (const conditionalNode of selectionNodes) {
362
+ // 1. Fragment masking: preserve the mask as an optional `Partial`
363
+ // ref rather than inlining the fragment's fields.
364
+ if (this._config.inlineFragmentTypes === 'mask' &&
365
+ 'fragmentName' in conditionalNode) {
366
+ const { fields: partialFields, dependentTypes: partialDependentTypes } = this.buildSelectionSet(schemaType, [conditionalNode], {
367
+ partialTypes: true,
368
+ parentFieldName: parentName,
369
+ });
370
+ prev[typeName].push(this.selectionSetStringFromFields(partialFields));
371
+ dependentTypes.push(...partialDependentTypes);
372
+ continue;
366
373
  }
367
- // When a node is a FragmentSpreadUsage,
368
- // We just "inline" all the field in its selection set. Note: each field has fragmentDirectives which should contain `@skip` or `@inlcude`
369
- // So, `buildSelectionSet` function below can correctly make said fields optional
370
- for (const fragmentSpreadUsageSelectionNode of node.selectionNodes) {
371
- prev.push(fragmentSpreadUsageSelectionNode);
374
+ // 2. Otherwise (`combine` / conditional inline-fragment fields):
375
+ // treat it like an Inline Fragment - inline the fields so
376
+ // `buildSelectionSet` can make each one optional. Note: each field
377
+ // carries `fragmentDirectives` containing `@skip`/`@include`.
378
+ if ('kind' in conditionalNode) {
379
+ flattenedSelectionNodes.push(conditionalNode);
380
+ continue;
372
381
  }
373
- return prev;
374
- }, []);
382
+ for (const fragmentSpreadUsageSelectionNode of conditionalNode.selectionNodes) {
383
+ flattenedSelectionNodes.push(fragmentSpreadUsageSelectionNode);
384
+ }
385
+ }
375
386
  // Top-level INLINE_FRAGMENT / FRAGMENT_SPREAD nodes among the
376
387
  // inlined selections cannot be consumed directly by
377
388
  // `buildSelectionSet`, which only handles FIELD and DIRECTIVE
@@ -686,7 +697,17 @@ export class SelectionSetToObject {
686
697
  }
687
698
  else if (this._config.inlineFragmentTypes === 'mask') {
688
699
  fields.push(`{ ' $fragmentRefs'?: { ${fragmentsSpreadUsages
689
- .map(name => `'${name}': ${options.unsetTypes ? `Incremental<${name}>` : name}`)
700
+ .map(name => {
701
+ // A conditional (`@skip`/`@include`) masked spread may be absent,
702
+ // so its data is wrapped in `Partial`.
703
+ if (options.partialTypes) {
704
+ return `'${name}': Partial<${name}>`;
705
+ }
706
+ if (options.unsetTypes) {
707
+ return `'${name}': Incremental<${name}>`;
708
+ }
709
+ return `'${name}': ${name}`;
710
+ })
690
711
  .join(`;`)} } }`);
691
712
  }
692
713
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "7.2.2",
3
+ "version": "7.2.3-alpha-20260727135845-3c5f2a39ff05ff56468cf8eb757df8244f841d0a",
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
  },
@@ -75,6 +75,7 @@ export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig =
75
75
  protected selectionSetStringFromFields(fields: (string | NameAndType)[]): string | null;
76
76
  protected buildSelectionSet(parentSchemaType: GraphQLObjectType, selectionNodes: Array<GroupedTypeNameNode>, options: {
77
77
  unsetTypes?: boolean;
78
+ partialTypes?: boolean;
78
79
  parentFieldName?: string;
79
80
  }): {
80
81
  typeInfo: {
@@ -75,6 +75,7 @@ export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig =
75
75
  protected selectionSetStringFromFields(fields: (string | NameAndType)[]): string | null;
76
76
  protected buildSelectionSet(parentSchemaType: GraphQLObjectType, selectionNodes: Array<GroupedTypeNameNode>, options: {
77
77
  unsetTypes?: boolean;
78
+ partialTypes?: boolean;
78
79
  parentFieldName?: string;
79
80
  }): {
80
81
  typeInfo: {