Skip to content

Commit 78f7bfc

Browse files
authored
[typescript] Remove NameNode overrides (#10416)
* Avoid NameNode and StringValue string conversion * Add changeset
1 parent 939752c commit 78f7bfc

File tree

4 files changed

+51
-58
lines changed

4 files changed

+51
-58
lines changed

.changeset/large-glasses-jump.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': major
3+
'@graphql-codegen/typescript': major
4+
---
5+
6+
Remove NameNode override

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

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import {
1313
Kind,
1414
ListTypeNode,
1515
NamedTypeNode,
16-
NameNode,
1716
NonNullTypeNode,
1817
ObjectTypeDefinitionNode,
1918
ScalarTypeDefinitionNode,
20-
StringValueNode,
2119
UnionTypeDefinitionNode,
2220
} from 'graphql';
2321
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor.js';
@@ -658,7 +656,7 @@ export class BaseTypesVisitor<
658656
.export()
659657
.asKind(this._parsedConfig.declarationKind.input)
660658
.withName(this.convertName(node))
661-
.withComment(node.description as any as string)
659+
.withComment(node.description?.value)
662660
.withBlock(node.fields.join('\n'));
663661
}
664662

@@ -670,15 +668,14 @@ export class BaseTypesVisitor<
670668
.export()
671669
.asKind(declarationKind)
672670
.withName(this.convertName(node))
673-
.withComment(node.description as any as string)
671+
.withComment(node.description?.value)
674672
.withContent(`\n` + node.fields.join('\n |'));
675673
}
676674

677675
InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string {
678676
if (this.config.onlyEnums) return '';
679677

680-
// Why the heck is node.name a string and not { value: string } at runtime ?!
681-
if (isOneOfInputObjectType(this._schema.getType(node.name as unknown as string))) {
678+
if (isOneOfInputObjectType(this._schema.getType(node.name.value))) {
682679
return this.getInputObjectOneOfDeclarationBlock(node).string;
683680
}
684681

@@ -688,19 +685,15 @@ export class BaseTypesVisitor<
688685
InputValueDefinition(node: InputValueDefinitionNode): string {
689686
if (this.config.onlyEnums) return '';
690687

691-
const comment = transformComment(node.description as any as string, 1);
688+
const comment = transformComment(node.description.value, 1);
692689
const { input } = this._parsedConfig.declarationKind;
693690

694691
let type: string = node.type as any as string;
695692
if (node.directives && this.config.directiveArgumentAndInputFieldMappings) {
696693
type = this._getDirectiveOverrideType(node.directives) || type;
697694
}
698695

699-
return comment + indent(`${node.name}: ${type}${this.getPunctuation(input)}`);
700-
}
701-
702-
Name(node: NameNode): string {
703-
return node.value;
696+
return comment + indent(`${node.name.value}: ${type}${this.getPunctuation(input)}`);
704697
}
705698

706699
FieldDefinition(node: FieldDefinitionNode): string {
@@ -710,7 +703,7 @@ export class BaseTypesVisitor<
710703
const { type } = this._parsedConfig.declarationKind;
711704
const comment = this.getNodeComment(node);
712705

713-
return comment + indent(`${node.name}: ${typeString}${this.getPunctuation(type)}`);
706+
return comment + indent(`${node.name.value}: ${typeString}${this.getPunctuation(type)}`);
714707
}
715708

716709
UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string {
@@ -724,7 +717,7 @@ export class BaseTypesVisitor<
724717
.export()
725718
.asKind('type')
726719
.withName(this.convertName(node))
727-
.withComment(node.description as any as string)
720+
.withComment(node.description.value)
728721
.withContent(possibleTypes).string;
729722
}
730723

@@ -747,9 +740,9 @@ export class BaseTypesVisitor<
747740
...(this.config.addTypename
748741
? [
749742
indent(
750-
`${this.config.immutableTypes ? 'readonly ' : ''}${optionalTypename}: '${node.name}'${this.getPunctuation(
751-
type
752-
)}`
743+
`${this.config.immutableTypes ? 'readonly ' : ''}${optionalTypename}: '${
744+
node.name.value
745+
}'${this.getPunctuation(type)}`
753746
),
754747
]
755748
: []),
@@ -761,7 +754,7 @@ export class BaseTypesVisitor<
761754
.export()
762755
.asKind(type)
763756
.withName(this.convertName(node))
764-
.withComment(node.description as any as string);
757+
.withComment(node.description?.value);
765758

766759
if (type === 'interface' || type === 'class') {
767760
if (interfacesNames.length > 0) {
@@ -799,7 +792,7 @@ export class BaseTypesVisitor<
799792
.export()
800793
.asKind(this._parsedConfig.declarationKind.interface)
801794
.withName(this.convertName(node))
802-
.withComment(node.description as any as string);
795+
.withComment(node.description?.value);
803796

804797
return declarationBlock.withBlock(node.fields.join('\n'));
805798
}
@@ -873,7 +866,7 @@ export class BaseTypesVisitor<
873866
}
874867

875868
EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
876-
const enumName = node.name as any as string;
869+
const enumName = node.name.value;
877870

878871
// In case of mapped external enum string
879872
if (this.config.enumValues[enumName]?.sourceFile) {
@@ -889,15 +882,10 @@ export class BaseTypesVisitor<
889882
useTypesSuffix: this.config.enumSuffix,
890883
})
891884
)
892-
.withComment(node.description as any as string)
885+
.withComment(node.description.value)
893886
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string;
894887
}
895888

896-
// We are using it in order to transform "description" field
897-
StringValue(node: StringValueNode): string {
898-
return node.value;
899-
}
900-
901889
protected makeValidEnumIdentifier(identifier: string): string {
902890
if (/^[0-9]/.exec(identifier)) {
903891
return wrapWithSingleQuotes(identifier, true);
@@ -921,10 +909,10 @@ export class BaseTypesVisitor<
921909
const comment = this.getNodeComment(enumOption);
922910
const schemaEnumValue =
923911
schemaEnumType && !this.config.ignoreEnumValuesFromSchema
924-
? schemaEnumType.getValue(enumOption.name as any).value
912+
? schemaEnumType.getValue(enumOption.name.value).value
925913
: undefined;
926914
let enumValue: string | number =
927-
typeof schemaEnumValue === 'undefined' ? (enumOption.name as any) : schemaEnumValue;
915+
typeof schemaEnumValue === 'undefined' ? enumOption.name.value : schemaEnumValue;
928916

929917
if (typeof this.config.enumValues[typeName]?.mappedValues?.[enumValue] !== 'undefined') {
930918
enumValue = this.config.enumValues[typeName].mappedValues[enumValue];
@@ -956,7 +944,7 @@ export class BaseTypesVisitor<
956944
.export()
957945
.asKind(this._parsedConfig.declarationKind.arguments)
958946
.withName(this.convertName(name))
959-
.withComment(node.description)
947+
.withComment(node.description?.value)
960948
.withBlock(this._argumentsTransformer.transform<InputValueDefinitionNode>(field.arguments));
961949
}
962950

@@ -998,7 +986,7 @@ export class BaseTypesVisitor<
998986
protected _getDirectiveOverrideType(directives: ReadonlyArray<DirectiveNode>): string | null {
999987
const type = directives
1000988
.map(directive => {
1001-
const directiveName = directive.name as any as string;
989+
const directiveName = directive.name.value;
1002990
if (this.config.directiveArgumentAndInputFieldMappings[directiveName]) {
1003991
return this._getDirectiveArgumentNadInputFieldMapping(directiveName);
1004992
}
@@ -1011,7 +999,7 @@ export class BaseTypesVisitor<
1011999
}
10121000

10131001
protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string {
1014-
const typeAsString = node.name as any as string;
1002+
const typeAsString = node.name.value;
10151003

10161004
if (this.scalars[typeAsString]) {
10171005
return this._getScalar(typeAsString, isVisitingInputType ? 'input' : 'output');
@@ -1020,7 +1008,7 @@ export class BaseTypesVisitor<
10201008
return this.config.enumValues[typeAsString].typeIdentifier;
10211009
}
10221010

1023-
const schemaType = this._schema.getType(node.name as any);
1011+
const schemaType = this._schema.getType(typeAsString);
10241012

10251013
if (schemaType && isEnumType(schemaType)) {
10261014
return this.convertName(node, {
@@ -1055,8 +1043,8 @@ export class BaseTypesVisitor<
10551043
}
10561044

10571045
getNodeComment(node: FieldDefinitionNode | EnumValueDefinitionNode | InputValueDefinitionNode): string {
1058-
let commentText: string = node.description as any;
1059-
const deprecationDirective = node.directives.find((v: any) => v.name === 'deprecated');
1046+
let commentText = node.description?.value;
1047+
const deprecationDirective = node.directives.find(v => v.name.value === 'deprecated');
10601048
if (deprecationDirective) {
10611049
const deprecationReason = this.getDeprecationReason(deprecationDirective);
10621050
commentText = `${commentText ? `${commentText}\n` : ''}@deprecated ${deprecationReason}`;
@@ -1066,11 +1054,11 @@ export class BaseTypesVisitor<
10661054
}
10671055

10681056
protected getDeprecationReason(directive: DirectiveNode): string | void {
1069-
if ((directive.name as any) === 'deprecated') {
1070-
const hasArguments = directive.arguments.length > 0;
1057+
if (directive.name.value === 'deprecated') {
10711058
let reason = 'Field no longer supported';
1072-
if (hasArguments) {
1073-
reason = directive.arguments[0].value as any;
1059+
const deprecatedReason = directive.arguments[0];
1060+
if (deprecatedReason && deprecatedReason.value.kind === Kind.STRING) {
1061+
reason = deprecatedReason.value.value;
10741062
}
10751063
return reason;
10761064
}

packages/plugins/typescript/typescript/src/introspection-visitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class TsIntrospectionVisitor extends TsVisitor {
1818
}
1919

2020
ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: string | number, parent: any) {
21-
const name: string = node.name as any;
21+
const name: string = node.name.value;
2222

2323
if (this.typesToInclude.some(type => type.name === name)) {
2424
return super.ObjectTypeDefinition(node, key, parent);
@@ -28,7 +28,7 @@ export class TsIntrospectionVisitor extends TsVisitor {
2828
}
2929

3030
EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
31-
const name: string = node.name as any;
31+
const name: string = node.name.value;
3232

3333
if (this.typesToInclude.some(type => type.name === name)) {
3434
return super.EnumTypeDefinition(node);

packages/plugins/typescript/typescript/src/visitor.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class TsVisitor<
107107
}
108108

109109
protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string {
110-
const typeAsString = node.name as any as string;
110+
const typeAsString = node.name.value;
111111

112112
if (this.config.useImplementingTypes) {
113113
const allTypesMap = this._schema.getTypeMap();
@@ -130,7 +130,7 @@ export class TsVisitor<
130130
}
131131

132132
const typeString = super._getTypeForNode(node, isVisitingInputType);
133-
const schemaType = this._schema.getType(node.name as any as string);
133+
const schemaType = this._schema.getType(node.name.value);
134134

135135
if (isEnumType(schemaType)) {
136136
// futureProofEnums + enumsAsTypes combination adds the future value to the enum type itself
@@ -261,7 +261,7 @@ export class TsVisitor<
261261
.export()
262262
.asKind('type')
263263
.withName(this.convertName(node))
264-
.withComment(node.description as any as string)
264+
.withComment(node.description?.value)
265265
.withContent(possibleTypes).string;
266266
// return super.UnionTypeDefinition(node, key, parent).concat(withFutureAddedValue).join("");
267267
}
@@ -288,7 +288,7 @@ export class TsVisitor<
288288
return (
289289
comment +
290290
indent(
291-
`${this.config.immutableTypes ? 'readonly ' : ''}${node.name}${
291+
`${this.config.immutableTypes ? 'readonly ' : ''}${node.name.value}${
292292
addOptionalSign ? '?' : ''
293293
}: ${typeString}${this.getPunctuation(type)}`
294294
)
@@ -319,7 +319,7 @@ export class TsVisitor<
319319
const readonlyPrefix = this.config.immutableTypes ? 'readonly ' : '';
320320

321321
const buildFieldDefinition = (isOneOf = false) => {
322-
return `${readonlyPrefix}${node.name}${addOptionalSign && !isOneOf ? '?' : ''}: ${
322+
return `${readonlyPrefix}${node.name.value}${addOptionalSign && !isOneOf ? '?' : ''}: ${
323323
isOneOf ? this.clearOptional(type) : type
324324
}${this.getPunctuation(declarationKind)}`;
325325
};
@@ -336,8 +336,7 @@ export class TsVisitor<
336336
}
337337
const fieldParts: Array<string> = [];
338338
for (const fieldName of Object.keys(parentType.getFields())) {
339-
// Why the heck is node.name a string and not { value: string } at runtime ?!
340-
if (fieldName === (node.name as any as string)) {
339+
if (fieldName === node.name.value) {
341340
fieldParts.push(buildFieldDefinition(true));
342341
continue;
343342
}
@@ -351,7 +350,7 @@ export class TsVisitor<
351350
}
352351

353352
EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
354-
const enumName = node.name as any as string;
353+
const enumName = node.name.value;
355354

356355
// In case of mapped external enum string
357356
if (this.config.enumValues[enumName]?.sourceFile) {
@@ -378,15 +377,15 @@ export class TsVisitor<
378377
return new DeclarationBlock(this._declarationBlockConfig)
379378
.export()
380379
.asKind('type')
381-
.withComment(node.description as any as string)
380+
.withComment(node.description?.value)
382381
.withName(enumTypeName)
383382
.withContent(
384383
'\n' +
385384
node.values
386385
.map(enumOption => {
387-
const name = enumOption.name as unknown as string;
386+
const name = enumOption.name.value;
388387
const enumValue: string | number = getValueFromConfig(name) ?? name;
389-
const comment = transformComment(enumOption.description as any as string, 1);
388+
const comment = transformComment(enumOption.description?.value, 1);
390389

391390
return comment + indent('| ' + wrapWithSingleQuotes(enumValue));
392391
})
@@ -398,15 +397,15 @@ export class TsVisitor<
398397
if (this.config.numericEnums) {
399398
const block = new DeclarationBlock(this._declarationBlockConfig)
400399
.export()
401-
.withComment(node.description as any as string)
400+
.withComment(node.description?.value)
402401
.withName(enumTypeName)
403402
.asKind('enum')
404403
.withBlock(
405404
node.values
406405
.map((enumOption, i) => {
407-
const valueFromConfig = getValueFromConfig(enumOption.name as unknown as string);
406+
const valueFromConfig = getValueFromConfig(enumOption.name.value);
408407
const enumValue: string | number = valueFromConfig ?? i;
409-
const comment = transformComment(enumOption.description as any as string, 1);
408+
const comment = transformComment(enumOption.description?.value, 1);
410409
const optionName = this.makeValidEnumIdentifier(
411410
this.convertName(enumOption, {
412411
useTypesPrefix: false,
@@ -433,7 +432,7 @@ export class TsVisitor<
433432
.export()
434433
.asKind('const')
435434
.withName(enumTypeName)
436-
.withComment(node.description as any as string)
435+
.withComment(node.description?.value)
437436
.withBlock(
438437
node.values
439438
.map(enumOption => {
@@ -443,8 +442,8 @@ export class TsVisitor<
443442
transformUnderscore: true,
444443
})
445444
);
446-
const comment = transformComment(enumOption.description as any as string, 1);
447-
const name = enumOption.name as unknown as string;
445+
const comment = transformComment(enumOption.description?.value, 1);
446+
const name = enumOption.name.value;
448447
const enumValue: string | number = getValueFromConfig(name) ?? name;
449448

450449
return comment + indent(`${optionName}: ${wrapWithSingleQuotes(enumValue)}`);
@@ -459,7 +458,7 @@ export class TsVisitor<
459458
.export()
460459
.asKind(this.config.constEnums ? 'const enum' : 'enum')
461460
.withName(enumTypeName)
462-
.withComment(node.description as any as string)
461+
.withComment(node.description?.value)
463462
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string;
464463
}
465464

0 commit comments

Comments
 (0)