Skip to content

Commit f1fb84c

Browse files
feat: cherry-pick DataLoaderSyncExhaustionExhaustedInstrumentation (#1613)
* feat: add missing async leafs use case to sync exhaustion (#1612) * feat: cherry-pick syncExhaustionInstrumentation fix * feat: remove Test file
1 parent a059442 commit f1fb84c

File tree

5 files changed

+64
-18
lines changed

5 files changed

+64
-18
lines changed

executions/graphql-kotlin-dataloader-instrumentation/src/main/kotlin/com/expediagroup/graphql/dataloader/instrumentation/syncexhaustion/state/ExecutionBatchState.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class ExecutionBatchState {
206206
state.isCompletedComplexObject() -> {
207207
isSyncExecutionExhausted(state.executionStrategyPaths.first())
208208
}
209-
state.isCompletedLeafOrNull() || state.isAsyncDispatchedNotLeaf() -> {
209+
state.isCompletedLeafOrNull() || state.isAsyncDispatched() -> {
210210
true
211211
}
212212
else -> false

executions/graphql-kotlin-dataloader-instrumentation/src/main/kotlin/com/expediagroup/graphql/dataloader/instrumentation/syncexhaustion/state/ExecutionStrategyState.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,50 +172,50 @@ class ExecutionStrategyState(
172172
* }
173173
* ```
174174
*/
175-
fun addExecutionStrategyPath(
176-
executionStrategyPath: String
177-
) {
175+
fun addExecutionStrategyPath(executionStrategyPath: String) {
178176
executionStrategyPaths.add(executionStrategyPath)
179177
}
180178

181179
/**
182-
* field [fetchState] is completed with a non null [result] and
180+
* field [fetchState] is completed with a non-null [result] and
183181
* [graphQLType] is not a leaf [GraphQLList] and
184-
* all non null complex objects inside the [result] list started their own executionStrategy
182+
* all non-null complex objects inside the [result] list started their own executionStrategy
185183
*
186-
* @return Boolean indicating if above sentence result
184+
* @return Boolean indicating if above conditions met
187185
*/
188186
fun isCompletedListOfComplexObjects(): Boolean =
189187
fetchState == FieldFetchState.COMPLETED && result != null &&
190188
GraphQLTypeUtil.isList(graphQLType) && !GraphQLTypeUtil.isLeaf(graphQLType) &&
191189
(result as? List<*>)?.filterNotNull()?.size == executionStrategyPaths.size
192190

193191
/**
194-
* field [fetchState] is completed with a non null [result] and
192+
* field [fetchState] is completed with a non-null [result] and
195193
* [graphQLType] is not a leaf complex type which executionStrategy was started.
196194
*
197-
* @return Boolean indicating if above sentence result
195+
* @return Boolean indicating if above conditions met
198196
*/
199197
fun isCompletedComplexObject(): Boolean =
200198
fetchState == FieldFetchState.COMPLETED && result != null &&
201199
!GraphQLTypeUtil.isList(graphQLType) && !GraphQLTypeUtil.isLeaf(graphQLType) &&
202200
executionStrategyPaths.isNotEmpty()
203201

204202
/**
205-
* field [fetchState] is completed and [graphQLType] is a Leaf or null.
203+
* field [fetchState] is [FieldFetchState.COMPLETED]
204+
* field [graphQLType] is a Leaf or null.
206205
*
207-
* @return Boolean indicating if above sentence result
206+
* @return Boolean indicating if above conditions met
208207
*/
209208
fun isCompletedLeafOrNull(): Boolean =
210209
fetchState == FieldFetchState.COMPLETED &&
211210
(GraphQLTypeUtil.isLeaf(graphQLType) || result == null)
212211

213212
/**
214-
* field [fetchType] is Async and [fetchState] is dispatched and
215-
* is not a leaf
213+
* field [fetchType] is [FieldFetchType.ASYNC],
214+
* field [fetchState] is [FieldFetchState.DISPATCHED]
215+
*
216+
* @return Boolean indicating if above conditions met
216217
*/
217-
fun isAsyncDispatchedNotLeaf(): Boolean =
218-
fetchType == FieldFetchType.ASYNC && fetchState == FieldFetchState.DISPATCHED &&
219-
!GraphQLTypeUtil.isLeaf(graphQLType)
218+
fun isAsyncDispatched(): Boolean =
219+
fetchType == FieldFetchType.ASYNC && fetchState == FieldFetchState.DISPATCHED
220220
}
221221
}

executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/AstronautGraphQL.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ object AstronautGraphQL {
6767
missions(ids: [ID!]): [Mission]!
6868
address: Address!
6969
phoneNumber: String!
70+
twitter: String!
7071
}
7172
type Astronaut {
7273
id: ID!

executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/fixture/domain/Nasa.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@
1616

1717
package com.expediagroup.graphql.dataloader.instrumentation.fixture.domain
1818

19+
import reactor.kotlin.core.publisher.toMono
20+
import java.time.Duration
21+
import java.util.concurrent.CompletableFuture
22+
1923
data class Address(
2024
val street: String = "300 E Street SW",
2125
val zipCode: String = "98004"
2226
)
2327

24-
data class Nasa(
28+
class Nasa(
2529
val address: Address = Address(),
2630
val phoneNumber: String = "+1 123-456-7890"
27-
)
31+
) {
32+
fun getTwitter(): CompletableFuture<String> =
33+
"https://twitter.com/NASA".toMono().delayElement(Duration.ofMillis(100)).toFuture()
34+
}

executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/syncexhaustion/DataLoaderSyncExecutionExhaustedInstrumentationTest.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,44 @@ class DataLoaderSyncExecutionExhaustedInstrumentationTest {
187187
}
188188
}
189189

190+
@Test
191+
fun `Instrumentation should batch transactions after exhausting a single ExecutionInput with async Leafs`() {
192+
val queries = listOf(
193+
"""
194+
fragment AstronautFragment on Astronaut { name missions { designation } }
195+
query ComplexQuery {
196+
astronaut1: astronaut(id: 1) { ...AstronautFragment }
197+
nasa {
198+
astronaut(id: 2) {...AstronautFragment }
199+
phoneNumber
200+
twitter
201+
}
202+
}
203+
""".trimIndent()
204+
)
205+
206+
val (results, kotlinDataLoaderRegistry) = AstronautGraphQL.execute(
207+
graphQL,
208+
queries,
209+
DataLoaderInstrumentationStrategy.SYNC_EXHAUSTION
210+
)
211+
212+
assertEquals(1, results.size)
213+
214+
val astronautStatistics = kotlinDataLoaderRegistry.dataLoadersMap["AstronautDataLoader"]?.statistics
215+
val missionsByAstronautStatistics = kotlinDataLoaderRegistry.dataLoadersMap["MissionsByAstronautDataLoader"]?.statistics
216+
217+
assertEquals(1, astronautStatistics?.batchInvokeCount)
218+
assertEquals(2, astronautStatistics?.batchLoadCount)
219+
220+
assertEquals(1, missionsByAstronautStatistics?.batchInvokeCount)
221+
assertEquals(2, missionsByAstronautStatistics?.batchLoadCount)
222+
223+
verify(exactly = 3) {
224+
kotlinDataLoaderRegistry.dispatchAll()
225+
}
226+
}
227+
190228
@Test
191229
fun `Instrumentation should batch transactions after exhausting multiple ExecutionInput`() {
192230
val queries = listOf(

0 commit comments

Comments
 (0)