Skip to content

Commit 463e6ef

Browse files
authored
[visitor-plugin-common] fix: satisfy strict mode in base-types-visitor (#4683) (#11037)
1 parent 1b0edd8 commit 463e6ef

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
---
4+
5+
Fix type errors in `visitor-plugin-common` (`base-types-visitor`) under `strict: true`:
6+
7+
- `BaseTypesVisitor`'s constructor takes `additionalConfig` as `Partial<TPluginConfig>`, since the visitor fills in the defaults for every key it leaves out.
8+
- `BaseTypesVisitor` handles input objects, objects, interfaces, unions and enums without `fields`, `types`, `values` or `description` by treating them as empty, instead of throwing.
9+
10+
Generated output is unchanged.

‎packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ export class BaseTypesVisitor<
503503
constructor(
504504
protected _schema: GraphQLSchema,
505505
rawConfig: TRawConfig,
506-
additionalConfig: TPluginConfig,
506+
additionalConfig: Partial<TPluginConfig>,
507507
defaultScalars: NormalizedScalarsMap = DEFAULT_SCALARS,
508508
) {
509509
super(rawConfig, {
@@ -568,7 +568,7 @@ export class BaseTypesVisitor<
568568
}
569569

570570
public getScalarsImports(): string[] {
571-
return Object.keys(this.config.scalars).reduce((res, enumName) => {
571+
return Object.keys(this.config.scalars).reduce<string[]>((res, enumName) => {
572572
const mappedValue = this.config.scalars[enumName];
573573

574574
if (mappedValue.input.isExternal) {
@@ -613,7 +613,7 @@ export class BaseTypesVisitor<
613613

614614
return null;
615615
})
616-
.filter(a => a);
616+
.filter((a): a is string => !!a);
617617
}
618618

619619
public get scalarsDefinition(): string {
@@ -694,21 +694,22 @@ export class BaseTypesVisitor<
694694
.asKind(this._parsedConfig.declarationKind.input)
695695
.withName(this.convertName(node))
696696
.withComment(node.description?.value)
697-
.withBlock(node.fields.join('\n'));
697+
.withBlock((node.fields || []).join('\n'));
698698
}
699699

700700
getInputObjectOneOfDeclarationBlock(node: InputObjectTypeDefinitionNode): DeclarationBlock {
701+
const fields = node.fields || [];
701702
return new DeclarationBlock(this._declarationBlockConfig)
702703
.export()
703704
.asKind(
704705
getOneOfInputDeclarationKind({
705-
fieldCount: node.fields.length,
706+
fieldCount: fields.length,
706707
inputDeclarationKind: this._parsedConfig.declarationKind.input,
707708
}),
708709
)
709710
.withName(this.convertName(node))
710711
.withComment(node.description?.value)
711-
.withContent(`\n` + node.fields.join('\n |'));
712+
.withContent(`\n` + fields.join('\n |'));
712713
}
713714

714715
InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string {
@@ -730,9 +731,10 @@ export class BaseTypesVisitor<
730731
): string {
731732
if (this.config.onlyEnums) return '';
732733

733-
const comment = transformComment(node.description.value, 1);
734+
const comment = transformComment(node.description?.value || '', 1);
734735
const { input } = this._parsedConfig.declarationKind;
735736

737+
// `node.type` is already printed to a string by the visitor; removing the cast needs a visited-AST type for `node`
736738
let type: string = node.type as any as string;
737739
if (node.directives && this.config.directiveArgumentAndInputFieldMappings) {
738740
type = this._getDirectiveOverrideType(node.directives) || type;
@@ -753,8 +755,9 @@ export class BaseTypesVisitor<
753755

754756
UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number, parent: any): string {
755757
if (this.config.onlyOperationTypes || this.config.onlyEnums) return '';
758+
// `node.types` are already printed strings, so the original AST node is read from `parent[key]`, and `parent` is `any` because oldVisit's parent is untyped
756759
const originalNode = parent[key] as UnionTypeDefinitionNode;
757-
const possibleTypes = originalNode.types
760+
const possibleTypes = (originalNode.types || [])
758761
.map(t =>
759762
this.scalars[t.name.value] ? this._getScalar(t.name.value, 'output') : this.convertName(t),
760763
)
@@ -764,7 +767,7 @@ export class BaseTypesVisitor<
764767
.export()
765768
.asKind('type')
766769
.withName(this.convertName(node))
767-
.withComment(node.description.value)
770+
.withComment(node.description?.value)
768771
.withContent(possibleTypes).string;
769772
}
770773

@@ -787,6 +790,7 @@ export class BaseTypesVisitor<
787790
): DeclarationBlock {
788791
const optionalTypename = this.config.nonOptionalTypename ? '__typename' : '__typename?';
789792
const { type, interface: interfacesType } = this._parsedConfig.declarationKind;
793+
// `node.fields` are already printed to strings by the visitor; dropping `as string[]` needs a visited-AST type for `node`
790794
const allFields = [
791795
...(this.config.addTypename
792796
? [
@@ -797,7 +801,7 @@ export class BaseTypesVisitor<
797801
),
798802
]
799803
: []),
800-
...node.fields,
804+
...(node.fields || []),
801805
] as string[];
802806
const interfacesNames = originalNode.interfaces
803807
? originalNode.interfaces.map(i => this.convertName(i))
@@ -857,7 +861,7 @@ export class BaseTypesVisitor<
857861
.withName(this.convertName(node))
858862
.withComment(node.description?.value);
859863

860-
return declarationBlock.withBlock(node.fields.join('\n'));
864+
return declarationBlock.withBlock((node.fields || []).join('\n'));
861865
}
862866

863867
InterfaceTypeDefinition(
@@ -905,11 +909,11 @@ export class BaseTypesVisitor<
905909
useTypesSuffix: this.config.enumSuffix,
906910
}),
907911
)
908-
.withComment(node.description.value)
912+
.withComment(node.description?.value)
909913
.withBlock(
910914
buildEnumValuesBlock({
911915
typeName: enumName,
912-
values: node.values,
916+
values: node.values || [],
913917
schema: this._schema,
914918
naming: {
915919
convert: this.config.convert,
@@ -960,9 +964,11 @@ export class BaseTypesVisitor<
960964
return this.getArgumentsObjectDeclarationBlock(node, name, field).string;
961965
}
962966

963-
protected buildArgumentsBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode) {
967+
protected buildArgumentsBlock(
968+
node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode,
969+
): string {
964970
const fieldsWithArguments =
965-
node.fields.filter(field => field.arguments && field.arguments.length > 0) || [];
971+
node.fields?.filter(field => field.arguments && field.arguments.length > 0) || [];
966972
return fieldsWithArguments
967973
.map(field => {
968974
const name =

0 commit comments

Comments
 (0)