diff --git a/graphql-dgs-spring-boot-oss-autoconfigure/src/main/kotlin/com/netflix/graphql/dgs/autoconfig/DgsAutoConfiguration.kt b/graphql-dgs-spring-boot-oss-autoconfigure/src/main/kotlin/com/netflix/graphql/dgs/autoconfig/DgsAutoConfiguration.kt index 60235b7a4..a689bbe92 100644 --- a/graphql-dgs-spring-boot-oss-autoconfigure/src/main/kotlin/com/netflix/graphql/dgs/autoconfig/DgsAutoConfiguration.kt +++ b/graphql-dgs-spring-boot-oss-autoconfigure/src/main/kotlin/com/netflix/graphql/dgs/autoconfig/DgsAutoConfiguration.kt @@ -16,6 +16,7 @@ package com.netflix.graphql.dgs.autoconfig +import com.netflix.graphql.dgs.DgsDataLoaderOptionsProvider import com.netflix.graphql.dgs.DgsFederationResolver import com.netflix.graphql.dgs.DgsQueryExecutor import com.netflix.graphql.dgs.context.DgsCustomContextBuilder @@ -135,6 +136,12 @@ open class DgsAutoConfiguration( return QueryValueCustomizer { a -> a } } + @Bean + @ConditionalOnMissingBean + open fun dgsDataLoaderOptionsProvider(): DgsDataLoaderOptionsProvider { + return DefaultDataLoaderOptionsProvider() + } + @Bean open fun dgsDataLoaderProvider(applicationContext: ApplicationContext): DgsDataLoaderProvider { return DgsDataLoaderProvider(applicationContext) diff --git a/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/DgsDataLoaderOptionsProvider.kt b/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/DgsDataLoaderOptionsProvider.kt new file mode 100644 index 000000000..abe1a12c1 --- /dev/null +++ b/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/DgsDataLoaderOptionsProvider.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2023 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.netflix.graphql.dgs + +import org.dataloader.DataLoaderOptions + +fun interface DgsDataLoaderOptionsProvider { + fun getOptions(dataLoaderName: String, annotation: DgsDataLoader): DataLoaderOptions +} diff --git a/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DefaultDataLoaderOptionsProvider.kt b/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DefaultDataLoaderOptionsProvider.kt new file mode 100644 index 000000000..ed0f12ed4 --- /dev/null +++ b/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DefaultDataLoaderOptionsProvider.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2023 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.netflix.graphql.dgs.internal + +import com.netflix.graphql.dgs.DgsDataLoader +import com.netflix.graphql.dgs.DgsDataLoaderOptionsProvider +import org.dataloader.DataLoaderOptions + +class DefaultDataLoaderOptionsProvider : DgsDataLoaderOptionsProvider { + override fun getOptions(dataLoaderName: String, annotation: DgsDataLoader): DataLoaderOptions { + val options = DataLoaderOptions() + .setBatchingEnabled(annotation.batching) + .setCachingEnabled(annotation.caching) + if (annotation.maxBatchSize > 0) { + options.setMaxBatchSize(annotation.maxBatchSize) + } + return options + } +} diff --git a/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DgsDataLoaderProvider.kt b/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DgsDataLoaderProvider.kt index 299cb7f6c..ae385b616 100644 --- a/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DgsDataLoaderProvider.kt +++ b/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DgsDataLoaderProvider.kt @@ -19,6 +19,7 @@ package com.netflix.graphql.dgs.internal import com.netflix.graphql.dgs.DataLoaderInstrumentationExtensionProvider import com.netflix.graphql.dgs.DgsComponent import com.netflix.graphql.dgs.DgsDataLoader +import com.netflix.graphql.dgs.DgsDataLoaderOptionsProvider import com.netflix.graphql.dgs.DgsDataLoaderRegistryConsumer import com.netflix.graphql.dgs.DgsDispatchPredicate import com.netflix.graphql.dgs.exceptions.DgsUnnamedDataLoaderOnFieldException @@ -29,7 +30,6 @@ import org.dataloader.BatchLoader import org.dataloader.BatchLoaderWithContext import org.dataloader.DataLoader import org.dataloader.DataLoaderFactory -import org.dataloader.DataLoaderOptions import org.dataloader.DataLoaderRegistry import org.dataloader.MappedBatchLoader import org.dataloader.MappedBatchLoaderWithContext @@ -46,7 +46,10 @@ import javax.annotation.PostConstruct /** * Framework implementation class responsible for finding and configuring data loaders. */ -class DgsDataLoaderProvider(private val applicationContext: ApplicationContext) { +class DgsDataLoaderProvider( + private val applicationContext: ApplicationContext, + private val dataLoaderOptionsProvider: DgsDataLoaderOptionsProvider = DefaultDataLoaderOptionsProvider() +) { private data class LoaderHolder(val theLoader: T, val annotation: DgsDataLoader, val name: String, val dispatchPredicate: DispatchPredicate? = null) @@ -194,7 +197,7 @@ class DgsDataLoaderProvider(private val applicationContext: ApplicationContext) dataLoaderName: String, dataLoaderRegistry: DataLoaderRegistry ): DataLoader<*, *> { - val options = dataLoaderOptions(dgsDataLoader) + val options = dataLoaderOptionsProvider.getOptions(dataLoaderName, dgsDataLoader) if (batchLoader is DgsDataLoaderRegistryConsumer) { batchLoader.setDataLoaderRegistry(dataLoaderRegistry) @@ -210,7 +213,7 @@ class DgsDataLoaderProvider(private val applicationContext: ApplicationContext) dataLoaderName: String, dataLoaderRegistry: DataLoaderRegistry ): DataLoader<*, *> { - val options = dataLoaderOptions(dgsDataLoader) + val options = dataLoaderOptionsProvider.getOptions(dataLoaderName, dgsDataLoader) if (batchLoader is DgsDataLoaderRegistryConsumer) { batchLoader.setDataLoaderRegistry(dataLoaderRegistry) @@ -227,7 +230,7 @@ class DgsDataLoaderProvider(private val applicationContext: ApplicationContext) supplier: Supplier, dataLoaderRegistry: DataLoaderRegistry ): DataLoader<*, *> { - val options = dataLoaderOptions(dgsDataLoader) + val options = dataLoaderOptionsProvider.getOptions(dataLoaderName, dgsDataLoader) .setBatchLoaderContextProvider(supplier::get) if (batchLoader is DgsDataLoaderRegistryConsumer) { @@ -245,7 +248,7 @@ class DgsDataLoaderProvider(private val applicationContext: ApplicationContext) supplier: Supplier, dataLoaderRegistry: DataLoaderRegistry ): DataLoader<*, *> { - val options = dataLoaderOptions(dgsDataLoader) + val options = dataLoaderOptionsProvider.getOptions(dataLoaderName, dgsDataLoader) .setBatchLoaderContextProvider(supplier::get) if (batchLoader is DgsDataLoaderRegistryConsumer) { @@ -256,16 +259,6 @@ class DgsDataLoaderProvider(private val applicationContext: ApplicationContext) return DataLoaderFactory.newMappedDataLoader(extendedBatchLoader, options) } - private fun dataLoaderOptions(annotation: DgsDataLoader): DataLoaderOptions { - val options = DataLoaderOptions() - .setBatchingEnabled(annotation.batching) - .setCachingEnabled(annotation.caching) - if (annotation.maxBatchSize > 0) { - options.setMaxBatchSize(annotation.maxBatchSize) - } - return options - } - private inline fun wrappedDataLoader(loader: T, name: String): T { try { val stream = applicationContext