Skip to content

Commit

Permalink
Inject DataLoaderOptionsProvider bean, if custom is provided. (#1688)
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasankavitha authored Oct 31, 2023
1 parent 3afa777 commit e726e3b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ open class DgsAutoConfiguration(
}

@Bean
open fun dgsDataLoaderProvider(applicationContext: ApplicationContext): DgsDataLoaderProvider {
return DgsDataLoaderProvider(applicationContext)
open fun dgsDataLoaderProvider(applicationContext: ApplicationContext, dataloaderOptionProvider: DgsDataLoaderOptionsProvider): DgsDataLoaderProvider {
return DgsDataLoaderProvider(applicationContext, dataloaderOptionProvider)
}

/**
Expand Down
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.DgsQueryExecutor
import com.netflix.graphql.dgs.autoconfig.testcomponents.CustomContextBuilderConfig
import com.netflix.graphql.dgs.autoconfig.testcomponents.CustomDataFetcherFactoryFixtures
Expand Down Expand Up @@ -147,6 +148,22 @@ class DgsAutoConfigurationTest {
}
}

@Test
fun `DGS custom data loader options beans is available and used`() {
context.withUserConfiguration(DataLoaderConfig::class.java).run { ctx ->
assertThat(ctx).getBean(DgsDataLoaderOptionsProvider::class.java).isNotNull()
assertThat(ctx).getBean(DgsDataLoaderOptionsProvider::class.java).javaClass.simpleName.equals("CustomDataLoaderOptionsProvider")
}
}

@Test
fun `DGS default data loader options bean is available`() {
context.run { ctx ->
assertThat(ctx).getBean(DgsDataLoaderOptionsProvider::class.java).isNotNull()
assertThat(ctx).getBean(DgsDataLoaderOptionsProvider::class.java).javaClass.simpleName.equals("DefaultDataLoaderOptionsProvider")
}
}

@Test
fun `It should be possible to override default input object mapper`() {
context.withUserConfiguration(CustomInputObjectMapperConfig::class.java).run { ctx ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package com.netflix.graphql.dgs.autoconfig.testcomponents
import com.netflix.graphql.dgs.DgsComponent
import com.netflix.graphql.dgs.DgsData
import com.netflix.graphql.dgs.DgsDataLoader
import com.netflix.graphql.dgs.DgsDataLoaderOptionsProvider
import graphql.schema.DataFetchingEnvironment
import org.dataloader.BatchLoader
import org.dataloader.DataLoaderOptions
import org.dataloader.MappedBatchLoader
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand Down Expand Up @@ -80,4 +82,20 @@ open class DataLoaderConfig {
open fun createFetcherUsingMappedLoader(): FetcherUsingMappedDataLoader {
return FetcherUsingMappedDataLoader()
}

@Bean
open fun dgsDataLoaderOptionsProvider(): DgsDataLoaderOptionsProvider {
return CustomDataLoaderOptionsProvider()
}
}

class CustomDataLoaderOptionsProvider : DgsDataLoaderOptionsProvider {
override fun getOptions(dataLoaderName: String, annotation: DgsDataLoader): DataLoaderOptions {
val options = DataLoaderOptions()
.setBatchingEnabled(false)
.setCachingEnabled(false)

options.setMaxBatchSize(50)
return options
}
}

0 comments on commit e726e3b

Please sign in to comment.