Skip to content

Commit 9ba9965

Browse files
feat: ability to specify @GraphQLName on input types w/o suffix (#1… (#1963)
Co-authored-by: Dariusz Kuc <9501705+dariuszkuc@users.noreply.github.com>
1 parent 7f8ff39 commit 9ba9965

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2024 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

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

19+
import com.expediagroup.graphql.generator.annotations.GraphQLValidObjectLocations
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,16 @@ 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 isInputOnlyLocation = this.findAnnotation<GraphQLValidObjectLocations>().let {
90+
it != null && it.locations.size == 1 && it.locations.contains(GraphQLValidObjectLocations.Locations.INPUT_OBJECT)
91+
}
92+
8793
val name = this.getGraphQLName()
8894
?: this.simpleName
8995
?: throw CouldNotGetNameOfKClassException(this)
9096

9197
return when {
92-
isInputClass -> if (name.endsWith(INPUT_SUFFIX, true)) name else "$name$INPUT_SUFFIX"
98+
isInputClass -> if (name.endsWith(INPUT_SUFFIX, true) || isInputOnlyLocation) name else "$name$INPUT_SUFFIX"
9399
else -> name
94100
}
95101
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2024 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package com.expediagroup.graphql.generator.internal.extensions
1919
import com.expediagroup.graphql.generator.annotations.GraphQLIgnore
2020
import com.expediagroup.graphql.generator.annotations.GraphQLName
2121
import com.expediagroup.graphql.generator.annotations.GraphQLUnion
22+
import com.expediagroup.graphql.generator.annotations.GraphQLValidObjectLocations
2223
import com.expediagroup.graphql.generator.exceptions.CouldNotGetNameOfKClassException
2324
import com.expediagroup.graphql.generator.hooks.NoopSchemaGeneratorHooks
2425
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
@@ -77,6 +78,13 @@ open class KClassExtensionsTest {
7778
@GraphQLName("MyClassRenamedInput")
7879
class MyClassCustomNameInput
7980

81+
@GraphQLValidObjectLocations([GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
82+
class MyInputClassWithoutSuffix
83+
84+
@GraphQLValidObjectLocations([GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
85+
@GraphQLName("MyClass")
86+
class MyInputClassWithoutSuffixUsingCustomName
87+
8088
protected class MyProtectedClass
8189

8290
class MyPublicClass
@@ -378,4 +386,10 @@ open class KClassExtensionsTest {
378386
assertFalse(IgnoredClass::class.isValidAdditionalType(true))
379387
assertFalse(IgnoredClass::class.isValidAdditionalType(false))
380388
}
389+
390+
@Test
391+
fun `@GraphQLName does not apply input suffix on input only classes`() {
392+
assertEquals("MyInputClassWithoutSuffix", MyInputClassWithoutSuffix::class.getSimpleName(isInputClass = true))
393+
assertEquals("MyClass", MyInputClassWithoutSuffixUsingCustomName::class.getSimpleName(isInputClass = true))
394+
}
381395
}

website/docs/schema-generator/customizing-schemas/renaming-fields.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ type MyCustomName {
2020
}
2121
```
2222

23+
:::info
24+
By default, in order to differentiate between input and output types, all input type names are auto generated with additional
25+
`Input` suffix. Suffix is appended only if input type name does not already end with `Input`. If you would like to change this
26+
behavior and avoid extra suffix, you need to explicitly specify that this is an input only type.
27+
28+
```kotlin
29+
// GraphQL input object type name: MyInputType
30+
@GraphQLValidObjectLocations([GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
31+
data class MyInputType(val id: ID)
32+
```
33+
34+
You can also rename input types
35+
36+
```kotlin
37+
// GraphQL input object type name: MyCustomInputType
38+
@GraphQLValidObjectLocations([GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
39+
@GraphQLName("MyCustomInputType")
40+
data class MyInputType(val id: ID)
41+
```
42+
:::
43+
2344
## Known Issues
2445

2546
Due to how we deserialize input classes, if you rename a field of an input class or an enum value you must also annotate

0 commit comments

Comments
 (0)