Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: grab kotlinDataLoaderRegistry from environment #1767

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ import java.util.concurrent.CompletableFuture
/**
* Check if all futures collected on [KotlinDataLoaderRegistry.dispatchAll] were handled
* and if we have more futures than we had when we started to dispatch, if so,
* means that [DataLoader]s were chained so we need to dispatch the dataLoaderRegistry.
* means that [DataLoader]s were chained, so we need to dispatch the dataLoaderRegistry.
*/
fun <V> CompletableFuture<V>.dispatchIfNeeded(
environment: DataFetchingEnvironment
): CompletableFuture<V> {
val dataLoaderRegistry =
environment
.graphQlContext.get<KotlinDataLoaderRegistry>(KotlinDataLoaderRegistry::class)
?: throw MissingKotlinDataLoaderRegistryException()
val dataLoaderRegistry = environment.dataLoaderRegistry as? KotlinDataLoaderRegistry ?: throw MissingKotlinDataLoaderRegistryException()

if (dataLoaderRegistry.dataLoadersInvokedOnDispatch()) {
val cantContinueExecution = when {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Expedia, Inc
* Copyright 2023 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@ import io.mockk.verify
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class DataLoaderSyncExecutionExhaustedInstrumentationTest {
private val dataLoaderSyncExecutionExhaustedInstrumentation = DataLoaderSyncExecutionExhaustedInstrumentation()
Expand Down Expand Up @@ -387,6 +388,10 @@ class DataLoaderSyncExecutionExhaustedInstrumentationTest {

assertEquals(3, results.size)

results.forEach { result ->
assertTrue(result.errors.isEmpty())
}

val missionStatistics = kotlinDataLoaderRegistry.dataLoadersMap["MissionDataLoader"]?.statistics
val planetStatistics = kotlinDataLoaderRegistry.dataLoadersMap["PlanetsByMissionDataLoader"]?.statistics

Expand Down Expand Up @@ -424,10 +429,15 @@ class DataLoaderSyncExecutionExhaustedInstrumentationTest {
)

assertEquals(3, results.size)
results.forEach { result ->
assertTrue(result.errors.isEmpty())
}

val astronautStatistics = kotlinDataLoaderRegistry.dataLoadersMap["AstronautDataLoader"]?.statistics
val missionsByAstronautStatistics = kotlinDataLoaderRegistry.dataLoadersMap["MissionsByAstronautDataLoader"]?.statistics
val planetStatistics = kotlinDataLoaderRegistry.dataLoadersMap["PlanetsByMissionDataLoader"]?.statistics

assertEquals(1, astronautStatistics?.batchInvokeCount)
assertEquals(1, missionsByAstronautStatistics?.batchInvokeCount)
assertEquals(1, planetStatistics?.batchInvokeCount)
}
Expand Down Expand Up @@ -466,11 +476,16 @@ class DataLoaderSyncExecutionExhaustedInstrumentationTest {
DataLoaderInstrumentationStrategy.SYNC_EXHAUSTION
)

val astronautStatistics = kotlinDataLoaderRegistry.dataLoadersMap["AstronautDataLoader"]?.statistics
val missionsByAstronautStatistics = kotlinDataLoaderRegistry.dataLoadersMap["MissionsByAstronautDataLoader"]?.statistics
val planetStatistics = kotlinDataLoaderRegistry.dataLoadersMap["PlanetsByMissionDataLoader"]?.statistics

assertEquals(2, results.size)
results.forEach { result ->
assertTrue(result.errors.isEmpty())
}

assertEquals(1, astronautStatistics?.batchInvokeCount)
assertEquals(1, missionsByAstronautStatistics?.batchInvokeCount)
assertEquals(1, planetStatistics?.batchInvokeCount)
}
Expand Down