From dc2b72a70ce3c6b9950a2a7dafb451478f6c0599 Mon Sep 17 00:00:00 2001 From: Paulius Dambrauskas Date: Tue, 1 Oct 2024 12:08:54 +0300 Subject: [PATCH 1/3] Allow to disable Input suffix --- .../graphql/generator/annotations/GraphQLName.kt | 2 +- .../generator/internal/extensions/annotationExtensions.kt | 4 +++- .../generator/internal/extensions/kClassExtensions.kt | 6 ++++-- .../generator/internal/extensions/KClassExtensionsTest.kt | 8 ++++++++ .../schema-generator/writing-schemas/arguments.md | 2 ++ 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLName.kt b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLName.kt index 211190c39a..4b366322b7 100644 --- a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLName.kt +++ b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLName.kt @@ -20,4 +20,4 @@ package com.expediagroup.graphql.generator.annotations * Set the GraphQL name to be picked up by the schema generator. */ @Target(AnnotationTarget.CLASS, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION) -annotation class GraphQLName(val value: String) +annotation class GraphQLName(val value: String, val skipSuffix: Boolean = false) diff --git a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/annotationExtensions.kt b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/annotationExtensions.kt index 16fe367133..1587595252 100644 --- a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/annotationExtensions.kt +++ b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/annotationExtensions.kt @@ -28,7 +28,9 @@ import kotlin.reflect.full.findAnnotation internal fun KAnnotatedElement.getGraphQLDescription(): String? = this.findAnnotation()?.value -internal fun KAnnotatedElement.getGraphQLName(): String? = this.findAnnotation()?.value +internal fun KAnnotatedElement.getGraphQLNameOverride() = this.findAnnotation() + +internal fun KAnnotatedElement.getGraphQLName(): String? = this.getGraphQLNameOverride()?.value internal fun KAnnotatedElement.getDeprecationReason(): String? = this.findAnnotation()?.getReason() diff --git a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt index e8280831da..f2aa5df973 100644 --- a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt +++ b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt @@ -84,12 +84,14 @@ internal fun KClass<*>.isListType(isDirective: Boolean = false): Boolean = this. @Throws(CouldNotGetNameOfKClassException::class) internal fun KClass<*>.getSimpleName(isInputClass: Boolean = false): String { - val name = this.getGraphQLName() + val override = this.getGraphQLNameOverride() + val skipSuffix = override?.skipSuffix ?: false + val name = override?.value ?: this.simpleName ?: throw CouldNotGetNameOfKClassException(this) return when { - isInputClass -> if (name.endsWith(INPUT_SUFFIX, true)) name else "$name$INPUT_SUFFIX" + isInputClass && !skipSuffix -> if (name.endsWith(INPUT_SUFFIX, true)) name else "$name$INPUT_SUFFIX" else -> name } } diff --git a/generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt b/generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt index d1b70be74c..7975427d13 100644 --- a/generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt +++ b/generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt @@ -74,6 +74,9 @@ open class KClassExtensionsTest { class MyClassInput + @GraphQLName("MyTestClassSkipSuffix", skipSuffix = true) + class MyTestClassSkipSuffix + @GraphQLName("MyClassRenamedInput") class MyClassCustomNameInput @@ -330,6 +333,11 @@ open class KClassExtensionsTest { assertEquals("MyClassInput", MyClassInput::class.getSimpleName(true)) } + @Test + fun `test input class name with skipped suffix`() { + assertEquals("MyTestClassSkipSuffix", MyTestClassSkipSuffix::class.getSimpleName(true)) + } + @Test fun `test input class name with GraphQLName`() { assertEquals("MyTestClassRenamedInput", MyTestClassCustomName::class.getSimpleName(true)) diff --git a/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md b/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md index 175eb5f744..a09c2a4d4e 100644 --- a/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md +++ b/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md @@ -61,6 +61,8 @@ input WidgetInput { } ``` +If you want to disable this behaviour for one of your types, you can add `@GraphQLName` annotation with `skipSuffix` set to `true`. + Note that only fields are exposed in the input objects. Functions will only be available on the GraphQL output types. :::caution From 3edcefb91eb51ae6d5ef8371b6c77a9d1dd4ec6e Mon Sep 17 00:00:00 2001 From: Paulius Dambrauskas Date: Thu, 3 Oct 2024 08:59:29 +0300 Subject: [PATCH 2/3] Use annotation instead of property --- .../generator/annotations/GraphQLName.kt | 2 +- .../annotations/GraphQLSkipInputSuffix.kt | 23 +++++++++++++++++++ .../extensions/annotationExtensions.kt | 4 +--- .../internal/extensions/kClassExtensions.kt | 7 +++--- .../extensions/KClassExtensionsTest.kt | 3 ++- .../writing-schemas/arguments.md | 2 +- 6 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLSkipInputSuffix.kt diff --git a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLName.kt b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLName.kt index 4b366322b7..211190c39a 100644 --- a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLName.kt +++ b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLName.kt @@ -20,4 +20,4 @@ package com.expediagroup.graphql.generator.annotations * Set the GraphQL name to be picked up by the schema generator. */ @Target(AnnotationTarget.CLASS, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION) -annotation class GraphQLName(val value: String, val skipSuffix: Boolean = false) +annotation class GraphQLName(val value: String) diff --git a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLSkipInputSuffix.kt b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLSkipInputSuffix.kt new file mode 100644 index 0000000000..ade0d67ff6 --- /dev/null +++ b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/annotations/GraphQLSkipInputSuffix.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2024 Expedia, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.expediagroup.graphql.generator.annotations + +/** + * Do not add "Input" Suffix for input types. + */ +@Target(AnnotationTarget.CLASS) +annotation class GraphQLSkipInputSuffix diff --git a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/annotationExtensions.kt b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/annotationExtensions.kt index 1587595252..16fe367133 100644 --- a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/annotationExtensions.kt +++ b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/annotationExtensions.kt @@ -28,9 +28,7 @@ import kotlin.reflect.full.findAnnotation internal fun KAnnotatedElement.getGraphQLDescription(): String? = this.findAnnotation()?.value -internal fun KAnnotatedElement.getGraphQLNameOverride() = this.findAnnotation() - -internal fun KAnnotatedElement.getGraphQLName(): String? = this.getGraphQLNameOverride()?.value +internal fun KAnnotatedElement.getGraphQLName(): String? = this.findAnnotation()?.value internal fun KAnnotatedElement.getDeprecationReason(): String? = this.findAnnotation()?.getReason() diff --git a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt index f2aa5df973..824dfce4a7 100644 --- a/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt +++ b/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt @@ -16,6 +16,7 @@ package com.expediagroup.graphql.generator.internal.extensions +import com.expediagroup.graphql.generator.annotations.GraphQLSkipInputSuffix import com.expediagroup.graphql.generator.exceptions.CouldNotGetNameOfKClassException import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks import com.expediagroup.graphql.generator.internal.filters.functionFilters @@ -28,6 +29,7 @@ import kotlin.reflect.KProperty import kotlin.reflect.KVisibility import kotlin.reflect.full.declaredMemberFunctions import kotlin.reflect.full.declaredMemberProperties +import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.findParameterByName import kotlin.reflect.full.isSubclassOf import kotlin.reflect.full.memberFunctions @@ -84,9 +86,8 @@ internal fun KClass<*>.isListType(isDirective: Boolean = false): Boolean = this. @Throws(CouldNotGetNameOfKClassException::class) internal fun KClass<*>.getSimpleName(isInputClass: Boolean = false): String { - val override = this.getGraphQLNameOverride() - val skipSuffix = override?.skipSuffix ?: false - val name = override?.value + val skipSuffix = this.findAnnotation() != null + val name = this.getGraphQLName() ?: this.simpleName ?: throw CouldNotGetNameOfKClassException(this) diff --git a/generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt b/generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt index 7975427d13..5af571c180 100644 --- a/generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt +++ b/generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt @@ -18,6 +18,7 @@ package com.expediagroup.graphql.generator.internal.extensions import com.expediagroup.graphql.generator.annotations.GraphQLIgnore import com.expediagroup.graphql.generator.annotations.GraphQLName +import com.expediagroup.graphql.generator.annotations.GraphQLSkipInputSuffix import com.expediagroup.graphql.generator.annotations.GraphQLUnion import com.expediagroup.graphql.generator.exceptions.CouldNotGetNameOfKClassException import com.expediagroup.graphql.generator.hooks.NoopSchemaGeneratorHooks @@ -74,7 +75,7 @@ open class KClassExtensionsTest { class MyClassInput - @GraphQLName("MyTestClassSkipSuffix", skipSuffix = true) + @GraphQLSkipInputSuffix class MyTestClassSkipSuffix @GraphQLName("MyClassRenamedInput") diff --git a/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md b/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md index a09c2a4d4e..83ca4456fa 100644 --- a/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md +++ b/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md @@ -61,7 +61,7 @@ input WidgetInput { } ``` -If you want to disable this behaviour for one of your types, you can add `@GraphQLName` annotation with `skipSuffix` set to `true`. +If you want to disable this behaviour for one of your types, you can add `@GraphQLSkipInputSuffix` to your type. Note that only fields are exposed in the input objects. Functions will only be available on the GraphQL output types. From b1787ce652a08a47ed972e6a2cda70ffd2bbd628 Mon Sep 17 00:00:00 2001 From: Paulius Dambrauskas Date: Tue, 8 Oct 2024 09:44:53 +0300 Subject: [PATCH 3/3] Update docs for version 8 --- .../version-7.x.x/schema-generator/writing-schemas/arguments.md | 2 -- .../version-8.x.x/schema-generator/writing-schemas/arguments.md | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md b/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md index 83ca4456fa..175eb5f744 100644 --- a/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md +++ b/website/versioned_docs/version-7.x.x/schema-generator/writing-schemas/arguments.md @@ -61,8 +61,6 @@ input WidgetInput { } ``` -If you want to disable this behaviour for one of your types, you can add `@GraphQLSkipInputSuffix` to your type. - Note that only fields are exposed in the input objects. Functions will only be available on the GraphQL output types. :::caution diff --git a/website/versioned_docs/version-8.x.x/schema-generator/writing-schemas/arguments.md b/website/versioned_docs/version-8.x.x/schema-generator/writing-schemas/arguments.md index 175eb5f744..83ca4456fa 100644 --- a/website/versioned_docs/version-8.x.x/schema-generator/writing-schemas/arguments.md +++ b/website/versioned_docs/version-8.x.x/schema-generator/writing-schemas/arguments.md @@ -61,6 +61,8 @@ input WidgetInput { } ``` +If you want to disable this behaviour for one of your types, you can add `@GraphQLSkipInputSuffix` to your type. + Note that only fields are exposed in the input objects. Functions will only be available on the GraphQL output types. :::caution