Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/large-glasses-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': major
'@graphql-codegen/typescript': major
---

Remove NameNode override
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import {
Kind,
ListTypeNode,
NamedTypeNode,
NameNode,
NonNullTypeNode,
ObjectTypeDefinitionNode,
ScalarTypeDefinitionNode,
StringValueNode,
UnionTypeDefinitionNode,
} from 'graphql';
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor.js';
Expand Down Expand Up @@ -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'));
}

Expand All @@ -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;
}

Expand All @@ -688,19 +685,15 @@ 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;
if (node.directives && this.config.directiveArgumentAndInputFieldMappings) {
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 {
Expand All @@ -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 {
Expand All @@ -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;
}

Expand All @@ -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)}`
),
]
: []),
Expand All @@ -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) {
Expand Down Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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];
Expand Down Expand Up @@ -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<InputValueDefinitionNode>(field.arguments));
}

Expand Down Expand Up @@ -998,7 +986,7 @@ export class BaseTypesVisitor<
protected _getDirectiveOverrideType(directives: ReadonlyArray<DirectiveNode>): 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);
}
Expand All @@ -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');
Expand All @@ -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, {
Expand Down Expand Up @@ -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}`;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
35 changes: 17 additions & 18 deletions packages/plugins/typescript/typescript/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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("");
}
Expand All @@ -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)}`
)
Expand Down Expand Up @@ -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)}`;
};
Expand All @@ -336,8 +336,7 @@ export class TsVisitor<
}
const fieldParts: Array<string> = [];
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;
}
Expand All @@ -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) {
Expand All @@ -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));
})
Expand All @@ -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,
Expand All @@ -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 => {
Expand All @@ -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)}`);
Expand All @@ -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;
}

Expand Down
Loading