Skip to content

Commit

Permalink
Allow to disable Input suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
pdambrauskas committed Oct 8, 2024
1 parent 1c58a46 commit dc2b72a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import kotlin.reflect.full.findAnnotation

internal fun KAnnotatedElement.getGraphQLDescription(): String? = this.findAnnotation<GraphQLDescription>()?.value

internal fun KAnnotatedElement.getGraphQLName(): String? = this.findAnnotation<GraphQLName>()?.value
internal fun KAnnotatedElement.getGraphQLNameOverride() = this.findAnnotation<GraphQLName>()

internal fun KAnnotatedElement.getGraphQLName(): String? = this.getGraphQLNameOverride()?.value

internal fun KAnnotatedElement.getDeprecationReason(): String? =
this.findAnnotation<Deprecated>()?.getReason()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ open class KClassExtensionsTest {

class MyClassInput

@GraphQLName("MyTestClassSkipSuffix", skipSuffix = true)
class MyTestClassSkipSuffix

@GraphQLName("MyClassRenamedInput")
class MyClassCustomNameInput

Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dc2b72a

Please sign in to comment.