Skip to content

Commit

Permalink
feat: add DgsDataLoaderOptionsProvider (#1485)
Browse files Browse the repository at this point in the history
* feat: add DgsDataLoaderOptionsProvider

* refactor: move DgsDataLoaderOptionsProvider out from internal
  • Loading branch information
prokop7 authored and srinivasankavitha committed Oct 29, 2023
1 parent 58b5462 commit 35befbe
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<T>(val theLoader: T, val annotation: DgsDataLoader, val name: String, val dispatchPredicate: DispatchPredicate? = null)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -227,7 +230,7 @@ class DgsDataLoaderProvider(private val applicationContext: ApplicationContext)
supplier: Supplier<T>,
dataLoaderRegistry: DataLoaderRegistry
): DataLoader<*, *> {
val options = dataLoaderOptions(dgsDataLoader)
val options = dataLoaderOptionsProvider.getOptions(dataLoaderName, dgsDataLoader)
.setBatchLoaderContextProvider(supplier::get)

if (batchLoader is DgsDataLoaderRegistryConsumer) {
Expand All @@ -245,7 +248,7 @@ class DgsDataLoaderProvider(private val applicationContext: ApplicationContext)
supplier: Supplier<T>,
dataLoaderRegistry: DataLoaderRegistry
): DataLoader<*, *> {
val options = dataLoaderOptions(dgsDataLoader)
val options = dataLoaderOptionsProvider.getOptions(dataLoaderName, dgsDataLoader)
.setBatchLoaderContextProvider(supplier::get)

if (batchLoader is DgsDataLoaderRegistryConsumer) {
Expand All @@ -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 <reified T> wrappedDataLoader(loader: T, name: String): T {
try {
val stream = applicationContext
Expand Down

0 comments on commit 35befbe

Please sign in to comment.