Skip to content

Commit 491558b

Browse files
committed
[typescript-operations] Extract convertSchemaEnumToDeclarationBlockString and getNodeComment to visitor-plugin-common (#10520)
* Extract convertSchemaEnumToDeclarationBlockString and getNodeComment to visitor-plugin-common to re-use in next major version * Create major breaking changeset for @graphql-codegen/visitor-plugin-common * Fix naming * Replace buildEnumValuesBlock method in base-types-visitor with a utility version * Fix changeset * Fix import path * Fix getNodeComment import issue by moving to utils.ts
1 parent f70796c commit 491558b

7 files changed

Lines changed: 360 additions & 181 deletions

File tree

.changeset/proud-cougars-hear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': major
3+
---
4+
5+
BREAKING CHANGE: `@graphql-codegen/visitor-plugin-common`'s `base-types-visitor` no longer has `getNodeComment` or `buildEnumValuesBlock` method.

.changeset/silly-kiwis-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript': patch
3+
---
4+
5+
Extract utilities from base-type-visitor to be shared with other plugins later: convertSchemaEnumToDeclarationBlockString, getNodeComment

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

Lines changed: 20 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import {
22
DirectiveDefinitionNode,
33
DirectiveNode,
44
EnumTypeDefinitionNode,
5-
EnumValueDefinitionNode,
65
FieldDefinitionNode,
7-
GraphQLEnumType,
86
GraphQLSchema,
97
InputObjectTypeDefinitionNode,
108
InputValueDefinitionNode,
@@ -19,6 +17,7 @@ import {
1917
UnionTypeDefinitionNode,
2018
} from 'graphql';
2119
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor.js';
20+
import { buildEnumValuesBlock } from './convert-schema-enum-to-declaration-block-string.js';
2221
import { normalizeDeclarationKind } from './declaration-kinds.js';
2322
import { parseEnumValues } from './enum-values.js';
2423
import { transformDirectiveArgumentAndInputFieldMappings } from './mappers.js';
@@ -37,6 +36,7 @@ import {
3736
DeclarationBlock,
3837
DeclarationBlockConfig,
3938
getConfigValue,
39+
getNodeComment,
4040
indent,
4141
isOneOfInputObjectType,
4242
transformComment,
@@ -493,8 +493,6 @@ export interface RawTypesConfig extends RawConfig {
493493
directiveArgumentAndInputFieldMappingTypeSuffix?: string;
494494
}
495495

496-
const onlyUnderscoresPattern = /^_+$/;
497-
498496
export class BaseTypesVisitor<
499497
TRawConfig extends RawTypesConfig = RawTypesConfig,
500498
TPluginConfig extends ParsedTypesConfig = ParsedTypesConfig,
@@ -723,7 +721,7 @@ export class BaseTypesVisitor<
723721

724722
const typeString = node.type as any as string;
725723
const { type } = this._parsedConfig.declarationKind;
726-
const comment = this.getNodeComment(node);
724+
const comment = getNodeComment(node);
727725

728726
return comment + indent(`${node.name.value}: ${typeString}${this.getPunctuation(type)}`);
729727
}
@@ -932,7 +930,23 @@ export class BaseTypesVisitor<
932930
}),
933931
)
934932
.withComment(node.description.value)
935-
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string;
933+
.withBlock(
934+
buildEnumValuesBlock({
935+
typeName: enumName,
936+
values: node.values,
937+
schema: this._schema,
938+
naming: {
939+
convert: this.config.convert,
940+
typesPrefix: this.config.typesPrefix,
941+
useTypesPrefix: this.config.enumPrefix,
942+
typesSuffix: this.config.typesSuffix,
943+
useTypesSuffix: this.config.enumSuffix,
944+
},
945+
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
946+
declarationBlockConfig: this._declarationBlockConfig,
947+
enumValues: this.config.enumValues,
948+
}),
949+
).string;
936950
}
937951

938952
protected makeValidEnumIdentifier(identifier: string): string {
@@ -942,48 +956,6 @@ export class BaseTypesVisitor<
942956
return identifier;
943957
}
944958

945-
protected buildEnumValuesBlock(
946-
typeName: string,
947-
values: ReadonlyArray<EnumValueDefinitionNode>,
948-
): string {
949-
const schemaEnumType: GraphQLEnumType | undefined = this._schema
950-
? (this._schema.getType(typeName) as GraphQLEnumType)
951-
: undefined;
952-
953-
return values
954-
.map(enumOption => {
955-
const optionName = this.makeValidEnumIdentifier(
956-
this.convertName(enumOption, {
957-
useTypesPrefix: false,
958-
// We can only strip out the underscores if the value contains other
959-
// characters. Otherwise we'll generate syntactically invalid code.
960-
transformUnderscore: !onlyUnderscoresPattern.test(enumOption.name.value),
961-
}),
962-
);
963-
const comment = this.getNodeComment(enumOption);
964-
const schemaEnumValue =
965-
schemaEnumType && !this.config.ignoreEnumValuesFromSchema
966-
? schemaEnumType.getValue(enumOption.name.value).value
967-
: undefined;
968-
let enumValue: string | number =
969-
typeof schemaEnumValue === 'undefined' ? enumOption.name.value : schemaEnumValue;
970-
971-
if (typeof this.config.enumValues[typeName]?.mappedValues?.[enumValue] !== 'undefined') {
972-
enumValue = this.config.enumValues[typeName].mappedValues[enumValue];
973-
}
974-
975-
return (
976-
comment +
977-
indent(
978-
`${optionName}${
979-
this._declarationBlockConfig.enumNameValueSeparator
980-
} ${wrapWithSingleQuotes(enumValue, typeof schemaEnumValue !== 'undefined')}`,
981-
)
982-
);
983-
})
984-
.join(',\n');
985-
}
986-
987959
DirectiveDefinition(_node: DirectiveDefinitionNode): string {
988960
return '';
989961
}
@@ -1100,30 +1072,6 @@ export class BaseTypesVisitor<
11001072
return null;
11011073
}
11021074

1103-
getNodeComment(
1104-
node: FieldDefinitionNode | EnumValueDefinitionNode | InputValueDefinitionNode,
1105-
): string {
1106-
let commentText = node.description?.value;
1107-
const deprecationDirective = node.directives.find(v => v.name.value === 'deprecated');
1108-
if (deprecationDirective) {
1109-
const deprecationReason = this.getDeprecationReason(deprecationDirective);
1110-
commentText = `${commentText ? `${commentText}\n` : ''}@deprecated ${deprecationReason}`;
1111-
}
1112-
const comment = transformComment(commentText, 1);
1113-
return comment;
1114-
}
1115-
1116-
protected getDeprecationReason(directive: DirectiveNode): string | void {
1117-
if (directive.name.value === 'deprecated') {
1118-
let reason = 'Field no longer supported';
1119-
const deprecatedReason = directive.arguments[0];
1120-
if (deprecatedReason && deprecatedReason.value.kind === Kind.STRING) {
1121-
reason = deprecatedReason.value.value;
1122-
}
1123-
return reason;
1124-
}
1125-
}
1126-
11271075
protected wrapWithListType(str: string): string {
11281076
return `Array<${str}>`;
11291077
}

0 commit comments

Comments
 (0)