Skip to content

Commit

Permalink
Use annotation instead of property
Browse files Browse the repository at this point in the history
  • Loading branch information
pdambrauskas committed Oct 3, 2024
1 parent f86a0a7 commit 3f160e1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 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, val skipSuffix: Boolean = false)
annotation class GraphQLName(val value: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2024 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.generator.annotations

/**
* Do not add "Input" Suffix for input types.
*/
@Target(AnnotationTarget.CLASS)
annotation class GraphQLSkipInputSuffix
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import kotlin.reflect.full.findAnnotation

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

internal fun KAnnotatedElement.getGraphQLNameOverride() = this.findAnnotation<GraphQLName>()

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

internal fun KAnnotatedElement.getDeprecationReason(): String? =
this.findAnnotation<Deprecated>()?.getReason()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import com.expediagroup.graphql.generator.annotations.GraphQLSkipInputSuffix
import com.expediagroup.graphql.generator.exceptions.CouldNotGetNameOfKClassException
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
import com.expediagroup.graphql.generator.internal.filters.functionFilters
Expand All @@ -28,6 +29,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,9 +86,8 @@ internal fun KClass<*>.isListType(isDirective: Boolean = false): Boolean = this.

@Throws(CouldNotGetNameOfKClassException::class)
internal fun KClass<*>.getSimpleName(isInputClass: Boolean = false): String {
val override = this.getGraphQLNameOverride()
val skipSuffix = override?.skipSuffix ?: false
val name = override?.value
val skipSuffix = this.findAnnotation<GraphQLSkipInputSuffix>() != null
val name = this.getGraphQLName()
?: this.simpleName
?: throw CouldNotGetNameOfKClassException(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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.GraphQLSkipInputSuffix
import com.expediagroup.graphql.generator.annotations.GraphQLUnion
import com.expediagroup.graphql.generator.exceptions.CouldNotGetNameOfKClassException
import com.expediagroup.graphql.generator.hooks.NoopSchemaGeneratorHooks
Expand Down Expand Up @@ -74,7 +75,7 @@ open class KClassExtensionsTest {

class MyClassInput

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

@GraphQLName("MyClassRenamedInput")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ input WidgetInput {
}
```

If you want to disable this behaviour for one of your types, you can add `@GraphQLName` annotation with `skipSuffix` set to `true`.
If you want to disable this behaviour for one of your types, you can add `@GraphQLSkipInputSuffix` to your type.

Note that only fields are exposed in the input objects. Functions will only be available on the GraphQL output types.

Expand Down

0 comments on commit 3f160e1

Please sign in to comment.