From 8eb56b541221f1b526dd14837fda14c6c7e86108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Fr=C3=A4nz?= Date: Mon, 18 Oct 2021 14:16:54 +0200 Subject: [PATCH 1/2] Implements explicit types for PropertyDSL and DataLoaderPropertyDSL --- .../apurebase/kgraphql/schema/dsl/DataLoaderPropertyDSL.kt | 6 ++++-- .../com/apurebase/kgraphql/schema/dsl/PropertyDSL.kt | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/DataLoaderPropertyDSL.kt b/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/DataLoaderPropertyDSL.kt index d721bdb0..a735ccc7 100644 --- a/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/DataLoaderPropertyDSL.kt +++ b/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/DataLoaderPropertyDSL.kt @@ -20,6 +20,8 @@ class DataLoaderPropertyDSL( private val inputValues = mutableListOf>() + var explicitReturnType: KType? = null + fun loader(block: BatchLoader) { dataLoader = block } @@ -79,7 +81,7 @@ class DataLoaderPropertyDSL( deprecationReason = deprecationReason, isDeprecated = isDeprecated, inputValues = inputValues, - returnType = returnType, + returnType = explicitReturnType ?: returnType, prepare = prepareWrapper!!, loader = TimedAutoDispatcherDataLoaderFactory( { TimedAutoDispatcherDataLoaderOptions() }, @@ -95,7 +97,7 @@ class DataLoaderPropertyDSL( } override fun setReturnType(type: KType) { - // NOOP + explicitReturnType = type } } diff --git a/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/PropertyDSL.kt b/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/PropertyDSL.kt index 52144dd5..247b06ec 100644 --- a/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/PropertyDSL.kt +++ b/kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/PropertyDSL.kt @@ -14,6 +14,8 @@ class PropertyDSL(val name : String, block : PropertyDSL.() -> private val inputValues = mutableListOf>() + var explicitReturnType: KType? = null + init { block() } @@ -57,7 +59,8 @@ class PropertyDSL(val name : String, block : PropertyDSL.() -> isDeprecated = isDeprecated, deprecationReason = deprecationReason, inputValues = inputValues, - accessRule = accessRuleBlock + accessRule = accessRuleBlock, + explicitReturnType = explicitReturnType ) override fun addInputValues(inputValues: Collection>) { @@ -65,6 +68,6 @@ class PropertyDSL(val name : String, block : PropertyDSL.() -> } override fun setReturnType(type: KType) { - // NOOP + explicitReturnType = type } } From a84c34b608e1e860795d225affc54665218d4dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Fr=C3=A4nz?= Date: Sun, 24 Oct 2021 14:15:22 +0200 Subject: [PATCH 2/2] Adds tests to check explicit type handling --- .../kgraphql/schema/SchemaBuilderTest.kt | 49 +++++++++++++++++ .../schema/dsl/DataLoaderPropertyDSLTest.kt | 53 +++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/kgraphql/src/test/kotlin/com/apurebase/kgraphql/schema/SchemaBuilderTest.kt b/kgraphql/src/test/kotlin/com/apurebase/kgraphql/schema/SchemaBuilderTest.kt index 66d04ccb..27f0f351 100644 --- a/kgraphql/src/test/kotlin/com/apurebase/kgraphql/schema/SchemaBuilderTest.kt +++ b/kgraphql/src/test/kotlin/com/apurebase/kgraphql/schema/SchemaBuilderTest.kt @@ -2,6 +2,7 @@ package com.apurebase.kgraphql.schema import com.apurebase.kgraphql.* import com.apurebase.kgraphql.schema.dsl.SchemaBuilder +import com.apurebase.kgraphql.schema.dsl.types.TypeDSL import com.apurebase.kgraphql.schema.introspection.TypeKind import com.apurebase.kgraphql.schema.scalar.StringScalarCoercion import com.apurebase.kgraphql.schema.structure.Field @@ -12,6 +13,8 @@ import org.hamcrest.CoreMatchers.* import org.hamcrest.MatcherAssert.assertThat import org.junit.jupiter.api.Test import java.util.* +import kotlin.reflect.KType +import kotlin.reflect.typeOf /** * Tests for SchemaBuilder behaviour, not request execution @@ -664,4 +667,50 @@ class SchemaBuilderTest { val result = deserialize(schema.executeBlocking("{data}")) assertThat(result.extract("data/data"), equalTo(listOf("generic"))) } + + inline fun TypeDSL.createGenericProperty(x: P) { + property

("data") { + resolver { _ -> x }.returns

() + } + } + + @Test + fun `specifying return type explicitly allows generic property creation`(){ + val schema = defaultSchema { + type { + createGenericProperty(InputOne("generic")) + } + } + + assertThat(schema.typeByKClass(InputOne::class), notNullValue()) + } + + inline fun TypeDSL.createGenericPropertyExplicitly(returnType: KType, x: P) { + property

("data") { + resolver { _ -> x } + setReturnType(returnType) + } + } + + data class Prop(val resultType: KType, val resolver: () -> T) + + @OptIn(ExperimentalStdlibApi::class) + @Test + fun `creation of properties from a list`(){ + + val props = listOf( + Prop(typeOf()) { 0 }, + Prop(typeOf()) { "test" }) + + val schema = defaultSchema { + type { + props.forEach { prop -> + createGenericPropertyExplicitly(prop.resultType, prop.resolver()) + } + } + } + + assertThat(schema.typeByKClass(Int::class), notNullValue()) + assertThat(schema.typeByKClass(String::class), notNullValue()) + } } diff --git a/kgraphql/src/test/kotlin/com/apurebase/kgraphql/schema/dsl/DataLoaderPropertyDSLTest.kt b/kgraphql/src/test/kotlin/com/apurebase/kgraphql/schema/dsl/DataLoaderPropertyDSLTest.kt index 08702e7a..5461e325 100644 --- a/kgraphql/src/test/kotlin/com/apurebase/kgraphql/schema/dsl/DataLoaderPropertyDSLTest.kt +++ b/kgraphql/src/test/kotlin/com/apurebase/kgraphql/schema/dsl/DataLoaderPropertyDSLTest.kt @@ -1,12 +1,19 @@ package com.apurebase.kgraphql.schema.dsl +import com.apurebase.kgraphql.Scenario import com.apurebase.kgraphql.defaultSchema import com.apurebase.kgraphql.deserialize import com.apurebase.kgraphql.extract +import com.apurebase.kgraphql.schema.SchemaBuilderTest +import com.apurebase.kgraphql.schema.dsl.types.TypeDSL import com.apurebase.kgraphql.schema.execution.Executor import nidomiro.kdataloader.ExecutionResult import org.amshove.kluent.shouldBeEqualTo +import org.hamcrest.CoreMatchers +import org.hamcrest.MatcherAssert import org.junit.jupiter.api.Test +import kotlin.reflect.KType +import kotlin.reflect.typeOf class DataLoaderPropertyDSLTest { @@ -43,4 +50,50 @@ class DataLoaderPropertyDSLTest { } class Parent(val data: String = "") + + inline fun TypeDSL.createGenericDataProperty(returnType: KType, crossinline resolver: () -> P) { + dataProperty("data") { + prepare { it } + loader { it.map { ExecutionResult.Success(resolver()) } } + setReturnType(returnType) + } + } + + @OptIn(ExperimentalStdlibApi::class) + @Test + fun `specifying return type explicitly allows generic data property creation`(){ + val schema = defaultSchema { + configure{ + executor = Executor.DataLoaderPrepared + } + type { + createGenericDataProperty(typeOf()) { SchemaBuilderTest.InputOne("generic") } + } + } + + MatcherAssert.assertThat(schema.typeByKClass(SchemaBuilderTest.InputOne::class), CoreMatchers.notNullValue()) + } + + data class Prop(val resultType: KType, val resolver: () -> T) + + @OptIn(ExperimentalStdlibApi::class) + @Test + fun `creation of data properties from a list`(){ + + val props = listOf(Prop(typeOf()) { 0 }, Prop(typeOf()) { "test" }) + + val schema = defaultSchema { + configure{ + executor = Executor.DataLoaderPrepared + } + type { + props.forEach { prop -> + createGenericDataProperty(prop.resultType, prop.resolver) + } + } + } + + MatcherAssert.assertThat(schema.typeByKClass(Int::class), CoreMatchers.notNullValue()) + MatcherAssert.assertThat(schema.typeByKClass(String::class), CoreMatchers.notNullValue()) + } }