Skip to content

Commit c360573

Browse files
authored
Allow to disable Input suffix (#2039)
### 📝 Description We are migrating our schema from a legacy Graphql service to a new on, which is based on graphql-kotlin. We have some input types that do not have `Input` suffix and want to migrate those types without breaking the schema. For this to work in a backwards compatible way , I'm suggesting adding new annotation -`@GraphQLSkipInputSuffix`, when it is added on input type - schema generator does not add the suffix @samuelAndalon
1 parent 1c58a46 commit c360573

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2024 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.generator.annotations
18+
19+
/**
20+
* Do not add "Input" Suffix for input types.
21+
*/
22+
@Target(AnnotationTarget.CLASS)
23+
annotation class GraphQLSkipInputSuffix

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.expediagroup.graphql.generator.internal.extensions
1818

19+
import com.expediagroup.graphql.generator.annotations.GraphQLSkipInputSuffix
1920
import com.expediagroup.graphql.generator.exceptions.CouldNotGetNameOfKClassException
2021
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
2122
import com.expediagroup.graphql.generator.internal.filters.functionFilters
@@ -28,6 +29,7 @@ import kotlin.reflect.KProperty
2829
import kotlin.reflect.KVisibility
2930
import kotlin.reflect.full.declaredMemberFunctions
3031
import kotlin.reflect.full.declaredMemberProperties
32+
import kotlin.reflect.full.findAnnotation
3133
import kotlin.reflect.full.findParameterByName
3234
import kotlin.reflect.full.isSubclassOf
3335
import kotlin.reflect.full.memberFunctions
@@ -84,12 +86,13 @@ internal fun KClass<*>.isListType(isDirective: Boolean = false): Boolean = this.
8486

8587
@Throws(CouldNotGetNameOfKClassException::class)
8688
internal fun KClass<*>.getSimpleName(isInputClass: Boolean = false): String {
89+
val skipSuffix = this.findAnnotation<GraphQLSkipInputSuffix>() != null
8790
val name = this.getGraphQLName()
8891
?: this.simpleName
8992
?: throw CouldNotGetNameOfKClassException(this)
9093

9194
return when {
92-
isInputClass -> if (name.endsWith(INPUT_SUFFIX, true)) name else "$name$INPUT_SUFFIX"
95+
isInputClass && !skipSuffix -> if (name.endsWith(INPUT_SUFFIX, true)) name else "$name$INPUT_SUFFIX"
9396
else -> name
9497
}
9598
}

generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.expediagroup.graphql.generator.internal.extensions
1818

1919
import com.expediagroup.graphql.generator.annotations.GraphQLIgnore
2020
import com.expediagroup.graphql.generator.annotations.GraphQLName
21+
import com.expediagroup.graphql.generator.annotations.GraphQLSkipInputSuffix
2122
import com.expediagroup.graphql.generator.annotations.GraphQLUnion
2223
import com.expediagroup.graphql.generator.exceptions.CouldNotGetNameOfKClassException
2324
import com.expediagroup.graphql.generator.hooks.NoopSchemaGeneratorHooks
@@ -74,6 +75,9 @@ open class KClassExtensionsTest {
7475

7576
class MyClassInput
7677

78+
@GraphQLSkipInputSuffix
79+
class MyTestClassSkipSuffix
80+
7781
@GraphQLName("MyClassRenamedInput")
7882
class MyClassCustomNameInput
7983

@@ -330,6 +334,11 @@ open class KClassExtensionsTest {
330334
assertEquals("MyClassInput", MyClassInput::class.getSimpleName(true))
331335
}
332336

337+
@Test
338+
fun `test input class name with skipped suffix`() {
339+
assertEquals("MyTestClassSkipSuffix", MyTestClassSkipSuffix::class.getSimpleName(true))
340+
}
341+
333342
@Test
334343
fun `test input class name with GraphQLName`() {
335344
assertEquals("MyTestClassRenamedInput", MyTestClassCustomName::class.getSimpleName(true))

website/versioned_docs/version-8.x.x/schema-generator/writing-schemas/arguments.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ input WidgetInput {
6161
}
6262
```
6363

64+
If you want to disable this behaviour for one of your types, you can add `@GraphQLSkipInputSuffix` to your type.
65+
6466
Note that only fields are exposed in the input objects. Functions will only be available on the GraphQL output types.
6567

6668
:::caution

0 commit comments

Comments
 (0)