diff --git a/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/BookDataLoader.kt b/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/BookDataLoader.kt index 05133558bc..d4fad685a2 100644 --- a/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/BookDataLoader.kt +++ b/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/BookDataLoader.kt @@ -19,12 +19,12 @@ package com.expediagroup.graphql.examples.server.ktor.schema.dataloaders import com.expediagroup.graphql.examples.server.ktor.schema.models.Book import com.expediagroup.graphql.dataloader.KotlinDataLoader import kotlinx.coroutines.runBlocking -import org.dataloader.BatchLoader +import org.dataloader.DataLoaderFactory import java.util.concurrent.CompletableFuture val BookDataLoader = object : KotlinDataLoader, List> { override val dataLoaderName = "BATCH_BOOK_LOADER" - override fun getBatchLoader() = BatchLoader, List> { ids -> + override fun getDataLoader() = DataLoaderFactory.newDataLoader, List> { ids -> CompletableFuture.supplyAsync { val allBooks = runBlocking { Book.search(ids.flatten()).toMutableList() } // produce lists of results from returned books diff --git a/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/CourseDataLoader.kt b/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/CourseDataLoader.kt index 4ab1e964a0..fa0c790722 100644 --- a/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/CourseDataLoader.kt +++ b/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/CourseDataLoader.kt @@ -19,12 +19,12 @@ package com.expediagroup.graphql.examples.server.ktor.schema.dataloaders import com.expediagroup.graphql.examples.server.ktor.schema.models.Course import com.expediagroup.graphql.dataloader.KotlinDataLoader import kotlinx.coroutines.runBlocking -import org.dataloader.BatchLoader +import org.dataloader.DataLoaderFactory import java.util.concurrent.CompletableFuture val CourseDataLoader = object : KotlinDataLoader { override val dataLoaderName = "COURSE_LOADER" - override fun getBatchLoader() = BatchLoader { ids -> + override fun getDataLoader() = DataLoaderFactory.newDataLoader { ids -> CompletableFuture.supplyAsync { runBlocking { Course.search(ids).toMutableList() } } diff --git a/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/UniversityDataLoader.kt b/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/UniversityDataLoader.kt index 5d4c88eb30..2993a08296 100644 --- a/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/UniversityDataLoader.kt +++ b/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/schema/dataloaders/UniversityDataLoader.kt @@ -19,15 +19,14 @@ package com.expediagroup.graphql.examples.server.ktor.schema.dataloaders import com.expediagroup.graphql.examples.server.ktor.schema.models.University import com.expediagroup.graphql.dataloader.KotlinDataLoader import kotlinx.coroutines.runBlocking -import org.dataloader.BatchLoader +import org.dataloader.DataLoaderFactory import java.util.concurrent.CompletableFuture val UniversityDataLoader = object : KotlinDataLoader { override val dataLoaderName = "UNIVERSITY_LOADER" - override fun getBatchLoader(): BatchLoader = - BatchLoader { ids -> - CompletableFuture.supplyAsync { - runBlocking { University.search(ids).toMutableList() } - } + override fun getDataLoader() = DataLoaderFactory.newDataLoader { ids -> + CompletableFuture.supplyAsync { + runBlocking { University.search(ids).toMutableList() } } + } } diff --git a/examples/server/spring-server/src/main/kotlin/com/expediagroup/graphql/examples/server/spring/dataloaders/CompanyDataLoader.kt b/examples/server/spring-server/src/main/kotlin/com/expediagroup/graphql/examples/server/spring/dataloaders/CompanyDataLoader.kt index 073f250ae4..c818b4a600 100644 --- a/examples/server/spring-server/src/main/kotlin/com/expediagroup/graphql/examples/server/spring/dataloaders/CompanyDataLoader.kt +++ b/examples/server/spring-server/src/main/kotlin/com/expediagroup/graphql/examples/server/spring/dataloaders/CompanyDataLoader.kt @@ -18,7 +18,7 @@ package com.expediagroup.graphql.examples.server.spring.dataloaders import com.expediagroup.graphql.examples.server.spring.model.Company import com.expediagroup.graphql.dataloader.KotlinDataLoader -import org.dataloader.BatchLoader +import org.dataloader.DataLoaderFactory import org.springframework.stereotype.Component import java.util.concurrent.CompletableFuture @@ -29,7 +29,7 @@ class CompanyDataLoader(private val service: CompanyService) : KotlinDataLoader< } override val dataLoaderName = name - override fun getBatchLoader() = BatchLoader { ids -> + override fun getDataLoader() = DataLoaderFactory.newDataLoader { ids -> CompletableFuture.supplyAsync { service.getCompanies(ids) } } } diff --git a/executions/graphql-kotlin-dataloader-instrumentation/build.gradle.kts b/executions/graphql-kotlin-dataloader-instrumentation/build.gradle.kts index f122379cb9..1939cfbe2f 100644 --- a/executions/graphql-kotlin-dataloader-instrumentation/build.gradle.kts +++ b/executions/graphql-kotlin-dataloader-instrumentation/build.gradle.kts @@ -7,7 +7,9 @@ val reactorExtensionsVersion: String by project dependencies { api(project(path = ":graphql-kotlin-dataloader")) - api("com.graphql-java:graphql-java:$graphQLJavaVersion") + api("com.graphql-java:graphql-java:$graphQLJavaVersion") { + exclude(group = "com.graphql-java", module = "java-dataloader") + } testImplementation("io.projectreactor.kotlin:reactor-kotlin-extensions:$reactorExtensionsVersion") testImplementation("io.projectreactor:reactor-core:$reactorVersion") testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") diff --git a/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/AstronautService.kt b/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/AstronautService.kt index 47bce1823a..f06a154cea 100644 --- a/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/AstronautService.kt +++ b/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/AstronautService.kt @@ -24,7 +24,8 @@ import com.expediagroup.graphql.dataloader.instrumentation.fixture.domain.Planet import com.expediagroup.graphql.dataloader.instrumentation.fixture.extensions.toListOfNullables import com.expediagroup.graphql.dataloader.instrumentation.fixture.repository.AstronautRepository import graphql.schema.DataFetchingEnvironment -import org.dataloader.BatchLoader +import org.dataloader.DataLoader +import org.dataloader.DataLoaderFactory import org.dataloader.DataLoaderOptions import org.dataloader.stats.SimpleStatisticsCollector import java.util.Optional @@ -34,15 +35,17 @@ data class AstronautServiceRequest(val id: Int) class AstronautDataLoader : KotlinDataLoader { override val dataLoaderName: String = "AstronautDataLoader" - override fun getOptions(): DataLoaderOptions = DataLoaderOptions.newOptions().setStatisticsCollector { SimpleStatisticsCollector() } - override fun getBatchLoader(): BatchLoader = - BatchLoader { keys -> - AstronautRepository - .getAstronauts(keys.map(AstronautServiceRequest::id)) - .collectList() - .map(List>::toListOfNullables) - .toFuture() - } + override fun getDataLoader(): DataLoader = + DataLoaderFactory.newDataLoader( + { keys -> + AstronautRepository + .getAstronauts(keys.map(AstronautServiceRequest::id)) + .collectList() + .map(List>::toListOfNullables) + .toFuture() + }, + DataLoaderOptions.newOptions().setStatisticsCollector(::SimpleStatisticsCollector) + ) } class AstronautService { diff --git a/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/MissionService.kt b/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/MissionService.kt index 90d188280a..c1d6338b23 100644 --- a/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/MissionService.kt +++ b/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/MissionService.kt @@ -21,7 +21,8 @@ import com.expediagroup.graphql.dataloader.instrumentation.fixture.domain.Missio import com.expediagroup.graphql.dataloader.instrumentation.fixture.extensions.toListOfNullables import com.expediagroup.graphql.dataloader.instrumentation.fixture.repository.MissionRepository import graphql.schema.DataFetchingEnvironment -import org.dataloader.BatchLoader +import org.dataloader.DataLoader +import org.dataloader.DataLoaderFactory import org.dataloader.DataLoaderOptions import org.dataloader.stats.SimpleStatisticsCollector import java.util.Optional @@ -31,26 +32,28 @@ data class MissionServiceRequest(val id: Int, val astronautId: Int = -1) class MissionDataLoader : KotlinDataLoader { override val dataLoaderName: String = "MissionDataLoader" - override fun getOptions(): DataLoaderOptions = DataLoaderOptions.newOptions().setStatisticsCollector { SimpleStatisticsCollector() } - override fun getBatchLoader(): BatchLoader = - BatchLoader { keys -> + override fun getDataLoader(): DataLoader = DataLoaderFactory.newDataLoader( + { keys -> MissionRepository .getMissions(keys.map(MissionServiceRequest::id)) .collectList() .map(List>::toListOfNullables) .toFuture() - } + }, + DataLoaderOptions.newOptions().setStatisticsCollector(::SimpleStatisticsCollector) + ) } class MissionsByAstronautDataLoader : KotlinDataLoader> { override val dataLoaderName: String = "MissionsByAstronautDataLoader" - override fun getOptions(): DataLoaderOptions = DataLoaderOptions.newOptions().setStatisticsCollector { SimpleStatisticsCollector() } - override fun getBatchLoader(): BatchLoader> = - BatchLoader> { keys -> + override fun getDataLoader(): DataLoader> = DataLoaderFactory.newDataLoader( + { keys -> MissionRepository .getMissionsByAstronautIds(keys.map(MissionServiceRequest::astronautId)) .collectList().toFuture() - } + }, + DataLoaderOptions.newOptions().setStatisticsCollector(::SimpleStatisticsCollector) + ) } class MissionService { diff --git a/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/PlanetService.kt b/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/PlanetService.kt index d7d530bf2c..f66fcb187a 100644 --- a/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/PlanetService.kt +++ b/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/PlanetService.kt @@ -20,7 +20,8 @@ import com.expediagroup.graphql.dataloader.KotlinDataLoader import com.expediagroup.graphql.dataloader.instrumentation.fixture.domain.Planet import com.expediagroup.graphql.dataloader.instrumentation.fixture.repository.PlanetRepository import graphql.schema.DataFetchingEnvironment -import org.dataloader.BatchLoader +import org.dataloader.DataLoader +import org.dataloader.DataLoaderFactory import org.dataloader.DataLoaderOptions import org.dataloader.stats.SimpleStatisticsCollector import java.util.concurrent.CompletableFuture @@ -29,14 +30,15 @@ data class PlanetServiceRequest(val id: Int, val missionId: Int = -1) class PlanetsByMissionDataLoader : KotlinDataLoader> { override val dataLoaderName: String = "PlanetsByMissionDataLoader" - override fun getOptions(): DataLoaderOptions = DataLoaderOptions.newOptions().setStatisticsCollector { SimpleStatisticsCollector() } - override fun getBatchLoader(): BatchLoader> = - BatchLoader> { keys -> + override fun getDataLoader(): DataLoader> = DataLoaderFactory.newDataLoader( + { keys -> PlanetRepository .getPlanetsByMissionIds(keys.map(PlanetServiceRequest::missionId)) .collectList() .toFuture() - } + }, + DataLoaderOptions.newOptions().setStatisticsCollector(::SimpleStatisticsCollector) + ) } class PlanetService { diff --git a/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/ProductService.kt b/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/ProductService.kt index 18395eb23e..6a347339b2 100644 --- a/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/ProductService.kt +++ b/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/datafetcher/ProductService.kt @@ -5,7 +5,8 @@ import com.expediagroup.graphql.dataloader.instrumentation.fixture.domain.Produc import com.expediagroup.graphql.dataloader.instrumentation.fixture.extensions.toListOfNullables import com.expediagroup.graphql.dataloader.instrumentation.fixture.repository.ProductRepository import graphql.schema.DataFetchingEnvironment -import org.dataloader.BatchLoader +import org.dataloader.DataLoader +import org.dataloader.DataLoaderFactory import org.dataloader.DataLoaderOptions import org.dataloader.stats.SimpleStatisticsCollector import java.util.Optional @@ -13,15 +14,16 @@ import java.util.concurrent.CompletableFuture class ProductDataLoader : KotlinDataLoader { override val dataLoaderName: String = "ProductDataLoader" - override fun getOptions(): DataLoaderOptions = DataLoaderOptions.newOptions().setStatisticsCollector { SimpleStatisticsCollector() } - override fun getBatchLoader(): BatchLoader = - BatchLoader { requests -> + override fun getDataLoader(): DataLoader = DataLoaderFactory.newDataLoader( + { requests -> ProductRepository .getProducts(requests) .collectList() .map(List>::toListOfNullables) .toFuture() - } + }, + DataLoaderOptions.newOptions().setStatisticsCollector(::SimpleStatisticsCollector) + ) } data class ProductServiceRequest(val id: Int, val fields: List) diff --git a/executions/graphql-kotlin-dataloader/README.md b/executions/graphql-kotlin-dataloader/README.md index b216446cf6..232e64ec6f 100644 --- a/executions/graphql-kotlin-dataloader/README.md +++ b/executions/graphql-kotlin-dataloader/README.md @@ -18,8 +18,7 @@ To help in the registration of `DataLoaders`, we have created a basic interface ```kotlin interface KotlinDataLoader { val dataLoaderName: String - fun getBatchLoader(): BatchLoader - fun getOptions(): DataLoaderOptions = DataLoaderOptions.newOptions() + fun getDataLoader(): DataLoader } ``` @@ -29,12 +28,14 @@ and its various configuration options. ```kotlin class UserDataLoader : KotlinDataLoader { override val dataLoaderName = "UserDataLoader" - override fun getBatchLoader() = BatchLoader { ids -> - CompletableFuture.supplyAsync { - ids.map { id -> userService.getUser(id) } - } - } - override fun getOptions() = DataLoaderOptions.newOptions().setCachingEnabled(false) + override fun getDataLoader() = DataLoaderFactory.newDataLoader( + { ids -> + CompletableFuture.supplyAsync { + ids.map { id -> userService.getUser(id) } + } + }, + DataLoaderOptions.newOptions().setCachingEnabled(false) + ) } ``` diff --git a/executions/graphql-kotlin-dataloader/build.gradle.kts b/executions/graphql-kotlin-dataloader/build.gradle.kts index 2dea171b8c..0dcad8938b 100644 --- a/executions/graphql-kotlin-dataloader/build.gradle.kts +++ b/executions/graphql-kotlin-dataloader/build.gradle.kts @@ -20,12 +20,12 @@ tasks { limit { counter = "INSTRUCTION" value = "COVEREDRATIO" - minimum = "0.55".toBigDecimal() + minimum = "0.53".toBigDecimal() } limit { counter = "BRANCH" value = "COVEREDRATIO" - minimum = "0.55".toBigDecimal() + minimum = "0.53".toBigDecimal() } } } diff --git a/executions/graphql-kotlin-dataloader/src/main/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoader.kt b/executions/graphql-kotlin-dataloader/src/main/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoader.kt index 5f2934fcb3..bfa9a6e1f5 100644 --- a/executions/graphql-kotlin-dataloader/src/main/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoader.kt +++ b/executions/graphql-kotlin-dataloader/src/main/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoader.kt @@ -16,17 +16,13 @@ package com.expediagroup.graphql.dataloader -import org.dataloader.BatchLoader import org.dataloader.DataLoader -import org.dataloader.DataLoaderOptions /** - * Configuration interface that will create a [DataLoader] instance, - * so we can have common logic around registering the data loaders + * Wrapper around the [DataLoader] class so we can have common logic around registering the loaders * by return type and loading values in the data fetchers. */ interface KotlinDataLoader { val dataLoaderName: String - fun getBatchLoader(): BatchLoader - fun getOptions(): DataLoaderOptions = DataLoaderOptions.newOptions() + fun getDataLoader(): DataLoader } diff --git a/executions/graphql-kotlin-dataloader/src/main/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryFactory.kt b/executions/graphql-kotlin-dataloader/src/main/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryFactory.kt index a628c2ef4a..da89e1d2fb 100644 --- a/executions/graphql-kotlin-dataloader/src/main/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryFactory.kt +++ b/executions/graphql-kotlin-dataloader/src/main/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryFactory.kt @@ -16,7 +16,6 @@ package com.expediagroup.graphql.dataloader -import org.dataloader.DataLoaderFactory import org.dataloader.DataLoaderRegistry /** @@ -36,10 +35,7 @@ class KotlinDataLoaderRegistryFactory( dataLoaders.forEach { dataLoader -> registry.register( dataLoader.dataLoaderName, - DataLoaderFactory.newDataLoader( - dataLoader.getBatchLoader(), - dataLoader.getOptions() - ) + dataLoader.getDataLoader() ) } return KotlinDataLoaderRegistry(registry) diff --git a/executions/graphql-kotlin-dataloader/src/test/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryFactoryTest.kt b/executions/graphql-kotlin-dataloader/src/test/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryFactoryTest.kt index 3c7d0d45c9..9baa88b93a 100644 --- a/executions/graphql-kotlin-dataloader/src/test/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryFactoryTest.kt +++ b/executions/graphql-kotlin-dataloader/src/test/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryFactoryTest.kt @@ -17,7 +17,7 @@ package com.expediagroup.graphql.dataloader import io.mockk.mockk -import org.dataloader.BatchLoader +import org.dataloader.DataLoader import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -39,7 +39,7 @@ class KotlinDataLoaderRegistryFactoryTest { fun `generate registry with basic loader`() { val mockLoader: KotlinDataLoader = object : KotlinDataLoader { override val dataLoaderName: String = "MockDataLoader" - override fun getBatchLoader(): BatchLoader = mockk() + override fun getDataLoader(): DataLoader = mockk() } val registry = KotlinDataLoaderRegistryFactory(listOf(mockLoader)).generate() diff --git a/executions/graphql-kotlin-dataloader/src/test/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryTest.kt b/executions/graphql-kotlin-dataloader/src/test/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryTest.kt index e215467294..03bb1c44e4 100644 --- a/executions/graphql-kotlin-dataloader/src/test/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryTest.kt +++ b/executions/graphql-kotlin-dataloader/src/test/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistryTest.kt @@ -16,7 +16,8 @@ package com.expediagroup.graphql.dataloader -import org.dataloader.BatchLoader +import org.dataloader.DataLoader +import org.dataloader.DataLoaderFactory import org.junit.jupiter.api.Test import reactor.kotlin.core.publisher.toFlux import java.time.Duration @@ -29,14 +30,14 @@ class KotlinDataLoaderRegistryTest { fun `Decorator will keep track of DataLoaders futures`() { val stringToUpperCaseDataLoader: KotlinDataLoader = object : KotlinDataLoader { override val dataLoaderName: String = "ToUppercaseDataLoader" - override fun getBatchLoader(): BatchLoader = BatchLoader { keys -> + override fun getDataLoader(): DataLoader = DataLoaderFactory.newDataLoader { keys -> keys.toFlux().map(String::uppercase).collectList().delayElement(Duration.ofMillis(300)).toFuture() } } val stringToLowerCaseDataLoader: KotlinDataLoader = object : KotlinDataLoader { override val dataLoaderName: String = "ToLowercaseDataLoader" - override fun getBatchLoader(): BatchLoader = BatchLoader { keys -> + override fun getDataLoader(): DataLoader = DataLoaderFactory.newDataLoader { keys -> keys.toFlux().map(String::lowercase).collectList().delayElement(Duration.ofMillis(300)).toFuture() } } diff --git a/servers/graphql-kotlin-server/src/test/kotlin/com/expediagroup/graphql/server/execution/GraphQLRequestHandlerTest.kt b/servers/graphql-kotlin-server/src/test/kotlin/com/expediagroup/graphql/server/execution/GraphQLRequestHandlerTest.kt index 229aa7ca58..0fadf60d62 100644 --- a/servers/graphql-kotlin-server/src/test/kotlin/com/expediagroup/graphql/server/execution/GraphQLRequestHandlerTest.kt +++ b/servers/graphql-kotlin-server/src/test/kotlin/com/expediagroup/graphql/server/execution/GraphQLRequestHandlerTest.kt @@ -38,7 +38,8 @@ import graphql.schema.GraphQLSchema import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.runBlocking -import org.dataloader.BatchLoader +import org.dataloader.DataLoader +import org.dataloader.DataLoaderFactory import org.junit.jupiter.api.Test import java.util.concurrent.CompletableFuture import kotlin.random.Random @@ -62,7 +63,7 @@ class GraphQLRequestHandlerTest { KotlinDataLoaderRegistryFactory( object : KotlinDataLoader { override val dataLoaderName: String = "UserDataLoader" - override fun getBatchLoader(): BatchLoader = BatchLoader { + override fun getDataLoader(): DataLoader = DataLoaderFactory.newDataLoader { _ -> CompletableFuture.completedFuture( listOf( User(1, "John Doe"), diff --git a/servers/graphql-kotlin-server/src/test/kotlin/com/expediagroup/graphql/server/extensions/RequestExtensionsKtTest.kt b/servers/graphql-kotlin-server/src/test/kotlin/com/expediagroup/graphql/server/extensions/RequestExtensionsKtTest.kt index 6e298655cb..1fb61618a1 100644 --- a/servers/graphql-kotlin-server/src/test/kotlin/com/expediagroup/graphql/server/extensions/RequestExtensionsKtTest.kt +++ b/servers/graphql-kotlin-server/src/test/kotlin/com/expediagroup/graphql/server/extensions/RequestExtensionsKtTest.kt @@ -20,7 +20,7 @@ import com.expediagroup.graphql.dataloader.KotlinDataLoader import com.expediagroup.graphql.dataloader.KotlinDataLoaderRegistryFactory import com.expediagroup.graphql.server.types.GraphQLRequest import io.mockk.mockk -import org.dataloader.BatchLoader +import org.dataloader.DataLoader import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -64,7 +64,7 @@ class RequestExtensionsKtTest { val dataLoaderRegistry = KotlinDataLoaderRegistryFactory( object : KotlinDataLoader { override val dataLoaderName: String = "abc" - override fun getBatchLoader(): BatchLoader = mockk() + override fun getDataLoader(): DataLoader = mockk() } ).generate() diff --git a/servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/SchemaConfigurationTest.kt b/servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/SchemaConfigurationTest.kt index 21a3845fca..88639af5d3 100644 --- a/servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/SchemaConfigurationTest.kt +++ b/servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/SchemaConfigurationTest.kt @@ -38,7 +38,8 @@ import graphql.schema.GraphQLSchema import graphql.schema.GraphQLTypeUtil import io.mockk.mockk import org.assertj.core.api.AssertionsForInterfaceTypes.assertThat -import org.dataloader.BatchLoader +import org.dataloader.DataLoader +import org.dataloader.DataLoaderFactory import org.junit.jupiter.api.Test import org.springframework.boot.autoconfigure.AutoConfigurations import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner @@ -192,7 +193,7 @@ class SchemaConfigurationTest { } override val dataLoaderName = name - override fun getBatchLoader(): BatchLoader = BatchLoader { keys -> + override fun getDataLoader(): DataLoader = DataLoaderFactory.newDataLoader { keys -> CompletableFuture.supplyAsync { keys.mapNotNull { Foo(it) } } diff --git a/servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/execution/SpringGraphQLSubscriptionHandlerTest.kt b/servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/execution/SpringGraphQLSubscriptionHandlerTest.kt index 5d097dca1c..74d07b4c11 100644 --- a/servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/execution/SpringGraphQLSubscriptionHandlerTest.kt +++ b/servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/execution/SpringGraphQLSubscriptionHandlerTest.kt @@ -32,7 +32,8 @@ import graphql.schema.DataFetchingEnvironment import graphql.schema.GraphQLSchema import io.mockk.mockk import kotlinx.coroutines.reactor.asFlux -import org.dataloader.BatchLoader +import org.dataloader.DataLoader +import org.dataloader.DataLoaderFactory import org.junit.jupiter.api.Test import reactor.core.publisher.Flux import reactor.kotlin.core.publisher.toFlux @@ -58,7 +59,7 @@ class SpringGraphQLSubscriptionHandlerTest { .build() private val mockLoader: KotlinDataLoader = object : KotlinDataLoader { override val dataLoaderName: String = "MockDataLoader" - override fun getBatchLoader(): BatchLoader = BatchLoader { ids -> + override fun getDataLoader(): DataLoader = DataLoaderFactory.newDataLoader { ids -> CompletableFuture.supplyAsync { ids.map { "$it:value" } } diff --git a/website/docs/server/data-loader/data-loader.md b/website/docs/server/data-loader/data-loader.md index 44fd3f051b..facbe4dd74 100644 --- a/website/docs/server/data-loader/data-loader.md +++ b/website/docs/server/data-loader/data-loader.md @@ -32,8 +32,7 @@ To help in the registration of `DataLoaders`, we have created an interface `Kotl ```kotlin interface KotlinDataLoader { val dataLoaderName: String - fun getBatchLoader(): BatchLoader - fun getOptions(): DataLoaderOptions = DataLoaderOptions.newOptions() + fun getDataLoader(): DataLoader } ``` @@ -49,8 +48,7 @@ A `DataLoader` caches the types by some unique value, usually by the type id, an ```kotlin class UserDataLoader : KotlinDataLoader { override val dataLoaderName = "UserDataLoader" - override fun getOptions() = DataLoaderOptions.newOptions().setCachingEnabled(false) - override fun getBatchLoader() = BatchLoader { ids -> + override fun getDataLoader() = DataLoaderFactory.newDataLoader { ids -> CompletableFuture.supplyAsync { ids.map { id -> userService.getUser(id) } } @@ -59,14 +57,17 @@ class UserDataLoader : KotlinDataLoader { class FriendsDataLoader : KotlinDataLoader> { override val dataLoaderName = "FriendsDataLoader" - override fun getDataLoader() = BatchLoader> { ids -> - CompletableFuture.supplyAsync { - ids.map { id -> - val friends: List = friendService.getFriends(id) - userService.getUsers(friends) + override fun getDataLoader() = DataLoaderFactory.newDataLoader( + { ids -> + CompletableFuture.supplyAsync { + ids.map { id -> + val friends: List = friendService.getFriends(id) + userService.getUsers(friends) + } } - } - } + }, + DataLoaderOptions.newOptions().setCachingEnabled(false) + ) } val dataLoaderRegistryFactory = KotlinDataLoaderRegistryFactory(