diff --git a/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/types/InputValuesDSL.kt b/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/types/InputValuesDSL.kt index 4b8b76a0..b30a7fbf 100644 --- a/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/types/InputValuesDSL.kt +++ b/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/types/InputValuesDSL.kt @@ -9,13 +9,13 @@ class InputValuesDSL { val inputValues = mutableListOf>() - fun arg(kClass: KClass, kType: KType? = null, block : InputValueDSL.() -> Unit){ + fun arg(kClass: KClass, kType: KType? = null, block: InputValueDSL.() -> Unit) { inputValues.add(InputValueDSL(kClass, kType).apply(block)) } @OptIn(ExperimentalStdlibApi::class) - inline fun arg(noinline block : InputValueDSL.() -> Unit){ - arg(T::class, typeOf(), block) + inline fun arg(optional: Boolean = false, noinline block: InputValueDSL.() -> Unit) { + val kType = if (optional) typeOf() else typeOf() + arg(T::class, kType, block) } - } diff --git a/kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/BaseSchemaTest.kt b/kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/BaseSchemaTest.kt index 66498840..bf0fc705 100644 --- a/kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/BaseSchemaTest.kt +++ b/kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/BaseSchemaTest.kt @@ -152,6 +152,28 @@ abstract class BaseSchemaTest { } } } + query("actorsByTags") { + description = "testing ktype & jvm erasure problems" + resolver { tags: List -> + mutableListOf(bradPitt, morganFreeman, kevinSpacey, tomHardy) + } + } + query("actorsByTagsOptional") { + description = "testing ktype & jvm erasure problems" + resolver { tags: List? -> + mutableListOf(bradPitt, morganFreeman, kevinSpacey, tomHardy) + }.withArgs { + arg>(optional = true) { name = "tags"; defaultValue = emptyList() } + } + } + query("actorsByTagsNullable") { + description = "testing ktype & jvm erasure problems" + resolver { tags: List? -> + mutableListOf(bradPitt, morganFreeman, kevinSpacey, tomHardy) + }.withArgs { + arg>(optional = true) { name = "tags"; defaultValue = null } + } + } query("filmByRank") { description = "ranked films" resolver { rank: Int -> when(rank){ @@ -242,6 +264,20 @@ abstract class BaseSchemaTest { } } } + + property("pictureWithArgs") { + resolver { actor, big : Boolean? -> + val actorName = actor.name.replace(' ', '_') + if(big == true){ + "http://picture.server/pic/$actorName?big=true" + } else { + "http://picture.server/pic/$actorName?big=false" + } + }.withArgs { + arg(optional = true) { name = "big"; description = "big or small picture" } + } + } + unionProperty("favourite") { returnType = favouriteID resolver { actor -> when(actor){ diff --git a/kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/QueryTest.kt b/kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/QueryTest.kt index 5149bb05..333c034d 100644 --- a/kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/QueryTest.kt +++ b/kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/QueryTest.kt @@ -146,6 +146,33 @@ class QueryTest : BaseSchemaTest() { } } + @Test + fun `query extension property with optional annotated argument`() { + val map = execute("{actors{name, pictureWithArgs}}") + for(i in 0..4){ + val name = map.extract("data/actors[$i]/name").replace(' ', '_') + assertThat(map.extract("data/actors[$i]/pictureWithArgs"), equalTo("http://picture.server/pic/$name?big=false")) + } + } + + @Test + fun `query with mandatory generic input type`() { + val map = execute("""{actorsByTags(tags: ["1", "2", "3"]){name}}""") + assertNoErrors(map) + } + + @Test + fun `query with optional generic input type`() { + val map = execute("{actorsByTagsOptional{name}}") + assertNoErrors(map) + } + + @Test + fun `query with nulabble generic input type`() { + val map = execute("{actorsByTagsNullable{name}}") + assertNoErrors(map) + } + @Test fun `query with transformed property`(){ val map = execute("{scenario{id, content(uppercase: false)}}")