From dcede66ae9473499c46decd4d73d03110b04dade Mon Sep 17 00:00:00 2001 From: Dan Adajian Date: Tue, 19 Mar 2024 11:37:47 -0500 Subject: [PATCH] fix: avoid using @Deprecated on union type (#15) --- src/helpers/build-annotations.ts | 23 +++++++++++++++---- src/helpers/build-directive-annotations.ts | 15 ++++++++---- .../expected.kt | 23 ++++++++++++++++++- .../schema.graphql | 15 ++++++++++++ .../expected.kt | 10 +------- .../schema.graphql | 7 ------ 6 files changed, 67 insertions(+), 26 deletions(-) diff --git a/src/helpers/build-annotations.ts b/src/helpers/build-annotations.ts index bd02659..9f93d43 100644 --- a/src/helpers/build-annotations.ts +++ b/src/helpers/build-annotations.ts @@ -41,10 +41,13 @@ export function buildAnnotations({ }) { const description = inputDescription ?? definitionNode?.description?.value ?? ""; - const descriptionAnnotator = isDeprecatedDescription(description) + const descriptionAnnotator = isDeprecatedDescription( + description, + resolvedType, + ) ? "@Deprecated" : "@GraphQLDescription"; - const descriptionValue = isDeprecatedDescription(description) + const descriptionValue = isDeprecatedDescription(description, resolvedType) ? description.replace("DEPRECATED: ", "") : description; const trimmedDescription = trimDescription(descriptionValue); @@ -53,7 +56,12 @@ export function buildAnnotations({ : ""; const directiveAnnotations = definitionNode - ? buildDirectiveAnnotations(definitionNode, config, description) + ? buildDirectiveAnnotations( + definitionNode, + config, + description, + resolvedType, + ) : ""; const unionAnnotation = resolvedType?.unionAnnotation ? `@${resolvedType.unionAnnotation}\n` @@ -78,8 +86,13 @@ export function buildAnnotations({ ); } -export function isDeprecatedDescription(description?: string) { - return description?.startsWith("DEPRECATED: "); +export function isDeprecatedDescription( + description?: string, + resolvedType?: TypeMetadata, +) { + return ( + description?.startsWith("DEPRECATED: ") && !resolvedType?.unionAnnotation + ); } function trimDescription(description: string) { diff --git a/src/helpers/build-directive-annotations.ts b/src/helpers/build-directive-annotations.ts index c88b2f4..cda2d8b 100644 --- a/src/helpers/build-directive-annotations.ts +++ b/src/helpers/build-directive-annotations.ts @@ -14,11 +14,13 @@ limitations under the License. import { CodegenConfig } from "../plugin"; import { DefinitionNode, isDeprecatedDescription } from "./build-annotations"; import { getFederationDirectiveReplacement } from "./get-federation-directive-replacement"; +import { TypeMetadata } from "./build-type-metadata"; export function buildDirectiveAnnotations( incomingNode: DefinitionNode, config: CodegenConfig, description?: string, + resolvedType?: TypeMetadata, ) { const kind = incomingNode.kind; const directives = incomingNode.directives ?? []; @@ -30,12 +32,17 @@ export function buildDirectiveAnnotations( directiveName === "deprecated" && !isDeprecatedDescription(description) ) { - const deprecatedReason = directive.arguments?.find( + const deprecatedReasonNode = directive.arguments?.find( (arg) => arg.name.value === "reason", )?.value; - return `@Deprecated("${ - deprecatedReason?.kind === "StringValue" ? deprecatedReason.value : "" - }")\n`; + const deprecatedReason = + deprecatedReasonNode?.kind === "StringValue" + ? deprecatedReasonNode.value + : ""; + const descriptionAnnotator = resolvedType?.unionAnnotation + ? "@GraphQLDescription" + : "@Deprecated"; + return `${descriptionAnnotator}("${deprecatedReason}")\n`; } const federationReplacement = getFederationDirectiveReplacement(directive); diff --git a/test/unit/should_annotate_types_properly/expected.kt b/test/unit/should_annotate_types_properly/expected.kt index c5a584a..fb51de1 100644 --- a/test/unit/should_annotate_types_properly/expected.kt +++ b/test/unit/should_annotate_types_properly/expected.kt @@ -8,5 +8,26 @@ data class MyType( @GraphQLDescription("A description for email") val email: String? = null, @GraphQLDescription("A `weird` description for name") - val name: String? = null + val name: String? = null, + @Deprecated("Use something else instead") + val deprecated1: String? = null, + @Deprecated("") + val deprecated2: String? = null, + @Deprecated("Deprecated directive works too") + val deprecated3: String? = null, + @Deprecated("It only takes the first one") + val deprecated4: String? = null, + @MyUnion + @GraphQLDescription("DEPRECATED: It uses the GraphQLDescription annotation for union types") + val deprecated5: Any? = null, + @MyUnion + @GraphQLDescription("It uses the GraphQLDescription annotation for union types") + val deprecated6: Any? = null ) + +@GraphQLUnion( + name = "MyUnion", + possibleTypes = [MyType::class], + description = "" +) +annotation class MyUnion diff --git a/test/unit/should_annotate_types_properly/schema.graphql b/test/unit/should_annotate_types_properly/schema.graphql index f409607..5b2a22b 100644 --- a/test/unit/should_annotate_types_properly/schema.graphql +++ b/test/unit/should_annotate_types_properly/schema.graphql @@ -11,4 +11,19 @@ type MyType { A \`weird\` description for name """ name: String + "DEPRECATED: Use something else instead" + deprecated1: String + deprecated2: String @deprecated + deprecated3: String @deprecated(reason: "Deprecated directive works too") + "DEPRECATED: It only takes the first one" + deprecated4: String + @deprecated(reason: "when you have multiple deprecated annotations") + "DEPRECATED: It uses the GraphQLDescription annotation for union types" + deprecated5: MyUnion + deprecated6: MyUnion + @deprecated( + reason: "It uses the GraphQLDescription annotation for union types" + ) } + +union MyUnion = MyType diff --git a/test/unit/should_generate_input_types_properly/expected.kt b/test/unit/should_generate_input_types_properly/expected.kt index e729e6c..fd7a52c 100644 --- a/test/unit/should_generate_input_types_properly/expected.kt +++ b/test/unit/should_generate_input_types_properly/expected.kt @@ -7,13 +7,5 @@ data class MyInputType( val username: String? = null, @GraphQLDescription("A description for email") val email: String? = null, - val name: String? = null, - @Deprecated("Use something else instead") - val deprecated1: String? = null, - @Deprecated("") - val deprecated2: String? = null, - @Deprecated("Deprecated directive works too") - val deprecated3: String? = null, - @Deprecated("It only takes the first one") - val deprecated4: String? = null + val name: String? = null ) diff --git a/test/unit/should_generate_input_types_properly/schema.graphql b/test/unit/should_generate_input_types_properly/schema.graphql index 1cd6914..f5e3732 100644 --- a/test/unit/should_generate_input_types_properly/schema.graphql +++ b/test/unit/should_generate_input_types_properly/schema.graphql @@ -4,11 +4,4 @@ input MyInputType { "A description for email" email: String name: String - "DEPRECATED: Use something else instead" - deprecated1: String - deprecated2: String @deprecated - deprecated3: String @deprecated(reason: "Deprecated directive works too") - "DEPRECATED: It only takes the first one" - deprecated4: String - @deprecated(reason: "when you have multiple deprecated annotations") }