Skip to content

Commit

Permalink
Merge pull request #176 from netag/main
Browse files Browse the repository at this point in the history
fix(dsl:input) - handle optional args
  • Loading branch information
jeggy authored May 31, 2022
2 parents aaf3640 + 9f13285 commit 12d7677
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class InputValuesDSL {

val inputValues = mutableListOf<InputValueDSL<*>>()

fun <T : Any> arg(kClass: KClass<T>, kType: KType? = null, block : InputValueDSL<T>.() -> Unit){
fun <T : Any> arg(kClass: KClass<T>, kType: KType? = null, block: InputValueDSL<T>.() -> Unit) {
inputValues.add(InputValueDSL(kClass, kType).apply(block))
}

@OptIn(ExperimentalStdlibApi::class)
inline fun <reified T : Any> arg(noinline block : InputValueDSL<T>.() -> Unit){
arg(T::class, typeOf<T>(), block)
inline fun <reified T : Any> arg(optional: Boolean = false, noinline block: InputValueDSL<T>.() -> Unit) {
val kType = if (optional) typeOf<T?>() else typeOf<T>()
arg(T::class, kType, block)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ abstract class BaseSchemaTest {
}
}
}
query("actorsByTags") {
description = "testing ktype & jvm erasure problems"
resolver { tags: List<String> ->
mutableListOf(bradPitt, morganFreeman, kevinSpacey, tomHardy)
}
}
query("actorsByTagsOptional") {
description = "testing ktype & jvm erasure problems"
resolver { tags: List<String>? ->
mutableListOf(bradPitt, morganFreeman, kevinSpacey, tomHardy)
}.withArgs {
arg<List<String>>(optional = true) { name = "tags"; defaultValue = emptyList() }
}
}
query("actorsByTagsNullable") {
description = "testing ktype & jvm erasure problems"
resolver { tags: List<String>? ->
mutableListOf(bradPitt, morganFreeman, kevinSpacey, tomHardy)
}.withArgs {
arg<List<String>>(optional = true) { name = "tags"; defaultValue = null }
}
}
query("filmByRank") {
description = "ranked films"
resolver { rank: Int -> when(rank){
Expand Down Expand Up @@ -242,6 +264,20 @@ abstract class BaseSchemaTest {
}
}
}

property<String>("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<Boolean>(optional = true) { name = "big"; description = "big or small picture" }
}
}

unionProperty("favourite") {
returnType = favouriteID
resolver { actor -> when(actor){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>("data/actors[$i]/name").replace(' ', '_')
assertThat(map.extract<String>("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)}}")
Expand Down

0 comments on commit 12d7677

Please sign in to comment.