Skip to content

Commit

Permalink
BREAKING: ability to specify @GraphQLName on input types without su…
Browse files Browse the repository at this point in the history
…ffix

Currently input types are always suffixed with `Input` which is problematic as there are use cases where users might want to provide different custom name. This change adds check whether specified type is input only and if thats the case it does not attempt to add `Input` suffix.
  • Loading branch information
dariuszkuc committed Apr 17, 2024
1 parent 51a48c3 commit 1aa8462
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

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

import com.expediagroup.graphql.generator.annotations.GraphQLValidObjectLocations
import com.expediagroup.graphql.generator.exceptions.CouldNotGetNameOfKClassException
import com.expediagroup.graphql.generator.exceptions.InvalidObjectLocationException
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
import com.expediagroup.graphql.generator.internal.filters.functionFilters
import com.expediagroup.graphql.generator.internal.filters.propertyFilters
Expand All @@ -28,6 +30,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
Expand Down Expand Up @@ -84,12 +87,14 @@ internal fun KClass<*>.isListType(isDirective: Boolean = false): Boolean = this.

@Throws(CouldNotGetNameOfKClassException::class)
internal fun KClass<*>.getSimpleName(isInputClass: Boolean = false): String {
val isInputOnlyLocation = this.findAnnotation<GraphQLValidObjectLocations>().let { it != null && it.locations.size == 1 && it.locations.contains(GraphQLValidObjectLocations.Locations.INPUT_OBJECT) }

val name = this.getGraphQLName()
?: this.simpleName
?: throw CouldNotGetNameOfKClassException(this)

return when {
isInputClass -> if (name.endsWith(INPUT_SUFFIX, true)) name else "$name$INPUT_SUFFIX"
isInputClass -> if (name.endsWith(INPUT_SUFFIX, true) || isInputOnlyLocation) name else "$name$INPUT_SUFFIX"
else -> name
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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.GraphQLUnion
import com.expediagroup.graphql.generator.annotations.GraphQLValidObjectLocations
import com.expediagroup.graphql.generator.exceptions.CouldNotGetNameOfKClassException
import com.expediagroup.graphql.generator.hooks.NoopSchemaGeneratorHooks
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
Expand Down Expand Up @@ -77,6 +78,14 @@ open class KClassExtensionsTest {
@GraphQLName("MyClassRenamedInput")
class MyClassCustomNameInput

@GraphQLValidObjectLocations([GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
class MyInputClassWithoutSuffix

@GraphQLValidObjectLocations([GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
@GraphQLName("MyClass")
class MyInputClassWithoutSuffixUsingCustomName


protected class MyProtectedClass

class MyPublicClass
Expand Down Expand Up @@ -378,4 +387,10 @@ open class KClassExtensionsTest {
assertFalse(IgnoredClass::class.isValidAdditionalType(true))
assertFalse(IgnoredClass::class.isValidAdditionalType(false))
}

@Test
fun `@GraphQLName does not apply input suffix on input only classes`() {
assertEquals("MyInputClassWithoutSuffix", MyInputClassWithoutSuffix::class.getSimpleName(isInputClass = true))
assertEquals("MyClass", MyInputClassWithoutSuffixUsingCustomName::class.getSimpleName(isInputClass = true))
}
}

0 comments on commit 1aa8462

Please sign in to comment.