From b8b554db6072e4b89fa4cbb165f595f1fe59eede Mon Sep 17 00:00:00 2001 From: babyfish-ct Date: Sat, 26 Mar 2022 19:10:52 +0800 Subject: [PATCH] 0.0.3 --- example/build.gradle.kts | 2 +- project/build.gradle.kts | 2 +- .../graphql/provider/runtime/DataFetchers.kt | 12 +++++++----- .../runtime/loader/ManyToManyBatchLoader.kt | 13 +++++++++++++ 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/example/build.gradle.kts b/example/build.gradle.kts index 87c7a83..c512e62 100644 --- a/example/build.gradle.kts +++ b/example/build.gradle.kts @@ -16,7 +16,7 @@ repositories { } dependencies { - implementation("org.babyfish.graphql.provider:graphql-provider-starter-dgs:0.0.2") + implementation("org.babyfish.graphql.provider:graphql-provider-starter-dgs:0.0.3") ksp("org.babyfish.kimmer:kimmer-ksp:0.3.0") runtimeOnly("io.r2dbc:r2dbc-h2:0.8.5.RELEASE") } diff --git a/project/build.gradle.kts b/project/build.gradle.kts index fd4a67c..9088ba9 100644 --- a/project/build.gradle.kts +++ b/project/build.gradle.kts @@ -1,4 +1,4 @@ allprojects { group = "org.babyfish.graphql.provider" - version = "0.0.2" + version = "0.0.3" } \ No newline at end of file diff --git a/project/graphql-provider/src/main/kotlin/org/babyfish/graphql/provider/runtime/DataFetchers.kt b/project/graphql-provider/src/main/kotlin/org/babyfish/graphql/provider/runtime/DataFetchers.kt index 70b473e..f4bd41c 100644 --- a/project/graphql-provider/src/main/kotlin/org/babyfish/graphql/provider/runtime/DataFetchers.kt +++ b/project/graphql-provider/src/main/kotlin/org/babyfish/graphql/provider/runtime/DataFetchers.kt @@ -79,6 +79,9 @@ open class DataFetchers( }.toFuture() as CompletableFuture } val entity = env.getSource>() + val idOnly = env.selectionSet.fields.let { + it.size == 1 && it[0].name == "id" + } if (prop.isReference && prop.storage is Column && Immutable.isLoaded(entity, prop.immutableProp)) { val parent = Immutable.get(entity, prop.immutableProp) as Entity<*>? if (parent === null) { @@ -86,8 +89,7 @@ open class DataFetchers( } val parentId = Immutable.get(parent, prop.targetType!!.idProp.immutableProp) if (env.arguments.isEmpty()) { - val fields = env.selectionSet.fields - if (fields.size == 1 && fields[0].name == "id") { + if (idOnly) { return CompletableFuture.completedFuture( produce(prop.targetType!!.kotlinType) { Draft.set(this, prop.targetType!!.idProp.immutableProp, parentId) @@ -97,7 +99,7 @@ open class DataFetchers( } return env.loaderByParentId(prop).load(parentId) } else { - val future = env.loaderById(prop).load(entity.id) + val future = env.loaderById(prop, idOnly).load(entity.id) if (prop.isReference) { return future.thenApply { it.firstOrNull() } } @@ -175,7 +177,7 @@ open class DataFetchers( } } - private fun DataFetchingEnvironment.loaderById(prop: ModelProp): DataLoader> { + private fun DataFetchingEnvironment.loaderById(prop: ModelProp, idOnly: Boolean): DataLoader> { val dataLoaderKey = "graphql-provider:loader-by-id:${prop}" return dataLoaderRegistry.computeIfAbsent(dataLoaderKey) { DataLoaderFactory.newMappedDataLoader( @@ -185,7 +187,7 @@ open class DataFetchers( applyFilter(prop, it) } else -> - ManyToManyBatchLoader(r2dbcClient, prop) { + ManyToManyBatchLoader(r2dbcClient, prop, idOnly) { applyFilter(prop, it) } }, diff --git a/project/graphql-provider/src/main/kotlin/org/babyfish/graphql/provider/runtime/loader/ManyToManyBatchLoader.kt b/project/graphql-provider/src/main/kotlin/org/babyfish/graphql/provider/runtime/loader/ManyToManyBatchLoader.kt index 092bcb1..0e2fc5e 100644 --- a/project/graphql-provider/src/main/kotlin/org/babyfish/graphql/provider/runtime/loader/ManyToManyBatchLoader.kt +++ b/project/graphql-provider/src/main/kotlin/org/babyfish/graphql/provider/runtime/loader/ManyToManyBatchLoader.kt @@ -10,6 +10,8 @@ import org.babyfish.graphql.provider.meta.ModelProp import org.babyfish.graphql.provider.runtime.ArgumentsConverter import org.babyfish.graphql.provider.runtime.FakeID import org.babyfish.graphql.provider.runtime.R2dbcClient +import org.babyfish.kimmer.Draft +import org.babyfish.kimmer.produce import org.babyfish.kimmer.sql.Entity import org.babyfish.kimmer.sql.ast.query.MutableRootQuery import org.babyfish.kimmer.sql.ast.valueIn @@ -22,6 +24,7 @@ import kotlin.reflect.KClass internal class ManyToManyBatchLoader( private val r2dbcClient: R2dbcClient, private val prop: ModelProp, + private val idOnly: Boolean, private val filterApplier: (MutableRootQuery, FakeID>) -> Unit ) : MappedBatchLoader> { @@ -40,6 +43,16 @@ internal class ManyToManyBatchLoader( val idMap = pairs.groupBy({it.first!!}) { it.second!! } + if (idOnly && prop.filter === null) { + val targetType = prop.targetType!! + return idMap.mapValues { entry -> + entry.value.map { + produce(targetType.kotlinType) { + Draft.set(this, targetType.idProp.immutableProp, it) + } + } + } + } val allTargetIds = pairs.map { it.second }.distinct() val rows = r2dbcClient.query(