Skip to content

Commit 8e22d85

Browse files
committed
use new hook to verify whether directive argument value is correct
1 parent be8ff8f commit 8e22d85

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

generator/graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/generator/federation/FederatedSchemaGeneratorHooks.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.expediagroup.graphql.generator.TopLevelObject
2222
import com.expediagroup.graphql.generator.annotations.GraphQLName
2323
import com.expediagroup.graphql.generator.directives.DirectiveMetaInformation
2424
import com.expediagroup.graphql.generator.federation.directives.COMPOSE_DIRECTIVE_NAME
25+
import com.expediagroup.graphql.generator.federation.directives.CONTACT_DIRECTIVE_NAME
2526
import com.expediagroup.graphql.generator.federation.directives.EXTENDS_DIRECTIVE_NAME
2627
import com.expediagroup.graphql.generator.federation.directives.EXTENDS_DIRECTIVE_TYPE
2728
import com.expediagroup.graphql.generator.federation.directives.EXTERNAL_DIRECTIVE_NAME
@@ -64,13 +65,11 @@ import com.expediagroup.graphql.generator.federation.types._Service
6465
import com.expediagroup.graphql.generator.federation.types.generateEntityFieldDefinition
6566
import com.expediagroup.graphql.generator.federation.validation.FederatedSchemaValidator
6667
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
67-
import com.expediagroup.graphql.generator.internal.types.DEFAULT_DIRECTIVE_STRING_VALUE
6868
import graphql.TypeResolutionEnvironment
6969
import graphql.schema.DataFetcher
7070
import graphql.schema.FieldCoordinates
7171
import graphql.schema.GraphQLCodeRegistry
7272
import graphql.schema.GraphQLDirective
73-
import graphql.schema.GraphQLNamedOutputType
7473
import graphql.schema.GraphQLNamedType
7574
import graphql.schema.GraphQLObjectType
7675
import graphql.schema.GraphQLScalarType
@@ -150,15 +149,13 @@ open class FederatedSchemaGeneratorHooks(
150149
val appliedLinkDirectives = schemaObject?.kClass?.annotations?.filterIsInstance(LinkDirective::class.java)
151150
appliedLinkDirectives?.forEach { appliedDirectiveAnnotation ->
152151
val specUrl = Paths.get(appliedDirectiveAnnotation.url)
153-
// TODO verify supported version?
154-
val specVersion = specUrl.fileName.name
155152
val spec = specUrl.parent.fileName.name
156153

157154
if (linkSpecs.containsKey(spec)) {
158155
throw RuntimeException("Attempting to import same @link spec twice")
159156
} else {
160157
val nameSpace: String = appliedDirectiveAnnotation.`as`.takeIf {
161-
it.isNotBlank() && it != DEFAULT_DIRECTIVE_STRING_VALUE
158+
it.isNotBlank()
162159
} ?: spec
163160
val imports: Map<String, String> = appliedDirectiveAnnotation.import.associate {
164161
normalizeImportName(it.name) to normalizeImportName(it.`as`)
@@ -229,6 +226,16 @@ open class FederatedSchemaGeneratorHooks(
229226
else -> super.willGenerateDirective(directiveInfo)
230227
}
231228

229+
override fun isValidDirectiveArgumentValue(directiveName: String, argumentName: String, value: Any?): Boolean {
230+
if (directiveName == LINK_DIRECTIVE_NAME && argumentName == "as" && value.toString().isBlank()) {
231+
return false
232+
}
233+
if (directiveName == CONTACT_DIRECTIVE_NAME && (argumentName == "url" || argumentName == "description") && value.toString().isBlank()) {
234+
return false
235+
}
236+
return super.isValidDirectiveArgumentValue(directiveName, argumentName, value)
237+
}
238+
232239
override fun didGenerateGraphQLType(type: KType, generatedType: GraphQLType): GraphQLType {
233240
validator.validateGraphQLType(generatedType)
234241
return super.didGenerateGraphQLType(type, generatedType)

generator/graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/generator/federation/directives/ContactDirective.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.expediagroup.graphql.generator.federation.directives
1818

1919
import com.expediagroup.graphql.generator.annotations.GraphQLDirective
20-
import com.expediagroup.graphql.generator.internal.types.DEFAULT_DIRECTIVE_STRING_VALUE
2120
import graphql.introspection.Introspection.DirectiveLocation
2221

2322
/**
@@ -60,9 +59,9 @@ annotation class ContactDirective(
6059
/** Contact title of the subgraph owner */
6160
val name: String,
6261
/** URL where the subgraph's owner can be reached */
63-
val url: String = DEFAULT_DIRECTIVE_STRING_VALUE,
62+
val url: String = "",
6463
/** Other relevant notes can be included here; supports Markdown links */
65-
val description: String = DEFAULT_DIRECTIVE_STRING_VALUE
64+
val description: String = ""
6665
)
6766

6867
internal const val CONTACT_DIRECTIVE_NAME = "contact"

generator/graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/generator/federation/directives/LinkDirective.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.expediagroup.graphql.generator.federation.directives
1818

1919
import com.expediagroup.graphql.generator.annotations.GraphQLDirective
20-
import com.expediagroup.graphql.generator.internal.types.DEFAULT_DIRECTIVE_STRING_VALUE
2120
import graphql.Scalars
2221
import graphql.introspection.Introspection.DirectiveLocation
2322
import graphql.schema.GraphQLArgument
@@ -59,7 +58,7 @@ const val FEDERATION_LATEST_VERSION = "2.5"
5958
description = LINK_DIRECTIVE_DESCRIPTION,
6059
locations = [DirectiveLocation.SCHEMA]
6160
)
62-
annotation class LinkDirective(val url: String, val `as`: String = DEFAULT_DIRECTIVE_STRING_VALUE, val import: Array<LinkImport>)
61+
annotation class LinkDirective(val url: String, val `as`: String = "", val import: Array<LinkImport>)
6362

6463
internal const val LINK_DIRECTIVE_NAME = "link"
6564
private const val LINK_DIRECTIVE_DESCRIPTION = "Links definitions within the document to external schemas."

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/hooks/SchemaGeneratorHooks.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import com.expediagroup.graphql.generator.exceptions.EmptySubscriptionTypeExcept
2828
import com.expediagroup.graphql.generator.internal.extensions.isSubclassOf
2929
import com.expediagroup.graphql.generator.internal.extensions.isValidAdditionalType
3030
import graphql.schema.FieldCoordinates
31+
import graphql.schema.GraphQLAppliedDirective
32+
import graphql.schema.GraphQLArgument
3133
import graphql.schema.GraphQLCodeRegistry
3234
import graphql.schema.GraphQLDirective
3335
import graphql.schema.GraphQLFieldDefinition
@@ -39,6 +41,7 @@ import graphql.schema.GraphQLSchemaElement
3941
import graphql.schema.GraphQLType
4042
import graphql.schema.GraphQLTypeUtil
4143
import org.reactivestreams.Publisher
44+
import kotlin.reflect.KAnnotatedElement
4245
import kotlin.reflect.KClass
4346
import kotlin.reflect.KFunction
4447
import kotlin.reflect.KProperty
@@ -135,6 +138,11 @@ interface SchemaGeneratorHooks {
135138
*/
136139
fun isValidAdditionalType(kClass: KClass<*>, inputType: Boolean): Boolean = kClass.isValidAdditionalType(inputType)
137140

141+
/**
142+
* Called before setting applied directive argument value.
143+
*/
144+
fun isValidDirectiveArgumentValue(directiveName: String, argumentName: String, value: Any?): Boolean = true
145+
138146
/**
139147
* Called after `willGenerateGraphQLType` and before `didGenerateGraphQLType`.
140148
* Enables you to change the wiring, e.g. apply directives to alter the target type.

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/types/generateDirective.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ import kotlin.reflect.KProperty
3333
import kotlin.reflect.full.hasAnnotation
3434
import com.expediagroup.graphql.generator.annotations.GraphQLDirective as GraphQLDirectiveAnnotation
3535

36-
const val DEFAULT_DIRECTIVE_STRING_VALUE = "DEFAULT_VALUE"
37-
const val DEFAULT_DIRECTIVE_INT_VALUE = Int.MIN_VALUE
38-
3936
internal fun generateDirectives(
4037
generator: SchemaGenerator,
4138
element: KAnnotatedElement,
@@ -99,12 +96,10 @@ private fun getDirective(generator: SchemaGenerator, directiveInfo: DirectiveMet
9996
directive.toAppliedDirective()
10097
.transform { builder ->
10198
directiveInfo.directive.annotationClass.getValidProperties(generator.config.hooks).forEach { prop ->
102-
// TODO how to handle/skip default values
10399
val argumentToBeModified = directive.getArgument(prop.name)
104100
if (argumentToBeModified != null) {
105101
val value = prop.call(directiveInfo.directive)
106-
// annotations cannot have null values, we should skip known defaults
107-
if (value != DEFAULT_DIRECTIVE_STRING_VALUE && value != DEFAULT_DIRECTIVE_INT_VALUE) {
102+
if (generator.config.hooks.isValidDirectiveArgumentValue(directiveName, prop.name, value)) {
108103
argumentToBeModified.toAppliedArgument()
109104
.transform { argumentBuilder ->
110105
argumentBuilder.valueProgrammatic(value)

0 commit comments

Comments
 (0)