Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class DataLoaderPropertyDSL<T, K, R>(

private val inputValues = mutableListOf<InputValueDef<*>>()

var explicitReturnType: KType? = null

fun loader(block: BatchLoader<K, R>) {
dataLoader = block
}
Expand Down Expand Up @@ -79,7 +81,7 @@ class DataLoaderPropertyDSL<T, K, R>(
deprecationReason = deprecationReason,
isDeprecated = isDeprecated,
inputValues = inputValues,
returnType = returnType,
returnType = explicitReturnType ?: returnType,
prepare = prepareWrapper!!,
loader = TimedAutoDispatcherDataLoaderFactory(
{ TimedAutoDispatcherDataLoaderOptions() },
Expand All @@ -95,7 +97,7 @@ class DataLoaderPropertyDSL<T, K, R>(
}

override fun setReturnType(type: KType) {
// NOOP
explicitReturnType = type
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class PropertyDSL<T : Any, R>(val name : String, block : PropertyDSL<T, R>.() ->

private val inputValues = mutableListOf<InputValueDef<*>>()

var explicitReturnType: KType? = null

init {
block()
}
Expand Down Expand Up @@ -57,14 +59,15 @@ class PropertyDSL<T : Any, R>(val name : String, block : PropertyDSL<T, R>.() ->
isDeprecated = isDeprecated,
deprecationReason = deprecationReason,
inputValues = inputValues,
accessRule = accessRuleBlock
accessRule = accessRuleBlock,
explicitReturnType = explicitReturnType
)

override fun addInputValues(inputValues: Collection<InputValueDef<*>>) {
this.inputValues.addAll(inputValues)
}

override fun setReturnType(type: KType) {
// NOOP
explicitReturnType = type
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -664,4 +667,50 @@ class SchemaBuilderTest {
val result = deserialize(schema.executeBlocking("{data}"))
assertThat(result.extract("data/data"), equalTo(listOf("generic")))
}

inline fun <T: Any, reified P: Any> TypeDSL<T>.createGenericProperty(x: P) {
property<P>("data") {
resolver { _ -> x }.returns<P>()
}
}

@Test
fun `specifying return type explicitly allows generic property creation`(){
val schema = defaultSchema {
type<Scenario> {
createGenericProperty(InputOne("generic"))
}
}

assertThat(schema.typeByKClass(InputOne::class), notNullValue())
}

inline fun <T: Any, reified P: Any> TypeDSL<T>.createGenericPropertyExplicitly(returnType: KType, x: P) {
property<P>("data") {
resolver { _ -> x }
setReturnType(returnType)
}
}

data class Prop<T>(val resultType: KType, val resolver: () -> T)

@OptIn(ExperimentalStdlibApi::class)
@Test
fun `creation of properties from a list`(){

val props = listOf(
Prop(typeOf<Int>()) { 0 },
Prop(typeOf<String>()) { "test" })

val schema = defaultSchema {
type<Scenario> {
props.forEach { prop ->
createGenericPropertyExplicitly(prop.resultType, prop.resolver())
}
}
}

assertThat(schema.typeByKClass(Int::class), notNullValue())
assertThat(schema.typeByKClass(String::class), notNullValue())
}
}
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -43,4 +50,50 @@ class DataLoaderPropertyDSLTest {
}

class Parent(val data: String = "")

inline fun <T: Any, reified P: Any> TypeDSL<T>.createGenericDataProperty(returnType: KType, crossinline resolver: () -> P) {
dataProperty<T, P>("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<Scenario> {
createGenericDataProperty(typeOf<SchemaBuilderTest.InputOne>()) { SchemaBuilderTest.InputOne("generic") }
}
}

MatcherAssert.assertThat(schema.typeByKClass(SchemaBuilderTest.InputOne::class), CoreMatchers.notNullValue())
}

data class Prop<T>(val resultType: KType, val resolver: () -> T)

@OptIn(ExperimentalStdlibApi::class)
@Test
fun `creation of data properties from a list`(){

val props = listOf(Prop(typeOf<Int>()) { 0 }, Prop(typeOf<String>()) { "test" })

val schema = defaultSchema {
configure{
executor = Executor.DataLoaderPrepared
}
type<Scenario> {
props.forEach { prop ->
createGenericDataProperty(prop.resultType, prop.resolver)
}
}
}

MatcherAssert.assertThat(schema.typeByKClass(Int::class), CoreMatchers.notNullValue())
MatcherAssert.assertThat(schema.typeByKClass(String::class), CoreMatchers.notNullValue())
}
}