From 2ec420142cb50ad38cb633ced1b2683c7eb72b98 Mon Sep 17 00:00:00 2001 From: Eddy Nguyen Date: Wed, 27 Aug 2025 00:38:24 +1000 Subject: [PATCH 1/2] Avoid NameNode and StringValue string conversion --- .../src/base-types-visitor.ts | 64 ++++++++----------- .../typescript/src/introspection-visitor.ts | 4 +- .../typescript/typescript/src/visitor.ts | 35 +++++----- 3 files changed, 45 insertions(+), 58 deletions(-) diff --git a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts index 92e75e63bd5..5c0fdac5bd5 100644 --- a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts @@ -13,11 +13,9 @@ import { Kind, ListTypeNode, NamedTypeNode, - NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, - StringValueNode, UnionTypeDefinitionNode, } from 'graphql'; import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor.js'; @@ -658,7 +656,7 @@ export class BaseTypesVisitor< .export() .asKind(this._parsedConfig.declarationKind.input) .withName(this.convertName(node)) - .withComment(node.description as any as string) + .withComment(node.description?.value) .withBlock(node.fields.join('\n')); } @@ -670,15 +668,14 @@ export class BaseTypesVisitor< .export() .asKind(declarationKind) .withName(this.convertName(node)) - .withComment(node.description as any as string) + .withComment(node.description?.value) .withContent(`\n` + node.fields.join('\n |')); } InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string { if (this.config.onlyEnums) return ''; - // Why the heck is node.name a string and not { value: string } at runtime ?! - if (isOneOfInputObjectType(this._schema.getType(node.name as unknown as string))) { + if (isOneOfInputObjectType(this._schema.getType(node.name.value))) { return this.getInputObjectOneOfDeclarationBlock(node).string; } @@ -688,7 +685,7 @@ export class BaseTypesVisitor< InputValueDefinition(node: InputValueDefinitionNode): string { if (this.config.onlyEnums) return ''; - const comment = transformComment(node.description as any as string, 1); + const comment = transformComment(node.description.value, 1); const { input } = this._parsedConfig.declarationKind; let type: string = node.type as any as string; @@ -696,11 +693,7 @@ export class BaseTypesVisitor< type = this._getDirectiveOverrideType(node.directives) || type; } - return comment + indent(`${node.name}: ${type}${this.getPunctuation(input)}`); - } - - Name(node: NameNode): string { - return node.value; + return comment + indent(`${node.name.value}: ${type}${this.getPunctuation(input)}`); } FieldDefinition(node: FieldDefinitionNode): string { @@ -710,7 +703,7 @@ export class BaseTypesVisitor< const { type } = this._parsedConfig.declarationKind; const comment = this.getNodeComment(node); - return comment + indent(`${node.name}: ${typeString}${this.getPunctuation(type)}`); + return comment + indent(`${node.name.value}: ${typeString}${this.getPunctuation(type)}`); } UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string { @@ -724,7 +717,7 @@ export class BaseTypesVisitor< .export() .asKind('type') .withName(this.convertName(node)) - .withComment(node.description as any as string) + .withComment(node.description.value) .withContent(possibleTypes).string; } @@ -747,9 +740,9 @@ export class BaseTypesVisitor< ...(this.config.addTypename ? [ indent( - `${this.config.immutableTypes ? 'readonly ' : ''}${optionalTypename}: '${node.name}'${this.getPunctuation( - type - )}` + `${this.config.immutableTypes ? 'readonly ' : ''}${optionalTypename}: '${ + node.name.value + }'${this.getPunctuation(type)}` ), ] : []), @@ -761,7 +754,7 @@ export class BaseTypesVisitor< .export() .asKind(type) .withName(this.convertName(node)) - .withComment(node.description as any as string); + .withComment(node.description?.value); if (type === 'interface' || type === 'class') { if (interfacesNames.length > 0) { @@ -799,7 +792,7 @@ export class BaseTypesVisitor< .export() .asKind(this._parsedConfig.declarationKind.interface) .withName(this.convertName(node)) - .withComment(node.description as any as string); + .withComment(node.description?.value); return declarationBlock.withBlock(node.fields.join('\n')); } @@ -873,7 +866,7 @@ export class BaseTypesVisitor< } EnumTypeDefinition(node: EnumTypeDefinitionNode): string { - const enumName = node.name as any as string; + const enumName = node.name.value; // In case of mapped external enum string if (this.config.enumValues[enumName]?.sourceFile) { @@ -889,15 +882,10 @@ export class BaseTypesVisitor< useTypesSuffix: this.config.enumSuffix, }) ) - .withComment(node.description as any as string) + .withComment(node.description.value) .withBlock(this.buildEnumValuesBlock(enumName, node.values)).string; } - // We are using it in order to transform "description" field - StringValue(node: StringValueNode): string { - return node.value; - } - protected makeValidEnumIdentifier(identifier: string): string { if (/^[0-9]/.exec(identifier)) { return wrapWithSingleQuotes(identifier, true); @@ -921,10 +909,10 @@ export class BaseTypesVisitor< const comment = this.getNodeComment(enumOption); const schemaEnumValue = schemaEnumType && !this.config.ignoreEnumValuesFromSchema - ? schemaEnumType.getValue(enumOption.name as any).value + ? schemaEnumType.getValue(enumOption.name.value).value : undefined; let enumValue: string | number = - typeof schemaEnumValue === 'undefined' ? (enumOption.name as any) : schemaEnumValue; + typeof schemaEnumValue === 'undefined' ? enumOption.name.value : schemaEnumValue; if (typeof this.config.enumValues[typeName]?.mappedValues?.[enumValue] !== 'undefined') { enumValue = this.config.enumValues[typeName].mappedValues[enumValue]; @@ -956,7 +944,7 @@ export class BaseTypesVisitor< .export() .asKind(this._parsedConfig.declarationKind.arguments) .withName(this.convertName(name)) - .withComment(node.description) + .withComment(node.description?.value) .withBlock(this._argumentsTransformer.transform(field.arguments)); } @@ -998,7 +986,7 @@ export class BaseTypesVisitor< protected _getDirectiveOverrideType(directives: ReadonlyArray): string | null { const type = directives .map(directive => { - const directiveName = directive.name as any as string; + const directiveName = directive.name.value; if (this.config.directiveArgumentAndInputFieldMappings[directiveName]) { return this._getDirectiveArgumentNadInputFieldMapping(directiveName); } @@ -1011,7 +999,7 @@ export class BaseTypesVisitor< } protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string { - const typeAsString = node.name as any as string; + const typeAsString = node.name.value; if (this.scalars[typeAsString]) { return this._getScalar(typeAsString, isVisitingInputType ? 'input' : 'output'); @@ -1020,7 +1008,7 @@ export class BaseTypesVisitor< return this.config.enumValues[typeAsString].typeIdentifier; } - const schemaType = this._schema.getType(node.name as any); + const schemaType = this._schema.getType(typeAsString); if (schemaType && isEnumType(schemaType)) { return this.convertName(node, { @@ -1055,8 +1043,8 @@ export class BaseTypesVisitor< } getNodeComment(node: FieldDefinitionNode | EnumValueDefinitionNode | InputValueDefinitionNode): string { - let commentText: string = node.description as any; - const deprecationDirective = node.directives.find((v: any) => v.name === 'deprecated'); + let commentText = node.description?.value; + const deprecationDirective = node.directives.find(v => v.name.value === 'deprecated'); if (deprecationDirective) { const deprecationReason = this.getDeprecationReason(deprecationDirective); commentText = `${commentText ? `${commentText}\n` : ''}@deprecated ${deprecationReason}`; @@ -1066,11 +1054,11 @@ export class BaseTypesVisitor< } protected getDeprecationReason(directive: DirectiveNode): string | void { - if ((directive.name as any) === 'deprecated') { - const hasArguments = directive.arguments.length > 0; + if (directive.name.value === 'deprecated') { let reason = 'Field no longer supported'; - if (hasArguments) { - reason = directive.arguments[0].value as any; + const deprecatedReason = directive.arguments[0]; + if (deprecatedReason && deprecatedReason.value.kind === Kind.STRING) { + reason = deprecatedReason.value.value; } return reason; } diff --git a/packages/plugins/typescript/typescript/src/introspection-visitor.ts b/packages/plugins/typescript/typescript/src/introspection-visitor.ts index 691a87a6796..c88b763e311 100644 --- a/packages/plugins/typescript/typescript/src/introspection-visitor.ts +++ b/packages/plugins/typescript/typescript/src/introspection-visitor.ts @@ -18,7 +18,7 @@ export class TsIntrospectionVisitor extends TsVisitor { } ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: string | number, parent: any) { - const name: string = node.name as any; + const name: string = node.name.value; if (this.typesToInclude.some(type => type.name === name)) { return super.ObjectTypeDefinition(node, key, parent); @@ -28,7 +28,7 @@ export class TsIntrospectionVisitor extends TsVisitor { } EnumTypeDefinition(node: EnumTypeDefinitionNode): string { - const name: string = node.name as any; + const name: string = node.name.value; if (this.typesToInclude.some(type => type.name === name)) { return super.EnumTypeDefinition(node); diff --git a/packages/plugins/typescript/typescript/src/visitor.ts b/packages/plugins/typescript/typescript/src/visitor.ts index 55c4ebd33c9..d306c7156c0 100644 --- a/packages/plugins/typescript/typescript/src/visitor.ts +++ b/packages/plugins/typescript/typescript/src/visitor.ts @@ -107,7 +107,7 @@ export class TsVisitor< } protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string { - const typeAsString = node.name as any as string; + const typeAsString = node.name.value; if (this.config.useImplementingTypes) { const allTypesMap = this._schema.getTypeMap(); @@ -130,7 +130,7 @@ export class TsVisitor< } const typeString = super._getTypeForNode(node, isVisitingInputType); - const schemaType = this._schema.getType(node.name as any as string); + const schemaType = this._schema.getType(node.name.value); if (isEnumType(schemaType)) { // futureProofEnums + enumsAsTypes combination adds the future value to the enum type itself @@ -261,7 +261,7 @@ export class TsVisitor< .export() .asKind('type') .withName(this.convertName(node)) - .withComment(node.description as any as string) + .withComment(node.description?.value) .withContent(possibleTypes).string; // return super.UnionTypeDefinition(node, key, parent).concat(withFutureAddedValue).join(""); } @@ -288,7 +288,7 @@ export class TsVisitor< return ( comment + indent( - `${this.config.immutableTypes ? 'readonly ' : ''}${node.name}${ + `${this.config.immutableTypes ? 'readonly ' : ''}${node.name.value}${ addOptionalSign ? '?' : '' }: ${typeString}${this.getPunctuation(type)}` ) @@ -319,7 +319,7 @@ export class TsVisitor< const readonlyPrefix = this.config.immutableTypes ? 'readonly ' : ''; const buildFieldDefinition = (isOneOf = false) => { - return `${readonlyPrefix}${node.name}${addOptionalSign && !isOneOf ? '?' : ''}: ${ + return `${readonlyPrefix}${node.name.value}${addOptionalSign && !isOneOf ? '?' : ''}: ${ isOneOf ? this.clearOptional(type) : type }${this.getPunctuation(declarationKind)}`; }; @@ -336,8 +336,7 @@ export class TsVisitor< } const fieldParts: Array = []; for (const fieldName of Object.keys(parentType.getFields())) { - // Why the heck is node.name a string and not { value: string } at runtime ?! - if (fieldName === (node.name as any as string)) { + if (fieldName === node.name.value) { fieldParts.push(buildFieldDefinition(true)); continue; } @@ -351,7 +350,7 @@ export class TsVisitor< } EnumTypeDefinition(node: EnumTypeDefinitionNode): string { - const enumName = node.name as any as string; + const enumName = node.name.value; // In case of mapped external enum string if (this.config.enumValues[enumName]?.sourceFile) { @@ -378,15 +377,15 @@ export class TsVisitor< return new DeclarationBlock(this._declarationBlockConfig) .export() .asKind('type') - .withComment(node.description as any as string) + .withComment(node.description?.value) .withName(enumTypeName) .withContent( '\n' + node.values .map(enumOption => { - const name = enumOption.name as unknown as string; + const name = enumOption.name.value; const enumValue: string | number = getValueFromConfig(name) ?? name; - const comment = transformComment(enumOption.description as any as string, 1); + const comment = transformComment(enumOption.description?.value, 1); return comment + indent('| ' + wrapWithSingleQuotes(enumValue)); }) @@ -398,15 +397,15 @@ export class TsVisitor< if (this.config.numericEnums) { const block = new DeclarationBlock(this._declarationBlockConfig) .export() - .withComment(node.description as any as string) + .withComment(node.description?.value) .withName(enumTypeName) .asKind('enum') .withBlock( node.values .map((enumOption, i) => { - const valueFromConfig = getValueFromConfig(enumOption.name as unknown as string); + const valueFromConfig = getValueFromConfig(enumOption.name.value); const enumValue: string | number = valueFromConfig ?? i; - const comment = transformComment(enumOption.description as any as string, 1); + const comment = transformComment(enumOption.description?.value, 1); const optionName = this.makeValidEnumIdentifier( this.convertName(enumOption, { useTypesPrefix: false, @@ -433,7 +432,7 @@ export class TsVisitor< .export() .asKind('const') .withName(enumTypeName) - .withComment(node.description as any as string) + .withComment(node.description?.value) .withBlock( node.values .map(enumOption => { @@ -443,8 +442,8 @@ export class TsVisitor< transformUnderscore: true, }) ); - const comment = transformComment(enumOption.description as any as string, 1); - const name = enumOption.name as unknown as string; + const comment = transformComment(enumOption.description?.value, 1); + const name = enumOption.name.value; const enumValue: string | number = getValueFromConfig(name) ?? name; return comment + indent(`${optionName}: ${wrapWithSingleQuotes(enumValue)}`); @@ -459,7 +458,7 @@ export class TsVisitor< .export() .asKind(this.config.constEnums ? 'const enum' : 'enum') .withName(enumTypeName) - .withComment(node.description as any as string) + .withComment(node.description?.value) .withBlock(this.buildEnumValuesBlock(enumName, node.values)).string; } From 6adbb6d3332bd63dc54b7983e8190050784722b1 Mon Sep 17 00:00:00 2001 From: Eddy Nguyen Date: Wed, 27 Aug 2025 00:57:24 +1000 Subject: [PATCH 2/2] Add changeset --- .changeset/large-glasses-jump.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/large-glasses-jump.md diff --git a/.changeset/large-glasses-jump.md b/.changeset/large-glasses-jump.md new file mode 100644 index 00000000000..7329a64386a --- /dev/null +++ b/.changeset/large-glasses-jump.md @@ -0,0 +1,6 @@ +--- +'@graphql-codegen/visitor-plugin-common': major +'@graphql-codegen/typescript': major +--- + +Remove NameNode override