Skip to content

Commit d586879

Browse files
authored
Merge pull request #909 from graphql-java-kickstart/feature/893-reject-scalar-directives
Throw for invalid directives on scalars
2 parents a155694 + c35b365 commit d586879

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎src/main/kotlin/graphql/kickstart/tools/SchemaParser.kt‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,7 @@ class SchemaParser internal constructor(
297297
.specifiedByUrl(getSpecifiedByUrl(directives) ?: provided.specifiedByUrl)
298298
// like graphql-java, only the schema's directives are applied
299299
.clearDirectives()
300-
try {
301-
builder.withSdlDirectives(directives, SCALAR)
302-
} catch (e: SchemaError) {
303-
// TODO throw instead in 15.0.0 (#893), like for the other types
304-
log.warn("Scalar '$name' is built without its directives from the schema: ${e.message}")
305-
}
300+
.withSdlDirectives(directives, SCALAR)
306301

307302
return builder.build()
308303
}

‎src/test/kotlin/graphql/kickstart/tools/DirectiveTest.kt‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -770,12 +770,12 @@ class DirectiveTest {
770770
}
771771

772772
@Test
773-
fun `should warn about undeclared directives on scalars and build them without the schema's directives`() {
774-
val (schema, warnings) = withWarnings {
773+
fun `should fail on an undeclared directive on a scalar`() {
774+
val error = assertThrows(SchemaError::class.java) {
775775
SchemaParser.newParser()
776776
.schemaString(
777777
"""
778-
scalar Date @specifiedBy(url: "https://example.com/date") @format(pattern: "yyyy-MM-dd")
778+
scalar Date @format(pattern: "yyyy-MM-dd")
779779
780780
type Query {
781781
today: Date
@@ -787,13 +787,34 @@ class DirectiveTest {
787787
.scalars(GraphQLScalarType.newScalar().name("Date").coercing(Scalars.GraphQLString.coercing).build())
788788
.build()
789789
.makeExecutableSchema()
790-
.assertEveryFieldResolves()
791790
}
792791

793-
assertEquals(warnings, listOf("Scalar 'Date' is built without its directives from the schema: Found applied directive format without corresponding directive definition."))
794-
val date = schema.getType("Date") as GraphQLScalarType
795-
assertEquals(date.appliedDirectives, emptyList())
796-
assertEquals(date.specifiedByUrl, "https://example.com/date")
792+
assertEquals(error.message, "Found applied directive format without corresponding directive definition.")
793+
}
794+
795+
@Test
796+
fun `should fail on an unexpected directive argument on a scalar`() {
797+
val error = assertThrows(SchemaError::class.java) {
798+
SchemaParser.newParser()
799+
.schemaString(
800+
"""
801+
directive @format(pattern: String) on SCALAR
802+
803+
scalar Date @format(patern: "yyyy-MM-dd")
804+
805+
type Query {
806+
today: Date
807+
}
808+
""")
809+
.resolvers(object : GraphQLQueryResolver {
810+
fun today(): String = "2026-10-19"
811+
})
812+
.scalars(GraphQLScalarType.newScalar().name("Date").coercing(Scalars.GraphQLString.coercing).build())
813+
.build()
814+
.makeExecutableSchema()
815+
}
816+
817+
assertEquals(error.message, "Found an unexpected directive argument format#patern .")
797818
}
798819

799820
@Test

0 commit comments

Comments
 (0)