Skip to content

Commit 0cca1e4

Browse files
feat(generator): avoid adding input-suffix for input-only classes (ExpediaGroup#1434)
1 parent f25d8e3 commit 0cca1e4

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

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

Lines changed: 14 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.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
@@ -89,11 +91,22 @@ internal fun KClass<*>.getSimpleName(isInputClass: Boolean = false): String {
8991
?: throw CouldNotGetNameOfKClassException(this)
9092

9193
return when {
92-
isInputClass -> if (name.endsWith(INPUT_SUFFIX, true)) name else "$name$INPUT_SUFFIX"
94+
isInputClass -> {
95+
if (name.endsWith(INPUT_SUFFIX, true) || this.isInputOnlyLocationRestricted()) {
96+
name
97+
} else {
98+
"$name$INPUT_SUFFIX"
99+
}
100+
}
93101
else -> name
94102
}
95103
}
96104

105+
private fun KClass<*>.isInputOnlyLocationRestricted(): Boolean = findAnnotation<GraphQLValidObjectLocations>()
106+
?.locations
107+
?.all { it == GraphQLValidObjectLocations.Locations.INPUT_OBJECT }
108+
?: false
109+
97110
internal fun KClass<*>.getQualifiedName(): String = this.qualifiedName.orEmpty()
98111

99112
internal fun KClass<*>.isPublic(): Boolean = this.visibility == KVisibility.PUBLIC

generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/types/GenerateInputObjectTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class GenerateInputObjectTest : TypeTestHelper() {
5050
val myField: String = "car"
5151
}
5252

53+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT, GraphQLValidObjectLocations.Locations.OBJECT])
54+
class InputAndObjectLocation {
55+
val myField: String = "car"
56+
}
57+
5358
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
5459
class OutputOnly {
5560
val myField: String = "car"
@@ -105,6 +110,18 @@ class GenerateInputObjectTest : TypeTestHelper() {
105110
}
106111
}
107112

113+
@Test
114+
fun `input only objects are not suffixed with 'Input'`() {
115+
val result = generateInputObject(generator, InputOnly::class)
116+
assertEquals("InputOnly", result.name)
117+
}
118+
119+
@Test
120+
fun `input and object located objects are suffixed with 'Input'`() {
121+
val result = generateInputObject(generator, InputAndObjectLocation::class)
122+
assertEquals("InputAndObjectLocationInput", result.name)
123+
}
124+
108125
@Test
109126
fun `output only objects throw an exception`() {
110127
assertFailsWith(InvalidObjectLocationException::class) {

website/docs/schema-generator/writing-schemas/arguments.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ This behavior is true for all arguments except for the special classes for the [
2525
Query, Mutation, and Subscription function arguments are automatically converted to GraphQL input fields. GraphQL makes a
2626
distinction between input and output types and requires unique names for all the types. Since we can use the same
2727
objects for input and output in our Kotlin functions, `graphql-kotlin-schema-generator` will automatically append
28-
an `Input` suffix to the GraphQL name of input objects.
28+
an `Input` suffix to the GraphQL name of input objects, unless the type is [restricted to being used for
29+
input only](../customizing-schemas/restricting-input-output.md).
2930

3031
For example, the following code:
3132

0 commit comments

Comments
 (0)