Skip to content

Commit 8babe46

Browse files
authored
[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 408e042 commit 8babe46

File tree

7 files changed

+358
-175
lines changed

7 files changed

+358
-175
lines changed

.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 & 68 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,
@@ -40,9 +38,11 @@ import {
4038
indent,
4139
isOneOfInputObjectType,
4240
transformComment,
41+
getNodeComment,
4342
wrapWithSingleQuotes,
4443
} from './utils.js';
4544
import { OperationVariablesToObject } from './variables-to-object.js';
45+
import { buildEnumValuesBlock } from './convert-schema-enum-to-declaration-block-string.js';
4646

4747
export interface ParsedTypesConfig extends ParsedConfig {
4848
enumValues: ParsedEnumValuesMap;
@@ -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
@@ -703,7 +701,7 @@ export class BaseTypesVisitor<
703701

704702
const typeString = node.type as any as string;
705703
const { type } = this._parsedConfig.declarationKind;
706-
const comment = this.getNodeComment(node);
704+
const comment = getNodeComment(node);
707705

708706
return comment + indent(`${node.name.value}: ${typeString}${this.getPunctuation(type)}`);
709707
}
@@ -885,7 +883,23 @@ export class BaseTypesVisitor<
885883
})
886884
)
887885
.withComment(node.description.value)
888-
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string;
886+
.withBlock(
887+
buildEnumValuesBlock({
888+
typeName: enumName,
889+
values: node.values,
890+
schema: this._schema,
891+
naming: {
892+
convert: this.config.convert,
893+
typesPrefix: this.config.typesPrefix,
894+
useTypesPrefix: this.config.enumPrefix,
895+
typesSuffix: this.config.typesSuffix,
896+
useTypesSuffix: this.config.enumSuffix,
897+
},
898+
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
899+
declarationBlockConfig: this._declarationBlockConfig,
900+
enumValues: this.config.enumValues,
901+
})
902+
).string;
889903
}
890904

891905
protected makeValidEnumIdentifier(identifier: string): string {
@@ -895,46 +909,6 @@ export class BaseTypesVisitor<
895909
return identifier;
896910
}
897911

898-
protected buildEnumValuesBlock(typeName: string, values: ReadonlyArray<EnumValueDefinitionNode>): string {
899-
const schemaEnumType: GraphQLEnumType | undefined = this._schema
900-
? (this._schema.getType(typeName) as GraphQLEnumType)
901-
: undefined;
902-
903-
return values
904-
.map(enumOption => {
905-
const optionName = this.makeValidEnumIdentifier(
906-
this.convertName(enumOption, {
907-
useTypesPrefix: false,
908-
// We can only strip out the underscores if the value contains other
909-
// characters. Otherwise we'll generate syntactically invalid code.
910-
transformUnderscore: !onlyUnderscoresPattern.test(enumOption.name.value),
911-
})
912-
);
913-
const comment = this.getNodeComment(enumOption);
914-
const schemaEnumValue =
915-
schemaEnumType && !this.config.ignoreEnumValuesFromSchema
916-
? schemaEnumType.getValue(enumOption.name.value).value
917-
: undefined;
918-
let enumValue: string | number =
919-
typeof schemaEnumValue === 'undefined' ? enumOption.name.value : schemaEnumValue;
920-
921-
if (typeof this.config.enumValues[typeName]?.mappedValues?.[enumValue] !== 'undefined') {
922-
enumValue = this.config.enumValues[typeName].mappedValues[enumValue];
923-
}
924-
925-
return (
926-
comment +
927-
indent(
928-
`${optionName}${this._declarationBlockConfig.enumNameValueSeparator} ${wrapWithSingleQuotes(
929-
enumValue,
930-
typeof schemaEnumValue !== 'undefined'
931-
)}`
932-
)
933-
);
934-
})
935-
.join(',\n');
936-
}
937-
938912
DirectiveDefinition(_node: DirectiveDefinitionNode): string {
939913
return '';
940914
}
@@ -1050,28 +1024,6 @@ export class BaseTypesVisitor<
10501024
return null;
10511025
}
10521026

1053-
getNodeComment(node: FieldDefinitionNode | EnumValueDefinitionNode | InputValueDefinitionNode): string {
1054-
let commentText = node.description?.value;
1055-
const deprecationDirective = node.directives.find(v => v.name.value === 'deprecated');
1056-
if (deprecationDirective) {
1057-
const deprecationReason = this.getDeprecationReason(deprecationDirective);
1058-
commentText = `${commentText ? `${commentText}\n` : ''}@deprecated ${deprecationReason}`;
1059-
}
1060-
const comment = transformComment(commentText, 1);
1061-
return comment;
1062-
}
1063-
1064-
protected getDeprecationReason(directive: DirectiveNode): string | void {
1065-
if (directive.name.value === 'deprecated') {
1066-
let reason = 'Field no longer supported';
1067-
const deprecatedReason = directive.arguments[0];
1068-
if (deprecatedReason && deprecatedReason.value.kind === Kind.STRING) {
1069-
reason = deprecatedReason.value.value;
1070-
}
1071-
return reason;
1072-
}
1073-
}
1074-
10751027
protected wrapWithListType(str: string): string {
10761028
return `Array<${str}>`;
10771029
}

0 commit comments

Comments
 (0)