From 44064cfea1d362d7c83648f3c34ed616e7dfcade Mon Sep 17 00:00:00 2001 From: Dan Adajian Date: Mon, 1 Apr 2024 07:27:11 -0500 Subject: [PATCH] fix: trim union descriptions (#21) --- src/definitions/union.ts | 3 ++- src/helpers/build-annotations.ts | 2 +- test/unit/should_generate_union_types_properly/expected.kt | 2 +- test/unit/should_generate_union_types_properly/schema.graphql | 4 +++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/definitions/union.ts b/src/definitions/union.ts index bb77973..e0a98f1 100644 --- a/src/definitions/union.ts +++ b/src/definitions/union.ts @@ -15,6 +15,7 @@ import { UnionTypeDefinitionNode } from "graphql"; import { shouldIncludeTypeDefinition } from "../helpers/should-include-type-definition"; import { buildDirectiveAnnotations } from "../helpers/build-directive-annotations"; import { CodegenConfig } from "../plugin"; +import { trimDescription } from "../helpers/build-annotations"; export function buildUnionTypeDefinition( node: UnionTypeDefinitionNode, @@ -30,7 +31,7 @@ export function buildUnionTypeDefinition( return `${directiveAnnotations}@GraphQLUnion( name = "${node.name.value}", possibleTypes = [${possibleTypes}], - description = "${node.description?.value ?? ""}" + description = "${node.description?.value ? trimDescription(node.description.value) : ""}" ) annotation class ${node.name.value}`; } diff --git a/src/helpers/build-annotations.ts b/src/helpers/build-annotations.ts index 9f93d43..0a0d12b 100644 --- a/src/helpers/build-annotations.ts +++ b/src/helpers/build-annotations.ts @@ -95,7 +95,7 @@ export function isDeprecatedDescription( ); } -function trimDescription(description: string) { +export function trimDescription(description: string) { return ( description .split("\n") diff --git a/test/unit/should_generate_union_types_properly/expected.kt b/test/unit/should_generate_union_types_properly/expected.kt index 80743a2..9ae03d0 100644 --- a/test/unit/should_generate_union_types_properly/expected.kt +++ b/test/unit/should_generate_union_types_properly/expected.kt @@ -14,7 +14,7 @@ data class MyType2( @GraphQLUnion( name = "MyUnion", possibleTypes = [MyType1::class, MyType2::class], - description = "A description for MyUnion" + description = "A trimmed description for MyUnion" ) annotation class MyUnion diff --git a/test/unit/should_generate_union_types_properly/schema.graphql b/test/unit/should_generate_union_types_properly/schema.graphql index 4725640..5a06f6f 100644 --- a/test/unit/should_generate_union_types_properly/schema.graphql +++ b/test/unit/should_generate_union_types_properly/schema.graphql @@ -7,7 +7,9 @@ type MyType2 { field: String } -"A description for MyUnion" +""" +A "trimmed" description for MyUnion +""" union MyUnion = MyType1 | MyType2 type MyUnionType {