@graphql-codegen/visitor-plugin-common 8.0.1 → 8.0.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.
@@ -200,9 +200,13 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
200
200
  const isExternalFile = !!this.config.enumValues[typeName].sourceFile;
201
201
  prev[typeName] = isExternalFile
202
202
  ? this.config.enumValues[typeName].typeIdentifierConverted
203
- : // `sourceIdentifier` is `null` when `enumValues` maps this enum with an object, and
204
- // `String()` prints it as the `null` type, e.g. `MyEnum: null`
205
- String(this.config.enumValues[typeName].sourceIdentifier);
203
+ : // `sourceIdentifier` is `null` when `enumValues` maps this enum with an object. The
204
+ // typescript plugin still generates the enum then, so use the name it gives it.
205
+ (this.config.enumValues[typeName].sourceIdentifier ??
206
+ this.convertName(typeName, {
207
+ useTypesPrefix: this.config.enumPrefix,
208
+ useTypesSuffix: this.config.enumSuffix,
209
+ }, true));
206
210
  }
207
211
  else if (defaultMapperType && !hasPlaceholder(defaultMapperType)) {
208
212
  prev[typeName] = applyWrapper(defaultMapperType);
@@ -98,7 +98,7 @@ class SelectionSetToObject {
98
98
  }
99
99
  else if ((0, graphql_1.isInterfaceType)(typeOnSchema) &&
100
100
  parentType.getInterfaces().includes(typeOnSchema)) {
101
- this._appendToTypeMap(types, parentType.name, fields);
101
+ this._appendToTypeMap(types, parentType.name, fieldsWithFragmentDirectives);
102
102
  this._appendToTypeMap(types, parentType.name, spreadsUsage[parentType.name]);
103
103
  this._collectInlineFragments(typeOnSchema, inlines, types);
104
104
  }
@@ -111,16 +111,17 @@ class SelectionSetToObject {
111
111
  ? this._schema.getType(node.typeCondition.name.value)
112
112
  : parentType;
113
113
  const { fields, inlines, spreads } = (0, utils_js_1.separateSelectionSet)(node.selectionSet.selections);
114
+ const { fieldsWithFragmentDirectives } = enrichFieldWithDirectives({ node, fields });
114
115
  const spreadsUsage = this.buildFragmentSpreadsUsage(spreads);
115
116
  if ((0, graphql_1.isObjectType)(schemaType) &&
116
117
  possibleTypes.find(possibleType => possibleType.name === schemaType.name)) {
117
- this._appendToTypeMap(types, schemaType.name, fields);
118
+ this._appendToTypeMap(types, schemaType.name, fieldsWithFragmentDirectives);
118
119
  this._appendToTypeMap(types, schemaType.name, spreadsUsage[schemaType.name]);
119
120
  this._collectInlineFragments(schemaType, inlines, types);
120
121
  }
121
122
  else if ((0, graphql_1.isInterfaceType)(schemaType) && schemaType.name === parentType.name) {
122
123
  for (const possibleType of possibleTypes) {
123
- this._appendToTypeMap(types, possibleType.name, fields);
124
+ this._appendToTypeMap(types, possibleType.name, fieldsWithFragmentDirectives);
124
125
  this._appendToTypeMap(types, possibleType.name, spreadsUsage[possibleType.name]);
125
126
  this._collectInlineFragments(schemaType, inlines, types);
126
127
  }
@@ -136,7 +137,7 @@ class SelectionSetToObject {
136
137
  // in case the possible type actually implements the given interface
137
138
  if (fragmentSpreadType &&
138
139
  (0, graphql_1.isTypeSubTypeOf)(this._schema, possibleType, fragmentSpreadType)) {
139
- this._appendToTypeMap(types, possibleType.name, fields);
140
+ this._appendToTypeMap(types, possibleType.name, fieldsWithFragmentDirectives);
140
141
  this._appendToTypeMap(types, possibleType.name, spreadsUsage[possibleType.name]);
141
142
  }
142
143
  }
@@ -162,7 +163,7 @@ class SelectionSetToObject {
162
163
  const possibleInterfaceTypes = (0, utils_js_1.getPossibleTypes)(this._schema, schemaType);
163
164
  for (const possibleType of possibleTypes) {
164
165
  if (possibleInterfaceTypes.find(possibleInterfaceType => possibleInterfaceType.name === possibleType.name)) {
165
- this._appendToTypeMap(types, possibleType.name, fields);
166
+ this._appendToTypeMap(types, possibleType.name, fieldsWithFragmentDirectives);
166
167
  this._appendToTypeMap(types, possibleType.name, spreadsUsage[possibleType.name]);
167
168
  this._collectInlineFragments(schemaType, inlines, types);
168
169
  }
@@ -170,7 +171,7 @@ class SelectionSetToObject {
170
171
  }
171
172
  else {
172
173
  for (const possibleType of possibleTypes) {
173
- this._appendToTypeMap(types, possibleType.name, fields);
174
+ this._appendToTypeMap(types, possibleType.name, fieldsWithFragmentDirectives);
174
175
  this._appendToTypeMap(types, possibleType.name, spreadsUsage[possibleType.name]);
175
176
  }
176
177
  }
@@ -548,6 +549,8 @@ class SelectionSetToObject {
548
549
  const primitiveAliasFields = new Map();
549
550
  const linkFieldSelectionSets = new Map();
550
551
  let requireTypename = false;
552
+ // `__typename` selected only under `@skip`/`@include` may be absent from the response
553
+ let conditionalTypename = false;
551
554
  // usages via fragment typescript type
552
555
  const fragmentsSpreadUsages = [];
553
556
  // ensure we mutate no function params
@@ -587,7 +590,13 @@ class SelectionSetToObject {
587
590
  primitiveAliasFields.set(selectionNode.alias.value, selectionNode);
588
591
  }
589
592
  else if (selectionNode.name.value === '__typename') {
590
- requireTypename = true;
593
+ if ((0, utils_js_1.hasConditionalDirectives)(selectionNode.directives) ||
594
+ (0, utils_js_1.hasConditionalDirectives)(selectionNode.fragmentDirectives)) {
595
+ conditionalTypename = true;
596
+ }
597
+ else {
598
+ requireTypename = true;
599
+ }
591
600
  }
592
601
  else {
593
602
  primitiveFields.set(selectionNode.name.value, selectionNode);
@@ -679,7 +688,7 @@ class SelectionSetToObject {
679
688
  selectionSet: this._processor.config.wrapTypeWithModifiers(selectionSetObjects.mergedTypeString.split(`\n`).join(`\n `), selectedFieldType),
680
689
  });
681
690
  }
682
- const typeInfoField = this.buildTypeNameField(parentSchemaType, this._config.nonOptionalTypename, requireTypename, this._config.skipTypeNameForRoot);
691
+ const typeInfoField = this.buildTypeNameField(parentSchemaType, this._config.nonOptionalTypename, requireTypename, this._config.skipTypeNameForRoot, conditionalTypename);
683
692
  // Only add the typename field if we're not merging fragment
684
693
  // types. If we are merging, we need to wait until we know all
685
694
  // the involved typenames.
@@ -741,7 +750,7 @@ class SelectionSetToObject {
741
750
  dependentTypes: linkFieldsInterfaces,
742
751
  };
743
752
  }
744
- buildTypeNameField(type, nonOptionalTypename, queriedForTypename, skipTypeNameForRoot) {
753
+ buildTypeNameField(type, nonOptionalTypename, queriedForTypename, skipTypeNameForRoot, conditionallyQueriedForTypename = false) {
745
754
  const typenameField = {
746
755
  name: this._processor.config.formatNamedField({ name: '__typename' }),
747
756
  type: `'${type.name}'`,
@@ -749,6 +758,17 @@ class SelectionSetToObject {
749
758
  if (queriedForTypename) {
750
759
  return typenameField;
751
760
  }
761
+ if (conditionallyQueriedForTypename) {
762
+ return nonOptionalTypename
763
+ ? typenameField
764
+ : {
765
+ ...typenameField,
766
+ name: this._processor.config.formatNamedField({
767
+ name: '__typename',
768
+ isOptional: true,
769
+ }),
770
+ };
771
+ }
752
772
  const rootTypes = (0, utils_1.getRootTypes)(this._schema);
753
773
  if (rootTypes.has(type) && skipTypeNameForRoot) {
754
774
  return null;
@@ -196,9 +196,13 @@ export class BaseResolversVisitor extends BaseVisitor {
196
196
  const isExternalFile = !!this.config.enumValues[typeName].sourceFile;
197
197
  prev[typeName] = isExternalFile
198
198
  ? this.config.enumValues[typeName].typeIdentifierConverted
199
- : // `sourceIdentifier` is `null` when `enumValues` maps this enum with an object, and
200
- // `String()` prints it as the `null` type, e.g. `MyEnum: null`
201
- String(this.config.enumValues[typeName].sourceIdentifier);
199
+ : // `sourceIdentifier` is `null` when `enumValues` maps this enum with an object. The
200
+ // typescript plugin still generates the enum then, so use the name it gives it.
201
+ (this.config.enumValues[typeName].sourceIdentifier ??
202
+ this.convertName(typeName, {
203
+ useTypesPrefix: this.config.enumPrefix,
204
+ useTypesSuffix: this.config.enumSuffix,
205
+ }, true));
202
206
  }
203
207
  else if (defaultMapperType && !hasPlaceholder(defaultMapperType)) {
204
208
  prev[typeName] = applyWrapper(defaultMapperType);
@@ -94,7 +94,7 @@ export class SelectionSetToObject {
94
94
  }
95
95
  else if (isInterfaceType(typeOnSchema) &&
96
96
  parentType.getInterfaces().includes(typeOnSchema)) {
97
- this._appendToTypeMap(types, parentType.name, fields);
97
+ this._appendToTypeMap(types, parentType.name, fieldsWithFragmentDirectives);
98
98
  this._appendToTypeMap(types, parentType.name, spreadsUsage[parentType.name]);
99
99
  this._collectInlineFragments(typeOnSchema, inlines, types);
100
100
  }
@@ -107,16 +107,17 @@ export class SelectionSetToObject {
107
107
  ? this._schema.getType(node.typeCondition.name.value)
108
108
  : parentType;
109
109
  const { fields, inlines, spreads } = separateSelectionSet(node.selectionSet.selections);
110
+ const { fieldsWithFragmentDirectives } = enrichFieldWithDirectives({ node, fields });
110
111
  const spreadsUsage = this.buildFragmentSpreadsUsage(spreads);
111
112
  if (isObjectType(schemaType) &&
112
113
  possibleTypes.find(possibleType => possibleType.name === schemaType.name)) {
113
- this._appendToTypeMap(types, schemaType.name, fields);
114
+ this._appendToTypeMap(types, schemaType.name, fieldsWithFragmentDirectives);
114
115
  this._appendToTypeMap(types, schemaType.name, spreadsUsage[schemaType.name]);
115
116
  this._collectInlineFragments(schemaType, inlines, types);
116
117
  }
117
118
  else if (isInterfaceType(schemaType) && schemaType.name === parentType.name) {
118
119
  for (const possibleType of possibleTypes) {
119
- this._appendToTypeMap(types, possibleType.name, fields);
120
+ this._appendToTypeMap(types, possibleType.name, fieldsWithFragmentDirectives);
120
121
  this._appendToTypeMap(types, possibleType.name, spreadsUsage[possibleType.name]);
121
122
  this._collectInlineFragments(schemaType, inlines, types);
122
123
  }
@@ -132,7 +133,7 @@ export class SelectionSetToObject {
132
133
  // in case the possible type actually implements the given interface
133
134
  if (fragmentSpreadType &&
134
135
  isTypeSubTypeOf(this._schema, possibleType, fragmentSpreadType)) {
135
- this._appendToTypeMap(types, possibleType.name, fields);
136
+ this._appendToTypeMap(types, possibleType.name, fieldsWithFragmentDirectives);
136
137
  this._appendToTypeMap(types, possibleType.name, spreadsUsage[possibleType.name]);
137
138
  }
138
139
  }
@@ -158,7 +159,7 @@ export class SelectionSetToObject {
158
159
  const possibleInterfaceTypes = getPossibleTypes(this._schema, schemaType);
159
160
  for (const possibleType of possibleTypes) {
160
161
  if (possibleInterfaceTypes.find(possibleInterfaceType => possibleInterfaceType.name === possibleType.name)) {
161
- this._appendToTypeMap(types, possibleType.name, fields);
162
+ this._appendToTypeMap(types, possibleType.name, fieldsWithFragmentDirectives);
162
163
  this._appendToTypeMap(types, possibleType.name, spreadsUsage[possibleType.name]);
163
164
  this._collectInlineFragments(schemaType, inlines, types);
164
165
  }
@@ -166,7 +167,7 @@ export class SelectionSetToObject {
166
167
  }
167
168
  else {
168
169
  for (const possibleType of possibleTypes) {
169
- this._appendToTypeMap(types, possibleType.name, fields);
170
+ this._appendToTypeMap(types, possibleType.name, fieldsWithFragmentDirectives);
170
171
  this._appendToTypeMap(types, possibleType.name, spreadsUsage[possibleType.name]);
171
172
  }
172
173
  }
@@ -544,6 +545,8 @@ export class SelectionSetToObject {
544
545
  const primitiveAliasFields = new Map();
545
546
  const linkFieldSelectionSets = new Map();
546
547
  let requireTypename = false;
548
+ // `__typename` selected only under `@skip`/`@include` may be absent from the response
549
+ let conditionalTypename = false;
547
550
  // usages via fragment typescript type
548
551
  const fragmentsSpreadUsages = [];
549
552
  // ensure we mutate no function params
@@ -583,7 +586,13 @@ export class SelectionSetToObject {
583
586
  primitiveAliasFields.set(selectionNode.alias.value, selectionNode);
584
587
  }
585
588
  else if (selectionNode.name.value === '__typename') {
586
- requireTypename = true;
589
+ if (hasConditionalDirectives(selectionNode.directives) ||
590
+ hasConditionalDirectives(selectionNode.fragmentDirectives)) {
591
+ conditionalTypename = true;
592
+ }
593
+ else {
594
+ requireTypename = true;
595
+ }
587
596
  }
588
597
  else {
589
598
  primitiveFields.set(selectionNode.name.value, selectionNode);
@@ -675,7 +684,7 @@ export class SelectionSetToObject {
675
684
  selectionSet: this._processor.config.wrapTypeWithModifiers(selectionSetObjects.mergedTypeString.split(`\n`).join(`\n `), selectedFieldType),
676
685
  });
677
686
  }
678
- const typeInfoField = this.buildTypeNameField(parentSchemaType, this._config.nonOptionalTypename, requireTypename, this._config.skipTypeNameForRoot);
687
+ const typeInfoField = this.buildTypeNameField(parentSchemaType, this._config.nonOptionalTypename, requireTypename, this._config.skipTypeNameForRoot, conditionalTypename);
679
688
  // Only add the typename field if we're not merging fragment
680
689
  // types. If we are merging, we need to wait until we know all
681
690
  // the involved typenames.
@@ -737,7 +746,7 @@ export class SelectionSetToObject {
737
746
  dependentTypes: linkFieldsInterfaces,
738
747
  };
739
748
  }
740
- buildTypeNameField(type, nonOptionalTypename, queriedForTypename, skipTypeNameForRoot) {
749
+ buildTypeNameField(type, nonOptionalTypename, queriedForTypename, skipTypeNameForRoot, conditionallyQueriedForTypename = false) {
741
750
  const typenameField = {
742
751
  name: this._processor.config.formatNamedField({ name: '__typename' }),
743
752
  type: `'${type.name}'`,
@@ -745,6 +754,17 @@ export class SelectionSetToObject {
745
754
  if (queriedForTypename) {
746
755
  return typenameField;
747
756
  }
757
+ if (conditionallyQueriedForTypename) {
758
+ return nonOptionalTypename
759
+ ? typenameField
760
+ : {
761
+ ...typenameField,
762
+ name: this._processor.config.formatNamedField({
763
+ name: '__typename',
764
+ isOptional: true,
765
+ }),
766
+ };
767
+ }
748
768
  const rootTypes = getRootTypes(this._schema);
749
769
  if (rootTypes.has(type) && skipTypeNameForRoot) {
750
770
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "8.0.1",
3
+ "version": "8.0.3",
4
4
  "peerDependencies": {
5
5
  "graphql": "^16.0.0 || ^17.0.0"
6
6
  },
@@ -85,7 +85,7 @@ export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig =
85
85
  fields: string[];
86
86
  dependentTypes: DependentType[];
87
87
  };
88
- protected buildTypeNameField(type: GraphQLObjectType, nonOptionalTypename: boolean, queriedForTypename: boolean, skipTypeNameForRoot: boolean): {
88
+ protected buildTypeNameField(type: GraphQLObjectType, nonOptionalTypename: boolean, queriedForTypename: boolean, skipTypeNameForRoot: boolean, conditionallyQueriedForTypename?: boolean): {
89
89
  name: string;
90
90
  type: string;
91
91
  } | null;
@@ -85,7 +85,7 @@ export declare class SelectionSetToObject<Config extends ParsedDocumentsConfig =
85
85
  fields: string[];
86
86
  dependentTypes: DependentType[];
87
87
  };
88
- protected buildTypeNameField(type: GraphQLObjectType, nonOptionalTypename: boolean, queriedForTypename: boolean, skipTypeNameForRoot: boolean): {
88
+ protected buildTypeNameField(type: GraphQLObjectType, nonOptionalTypename: boolean, queriedForTypename: boolean, skipTypeNameForRoot: boolean, conditionallyQueriedForTypename?: boolean): {
89
89
  name: string;
90
90
  type: string;
91
91
  } | null;