Skip to content

Commit 9379dc6

Browse files
dariuszkucbrennantaylor
authored andcommitted
keep custom directive names if they are specified (#103)
We should only normalize the default (class name) directive names.
1 parent f674fd4 commit 9379dc6

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

example/src/main/kotlin/com.expedia.graphql.sample/Application.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ class Application {
6767
SchemaPrinter.Options.defaultOptions()
6868
.includeScalarTypes(true)
6969
.includeExtendedScalarTypes(true)
70-
.includeIntrospectionTypes(true)
7170
.includeSchemaDefintion(true)
7271
).print(schema)
7372
)

src/main/kotlin/com/expedia/graphql/schema/extensions/directiveExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private fun DirectiveInfo.getGraphQLDirective(generator: SchemaGenerator): graph
2828

2929
@Suppress("Detekt.SpreadOperator")
3030
val builder = graphql.schema.GraphQLDirective.newDirective()
31-
.name(name.normalizeDirectiveName())
31+
.name(name)
3232
.validLocations(*this.directiveAnnotation.locations)
3333
.description(this.directiveAnnotation.description)
3434

@@ -54,7 +54,7 @@ private fun String.normalizeDirectiveName() = CaseFormat.UPPER_CAMEL.to(CaseForm
5454
private data class DirectiveInfo(val directive: Annotation, val directiveAnnotation: GraphQLDirective) {
5555
val effectiveName: String? = when {
5656
directiveAnnotation.name.isNotEmpty() -> directiveAnnotation.name
57-
directive.annotationClass.simpleName.isNullOrEmpty().not() -> directive.annotationClass.simpleName
57+
directive.annotationClass.simpleName.isNullOrBlank().not() -> directive.annotationClass.simpleName?.normalizeDirectiveName()
5858
else -> null
5959
}
6060
}

src/test/kotlin/com/expedia/graphql/schema/generator/DirectiveTests.kt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,35 @@ class DirectiveTests {
4545
}
4646

4747
@Test
48-
fun `Directive renaming`() {
48+
fun `Default directive names are normalized`() {
4949
val schema = toSchema(listOf(TopLevelObjectDef(QueryObject())), config = testSchemaConfig)
5050

51-
val renamedDirective = assertNotNull(
52-
(schema.getType("Location") as? GraphQLObjectType)
53-
?.getDirective("rightNameDirective")
51+
val query = schema.queryType.getFieldDefinition("query")
52+
assertNotNull(query)
53+
assertNotNull(query.getDirective("dummyDirective"))
54+
}
55+
56+
@Test
57+
fun `Custom directive names are not modified`() {
58+
val schema = toSchema(listOf(TopLevelObjectDef(QueryObject())), config = testSchemaConfig)
59+
60+
val directive = assertNotNull(
61+
(schema.getType("Location") as? GraphQLObjectType)
62+
?.getDirective("RightNameDirective")
5463
)
5564

56-
assertEquals("arenaming", renamedDirective.arguments[0].value)
57-
assertEquals("arg", renamedDirective.arguments[0].name)
58-
assertEquals(Scalars.GraphQLString, renamedDirective.arguments[0].type)
65+
assertEquals("arenaming", directive.arguments[0].value)
66+
assertEquals("arg", directive.arguments[0].name)
67+
assertEquals(Scalars.GraphQLString, directive.arguments[0].type)
5968
}
6069
}
6170

6271
@GraphQLDirective(name = "RightNameDirective")
6372
annotation class WrongNameDirective(val arg: String)
6473

74+
@GraphQLDirective
75+
annotation class DummyDirective
76+
6577
class Geography(
6678
val id: Int?,
6779
val type: GeoType,
@@ -79,6 +91,8 @@ enum class GeoType {
7991
data class Location(val lat: Double, val lon: Double)
8092

8193
class QueryObject {
94+
95+
@DummyDirective
8296
fun query(value: Int): Geography = Geography(value, GeoType.CITY, listOf())
8397
}
8498

src/test/kotlin/com/expedia/graphql/schema/generator/directive/DirectiveWiringHelperTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal class DirectiveWiringHelperTest {
6161
null
6262
}
6363

64-
override fun providesSchemaDirectiveWiring(environment: SchemaDirectiveWiringEnvironment<*>): Boolean = true
64+
override fun providesSchemaDirectiveWiring(environment: SchemaDirectiveWiringEnvironment<*>): Boolean = environment.directive.name == "lowercase"
6565
}
6666

6767
@Test

0 commit comments

Comments
 (0)