Skip to content

Commit 521b269

Browse files
committed
Fix built-in directive print strings to include arguments
1 parent a7206cc commit 521b269

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

graphql-kotlin-schema-generator/src/main/kotlin/com/expedia/graphql/directives/DeprecatedDirective.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import graphql.Scalars
44
import graphql.introspection.Introspection
55
import graphql.schema.GraphQLArgument
66
import graphql.schema.GraphQLDirective
7-
import graphql.schema.GraphQLNonNull
87

98
const val DEPRECATED_DIRECTIVE_NAME = "deprecated"
109

1110
private val DefaultDeprecatedArgument: GraphQLArgument = GraphQLArgument.newArgument()
1211
.name("reason")
13-
.type(GraphQLNonNull.nonNull(Scalars.GraphQLString))
12+
.type(Scalars.GraphQLString)
1413
.defaultValue("No longer supported")
1514
.build()
1615

graphql-kotlin-schema-generator/src/main/kotlin/com/expedia/graphql/extensions/GraphQLSchemaExtensions.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package com.expedia.graphql.extensions
22

33
import com.expedia.graphql.directives.DeprecatedDirective
44
import graphql.Directives
5+
import graphql.schema.GraphQLArgument
6+
import graphql.schema.GraphQLDirective
57
import graphql.schema.GraphQLSchema
8+
import graphql.schema.GraphQLTypeUtil
69
import graphql.schema.idl.SchemaPrinter
710

811
/**
@@ -38,10 +41,23 @@ fun GraphQLSchema.print(
3841
val defaultDirectives = arrayOf(Directives.IncludeDirective, Directives.SkipDirective, DeprecatedDirective)
3942
val directivesToString = defaultDirectives.joinToString("\n\n") { directive -> """
4043
#${directive.description}
41-
directive @${directive.name} on ${directive.validLocations().joinToString(" | ") { loc -> loc.name }}
44+
directive @${directive.name}${parseDirectiveArguments(directive)} on ${directive.validLocations().joinToString(" | ") { loc -> loc.name }}
4245
""".trimIndent()
4346
}
4447
schemaString += "\n" + directivesToString
4548
}
4649
return schemaString
4750
}
51+
52+
private fun parseDirectiveArguments(directive: GraphQLDirective) = if (directive.arguments.isNotEmpty()) {
53+
"""(${directive.arguments.joinToString(", ") { argument ->
54+
"${argument.name}: ${GraphQLTypeUtil.simplePrint(argument.type)}${parseDirectiveArgumentValue(argument)}" }})"""
55+
} else {
56+
""
57+
}
58+
59+
private fun parseDirectiveArgumentValue(argument: GraphQLArgument) = if (null != argument.defaultValue) {
60+
" = \"${argument.defaultValue}\""
61+
} else {
62+
""
63+
}

graphql-kotlin-schema-generator/src/test/kotlin/com/expedia/graphql/extensions/GraphQLSchemaExtensionsTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,13 @@ class GraphQLSchemaExtensionsTest {
217217
}
218218
219219
#Directs the executor to include this field or fragment only when the `if` argument is true
220-
directive @include on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
220+
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
221221
222222
#Directs the executor to skip this field or fragment when the `if`'argument is true.
223-
directive @skip on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
223+
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
224224
225225
#Marks the target field/enum value as deprecated
226-
directive @deprecated on FIELD_DEFINITION | ENUM_VALUE
226+
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE
227227
""".trimIndent()
228228
assertEquals(expected, sdl)
229229
}

0 commit comments

Comments
 (0)