Skip to content

Commit bd00de6

Browse files
authored
Merge branch 'master' into cleanup-2026-02-03
2 parents 6cd0806 + dc32d4b commit bd00de6

File tree

2 files changed

+34
-41
lines changed

2 files changed

+34
-41
lines changed

graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DefaultDgsDataLoaderProvider.kt

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ class DefaultDgsDataLoaderProvider(
100100
mappedBatchLoaders.forEach { registerDataLoader(it, registry, contextSupplier, extensionProviders) }
101101
mappedBatchLoadersWithContext.forEach { registerDataLoader(it, registry, contextSupplier, extensionProviders) }
102102
}
103-
logger.debug("Created DGS dataloader registry in {}ms", totalTime)
103+
if (logger.isDebugEnabled) {
104+
logger.debug("Created DGS dataloader registry in {}ms", totalTime)
105+
}
104106
return registry
105107
}
106108

@@ -179,18 +181,18 @@ class DefaultDgsDataLoaderProvider(
179181
annotation: DgsDataLoader,
180182
dispatchPredicate: DispatchPredicate? = null,
181183
) {
182-
if (dataLoaders.contains(dataLoaderName)) {
184+
if (dataLoaderName in dataLoaders) {
183185
throw MultipleDataLoadersDefinedException(dgsComponentClass, dataLoaders.getValue(dataLoaderName))
184186
}
185187
dataLoaders[dataLoaderName] = dgsComponentClass
186188

187189
fun <T : Any> createHolder(t: T): LoaderHolder<T> = LoaderHolder(t, annotation, dataLoaderName, dispatchPredicate)
188190

189191
when (val customizedDataLoader = runCustomizers(dataLoader, dataLoaderName, dgsComponentClass)) {
190-
is BatchLoader<*, *> -> batchLoaders.add(createHolder(customizedDataLoader))
191-
is BatchLoaderWithContext<*, *> -> batchLoadersWithContext.add(createHolder(customizedDataLoader))
192-
is MappedBatchLoader<*, *> -> mappedBatchLoaders.add(createHolder(customizedDataLoader))
193-
is MappedBatchLoaderWithContext<*, *> -> mappedBatchLoadersWithContext.add(createHolder(customizedDataLoader))
192+
is BatchLoader<*, *> -> batchLoaders += createHolder(customizedDataLoader)
193+
is BatchLoaderWithContext<*, *> -> batchLoadersWithContext += createHolder(customizedDataLoader)
194+
is MappedBatchLoader<*, *> -> mappedBatchLoaders += createHolder(customizedDataLoader)
195+
is MappedBatchLoaderWithContext<*, *> -> mappedBatchLoadersWithContext += createHolder(customizedDataLoader)
194196
else -> throw InvalidDataLoaderTypeException(dgsComponentClass)
195197
}
196198
}
@@ -199,23 +201,17 @@ class DefaultDgsDataLoaderProvider(
199201
originalDataLoader: Any,
200202
name: String,
201203
dgsComponentClass: Class<*>,
202-
): Any {
203-
var dataLoader = originalDataLoader
204-
205-
customizers.forEach {
206-
dataLoader =
207-
when (dataLoader) {
208-
is BatchLoader<*, *> -> it.provide(dataLoader as BatchLoader<*, *>, name)
209-
is BatchLoaderWithContext<*, *> -> it.provide(dataLoader as BatchLoaderWithContext<*, *>, name)
210-
is MappedBatchLoader<*, *> -> it.provide(dataLoader as MappedBatchLoader<*, *>, name)
211-
is MappedBatchLoaderWithContext<*, *> -> it.provide(dataLoader as MappedBatchLoaderWithContext<*, *>, name)
212-
else -> throw InvalidDataLoaderTypeException(dgsComponentClass)
213-
}
204+
): Any =
205+
customizers.fold(originalDataLoader) { dataLoader, customizer ->
206+
when (dataLoader) {
207+
is BatchLoader<*, *> -> customizer.provide(dataLoader, name)
208+
is BatchLoaderWithContext<*, *> -> customizer.provide(dataLoader, name)
209+
is MappedBatchLoader<*, *> -> customizer.provide(dataLoader, name)
210+
is MappedBatchLoaderWithContext<*, *> -> customizer.provide(dataLoader, name)
211+
else -> throw InvalidDataLoaderTypeException(dgsComponentClass)
212+
}
214213
}
215214

216-
return dataLoader
217-
}
218-
219215
private fun createDataLoader(
220216
batchLoader: BatchLoader<*, *>,
221217
dgsDataLoader: DgsDataLoader,
@@ -341,15 +337,11 @@ class DefaultDgsDataLoaderProvider(
341337
else -> throw IllegalArgumentException("Data loader ${holder.name} has unknown type")
342338
}
343339
// detect and throw an exception if multiple data loaders use the same name
344-
if (registry.keys.contains(holder.name)) {
340+
if (registry.getDataLoader<Any, Any>(holder.name) != null) {
345341
throw MultipleDataLoadersDefinedException(holder.theLoader.javaClass)
346342
}
347343

348-
if (holder.dispatchPredicate == null) {
349-
registry.register(holder.name, loader, DispatchPredicate.DISPATCH_ALWAYS)
350-
} else {
351-
registry.register(holder.name, loader, holder.dispatchPredicate)
352-
}
344+
registry.register(holder.name, loader, holder.dispatchPredicate ?: DispatchPredicate.DISPATCH_ALWAYS)
353345
}
354346

355347
private inline fun <reified T> wrappedDataLoader(

graphql-dgs/src/test/kotlin/com/netflix/graphql/dgs/DefaultDgsDataLoaderProviderTest.kt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import org.junit.jupiter.api.Nested
3535
import org.junit.jupiter.api.Test
3636
import org.junit.jupiter.api.assertThrows
3737
import org.springframework.beans.factory.BeanCreationException
38+
import org.springframework.beans.factory.getBean
3839
import org.springframework.boot.test.context.runner.ApplicationContextRunner
3940
import java.util.concurrent.CompletableFuture
4041
import java.util.concurrent.CompletionStage
@@ -51,7 +52,7 @@ class DefaultDgsDataLoaderProviderTest {
5152
ExampleBatchLoader::class.java,
5253
).withBean(ExampleBatchLoaderWithDispatchPredicate::class.java)
5354
.run { context ->
54-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
55+
val provider = context.getBean<DgsDataLoaderProvider>()
5556
val dataLoaderRegistry = provider.buildRegistry()
5657
Assertions.assertEquals(2, dataLoaderRegistry.dataLoaders.size)
5758
val dataLoader = dataLoaderRegistry.getDataLoader<Any, Any>("exampleLoader")
@@ -68,7 +69,7 @@ class DefaultDgsDataLoaderProviderTest {
6869
ExampleBatchLoaderWithContext::class.java,
6970
).withBean(ExampleBatchLoaderWithContextAndDispatchPredicate::class.java)
7071
.run { context ->
71-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
72+
val provider = context.getBean<DgsDataLoaderProvider>()
7273
val dataLoaderRegistry = provider.buildRegistry()
7374
Assertions.assertEquals(2, dataLoaderRegistry.dataLoaders.size)
7475
val dataLoader = dataLoaderRegistry.getDataLoader<Any, Any>("exampleLoaderWithContext")
@@ -83,7 +84,7 @@ class DefaultDgsDataLoaderProviderTest {
8384
applicationContextRunner.withBean(ExampleBatchLoader::class.java).withBean(ExampleDuplicateBatchLoader::class.java).run { context ->
8485
val exc =
8586
assertThrows<IllegalStateException> {
86-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
87+
val provider = context.getBean<DgsDataLoaderProvider>()
8788
provider.buildRegistry()
8889
}
8990

@@ -103,7 +104,7 @@ class DefaultDgsDataLoaderProviderTest {
103104
.run { context ->
104105
val exc =
105106
assertThrows<IllegalStateException> {
106-
context.getBean(DgsDataLoaderProvider::class.java)
107+
context.getBean<DgsDataLoaderProvider>()
107108
}
108109
assertThat(exc.cause)
109110
.isInstanceOf(BeanCreationException::class.java)
@@ -115,7 +116,7 @@ class DefaultDgsDataLoaderProviderTest {
115116
@Test
116117
fun findDataLoadersFromFields() {
117118
applicationContextRunner.withBean(ExampleBatchLoaderFromField::class.java).run { context ->
118-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
119+
val provider = context.getBean<DgsDataLoaderProvider>()
119120
val dataLoaderRegistry = provider.buildRegistry()
120121
Assertions.assertEquals(2, dataLoaderRegistry.dataLoaders.size)
121122
val dataLoader = dataLoaderRegistry.getDataLoader<Any, Any>("exampleLoaderFromField")
@@ -133,7 +134,7 @@ class DefaultDgsDataLoaderProviderTest {
133134
ExampleMappedBatchLoader::class.java,
134135
).withBean(ExampleMappedBatchLoaderWithDispatchPredicate::class.java)
135136
.run { context ->
136-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
137+
val provider = context.getBean<DgsDataLoaderProvider>()
137138
val dataLoaderRegistry = provider.buildRegistry()
138139
Assertions.assertEquals(2, dataLoaderRegistry.dataLoaders.size)
139140
val dataLoader = dataLoaderRegistry.getDataLoader<Any, Any>("exampleMappedLoader")
@@ -150,7 +151,7 @@ class DefaultDgsDataLoaderProviderTest {
150151
ExampleMappedBatchLoaderWithContext::class.java,
151152
).withBean(ExampleMappedBatchLoaderWithContextAndDispatchPredicate::class.java)
152153
.run { context ->
153-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
154+
val provider = context.getBean<DgsDataLoaderProvider>()
154155
val dataLoaderRegistry = provider.buildRegistry()
155156
Assertions.assertEquals(2, dataLoaderRegistry.dataLoaders.size)
156157
val dataLoader = dataLoaderRegistry.getDataLoader<Any, Any>("exampleMappedLoaderWithContext")
@@ -163,7 +164,7 @@ class DefaultDgsDataLoaderProviderTest {
163164
@Test
164165
fun findMappedDataLoadersFromFields() {
165166
applicationContextRunner.withBean(ExampleMappedBatchLoaderFromField::class.java).run { context ->
166-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
167+
val provider = context.getBean<DgsDataLoaderProvider>()
167168
val dataLoaderRegistry = provider.buildRegistry()
168169
Assertions.assertEquals(2, dataLoaderRegistry.dataLoaders.size)
169170
val dataLoader = dataLoaderRegistry.getDataLoader<Any, Any>("exampleMappedLoaderFromField")
@@ -177,7 +178,7 @@ class DefaultDgsDataLoaderProviderTest {
177178
@Test
178179
fun dataLoaderConsumer() {
179180
applicationContextRunner.withBean(ExampleDataLoaderWithRegistry::class.java).run { context ->
180-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
181+
val provider = context.getBean<DgsDataLoaderProvider>()
181182
val registry = provider.buildRegistry()
182183

183184
// Use the dataloader's "load" method to check if the registry was set correctly, because the dataloader instance isn't itself a DgsDataLoaderRegistryConsumer
@@ -194,7 +195,7 @@ class DefaultDgsDataLoaderProviderTest {
194195
@Test
195196
fun findDataLoadersWithoutName() {
196197
applicationContextRunner.withBean(ExampleBatchLoaderWithoutName::class.java).run { context ->
197-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
198+
val provider = context.getBean<DgsDataLoaderProvider>()
198199
val dataLoaderRegistry = provider.buildRegistry()
199200
Assertions.assertEquals(1, dataLoaderRegistry.dataLoaders.size)
200201
val dataLoader =
@@ -206,7 +207,7 @@ class DefaultDgsDataLoaderProviderTest {
206207
@Test
207208
fun findDataLoadersWithoutNameByClass() {
208209
applicationContextRunner.withBean(ExampleBatchLoaderWithoutName::class.java).run { context ->
209-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
210+
val provider = context.getBean<DgsDataLoaderProvider>()
210211
val dataLoaderRegistry = provider.buildRegistry()
211212
Assertions.assertEquals(1, dataLoaderRegistry.dataLoaders.size)
212213
val dataLoader =
@@ -224,7 +225,7 @@ class DefaultDgsDataLoaderProviderTest {
224225
@Test
225226
fun findDataLoadersFromFieldsWithoutName() {
226227
applicationContextRunner.withBean(ExampleBatchLoaderWithoutNameFromField::class.java).run { context ->
227-
assertThatThrownBy { context.getBean(DgsDataLoaderProvider::class.java) }
228+
assertThatThrownBy { context.getBean<DgsDataLoaderProvider>() }
228229
.rootCause()
229230
.isInstanceOf(DgsUnnamedDataLoaderOnFieldException::class.java)
230231
.hasMessage(
@@ -241,10 +242,10 @@ class DefaultDgsDataLoaderProviderTest {
241242
).withBean(DgsWrapWithContextDataLoaderCustomizer::class.java)
242243
.withBean(DataLoaderCustomizerCounter::class.java)
243244
.run { context ->
244-
val provider = context.getBean(DgsDataLoaderProvider::class.java)
245+
val provider = context.getBean<DgsDataLoaderProvider>()
245246
val dataLoaderRegistry = provider.buildRegistry()
246247

247-
val counter = context.getBean(DataLoaderCustomizerCounter::class.java)
248+
val counter = context.getBean<DataLoaderCustomizerCounter>()
248249

249250
assertThat(dataLoaderRegistry.dataLoaders.size).isEqualTo(1)
250251

0 commit comments

Comments
 (0)