Skip to content

Commit 54718c0

Browse files
Improve @deprecated Enum Type developer experience (#7628)
* Add failing test for enum value deprecated support * Make deprecation comment function usable for both field definition and enum value * Update deprecation comment function to handle both field definitions and enum values * Remove unnecessary console log * Create silver-mice-rule.md * Update name from getDeprecationComment to getNodeComment Co-authored-by: Tom Bailey <tbailey.ni> Co-authored-by: Charly POLY <1252066+charlypoly@users.noreply.github.com>
1 parent 2096f47 commit 54718c0

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

‎.changeset/silver-mice-rule.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphql-codegen/typescript": patch
3+
"@graphql-codegen/visitor-plugin-common": patch
4+
---
5+
6+
Improve @Deprecated Enum Type developer experience

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ export class BaseTypesVisitor<
456456
FieldDefinition(node: FieldDefinitionNode): string {
457457
const typeString = node.type as any as string;
458458
const { type } = this._parsedConfig.declarationKind;
459-
const comment = this.getFieldComment(node);
459+
const comment = this.getNodeComment(node);
460460

461461
return comment + indent(`${node.name}: ${typeString}${this.getPunctuation(type)}`);
462462
}
@@ -526,17 +526,6 @@ export class BaseTypesVisitor<
526526
return declarationBlock;
527527
}
528528

529-
getFieldComment(node: FieldDefinitionNode): string {
530-
let commentText: string = node.description as any;
531-
const deprecationDirective = node.directives.find((v: any) => v.name === 'deprecated');
532-
if (deprecationDirective) {
533-
const deprecationReason = this.getDeprecationReason(deprecationDirective);
534-
commentText = `${commentText ? `${commentText}\n` : ''}@deprecated ${deprecationReason}`;
535-
}
536-
const comment = transformComment(commentText, 1);
537-
return comment;
538-
}
539-
540529
protected mergeAllFields(allFields: string[], _hasInterfaces: boolean): string {
541530
return allFields.join('\n');
542531
}
@@ -664,7 +653,7 @@ export class BaseTypesVisitor<
664653
const optionName = this.makeValidEnumIdentifier(
665654
this.convertName(enumOption, { useTypesPrefix: false, transformUnderscore: true })
666655
);
667-
const comment = transformComment(enumOption.description as any as string, 1);
656+
const comment = this.getNodeComment(enumOption);
668657
const schemaEnumValue =
669658
schemaEnumType && !this.config.ignoreEnumValuesFromSchema
670659
? schemaEnumType.getValue(enumOption.name as any).value
@@ -799,6 +788,17 @@ export class BaseTypesVisitor<
799788
return null;
800789
}
801790

791+
getNodeComment(node: FieldDefinitionNode | EnumValueDefinitionNode): string {
792+
let commentText: string = node.description as any;
793+
const deprecationDirective = node.directives.find((v: any) => v.name === 'deprecated');
794+
if (deprecationDirective) {
795+
const deprecationReason = this.getDeprecationReason(deprecationDirective);
796+
commentText = `${commentText ? `${commentText}\n` : ''}@deprecated ${deprecationReason}`;
797+
}
798+
const comment = transformComment(commentText, 1);
799+
return comment;
800+
}
801+
802802
protected getDeprecationReason(directive: DirectiveNode): string | void {
803803
if ((directive.name as any) === 'deprecated') {
804804
const hasArguments = directive.arguments.length > 0;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export class TsVisitor<
258258
: (node.type as any as string);
259259
const originalFieldNode = parent[key] as FieldDefinitionNode;
260260
const addOptionalSign = !this.config.avoidOptionals.field && originalFieldNode.type.kind !== Kind.NON_NULL_TYPE;
261-
const comment = this.getFieldComment(node);
261+
const comment = this.getNodeComment(node);
262262
const { type } = this.config.declarationKind;
263263

264264
return (

‎packages/plugins/typescript/typescript/tests/typescript.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,23 @@ describe('TypeScript', () => {
937937
validateTs(result);
938938
});
939939

940+
it('#7627 - enum value @deprecated directive support', async () => {
941+
const schema = buildSchema(`
942+
enum MyEnum {
943+
A
944+
B @deprecated(reason: "Enum value \`B\` has been deprecated.")
945+
}`);
946+
947+
const result = await plugin(schema, [], {}, { outputFile: '' });
948+
expect(result.content).toBeSimilarStringTo(`
949+
export enum MyEnum {
950+
A = 'A',
951+
/** @deprecated Enum value \`B\` has been deprecated. */
952+
B = 'B'
953+
}`);
954+
validateTs(result);
955+
});
956+
940957
it('#1462 - Union of scalars and argument of directive', async () => {
941958
const schema = buildSchema(`
942959
union Any = String | Int | Float | ID

0 commit comments

Comments
 (0)