Skip to content

feat: graphql java 23 update #2106

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

Merged
merged 12 commits into from
May 8, 2025
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
27 changes: 11 additions & 16 deletions executions/graphql-kotlin-dataloader-instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Javadocs](https://img.shields.io/maven-central/v/com.expediagroup/graphql-kotlin-dataloader-instrumentation.svg?label=javadoc&colorB=brightgreen)](https://www.javadoc.io/doc/com.expediagroup/graphql-kotlin-dataloader-instrumentation)

`graphql-kotlin-dataloader-instrumentation` is set of custom instrumentations that will signal when is the right moment
to dispatch a `KotlinDataLoaderRegistry`.
to dispatch a `DataLoaderRegistry`.

This instrumentation follows the same approach of the [DataLoaderDispatcherInstrumentation](https://github.com/graphql-java/graphql-java/blob/master/src/main/java/graphql/execution/instrumentation/dataloader/DataLoaderDispatcherInstrumentation.java).

Expand Down Expand Up @@ -32,19 +32,17 @@ implementation("com.expediagroup:graphql-kotlin-dataloader-instrumentation:$late

## Use it

When creating your `GraphQL` instance make sure to include either
`DataLoaderLevelDispatchedInstrumentation` or `DataLoaderSyncExecutionExhaustedInstrumentation`.
When creating your `GraphQL` instance make sure to include an instance of `GraphQLSyncExecutionExhaustedDataLoaderDispatcher`

```kotlin
GraphQL
.instrumentation(DataLoaderSyncExecutionExhaustedInstrumentation())
.instrumentation(GraphQLSyncExecutionExhaustedDataLoaderDispatcher())
// configure schema, type wiring, etc.
.build()
```

When ready to execute an operation or operations create a `GraphQLContext` instance with one of the following entries:
- An instance of `ExecutionLevelDispatchedState` if you choose `DataLoaderLevelDispatchedInstrumentation`.
- An instance of `SyncExecutionExhaustedState` if you choose `DataLoaderSyncExecutionExhaustedInstrumentation`.
When ready to execute an operation or operations create a `GraphQLContext` instance with an instance of
`SyncExecutionExhaustedState`


```kotlin
Expand All @@ -66,22 +64,19 @@ val queries = [
]

val graphQLContext = mapOf(
SyncExecutionExhaustedState::class to SyncExecutionExhaustedState(
queries.size,
kotlinDataLoaderRegistry
)
SyncExecutionExhaustedState::class to SyncExecutionExhaustedState(queries.size) {
dataLoaderRegistry
}
)

val executionInput1 = ExecutionInput.newExecutionInput(queries[0]).graphQLContext(graphQLContext).dataLoaderRegistry(kotlinDataLoaderRegistry).build()
val executionInput2 = ExecutionInput.newExecutionInput(queries[1]).graphQLContext(graphQLContext).dataLoaderRegistry(kotlinDataLoaderRegistry).build()
val executionInput1 = ExecutionInput.newExecutionInput(queries[0]).graphQLContext(graphQLContext).dataLoaderRegistry(dataLoaderRegistry).build()
val executionInput2 = ExecutionInput.newExecutionInput(queries[1]).graphQLContext(graphQLContext).dataLoaderRegistry(dataLoaderRegistry).build()

val result1 = graphQL.executeAsync(executionInput1)
val result2 = graphQL.executeAsync(executionInput2)
```

- `DataLoaderLevelDispatchedInstrumentation` will dispatch the `KotlinDataLoaderRegistry` instance when
a certain level of all executionInputs was dispatched (all DataFetchers were invoked).
- `DataLoaderSyncExecutionExhaustedInstrumentation` will dispatch the `KotlinDataLoaderRegistry` instance when
the `GraphQLSyncExecutionExhaustedDataLoaderDispatcher` will dispatch the `DataLoaderRegistry` when
the synchronous execution of an operation exhausted (synchronous execution will be exhausted when all data fetchers
of all paths executed up until a scalar leaf, or a `CompletableFuture`).

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 Expedia, Inc
* Copyright 2025 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 @@ -16,35 +16,31 @@

package com.expediagroup.graphql.dataloader.instrumentation.extensions

import com.expediagroup.graphql.dataloader.KotlinDataLoaderRegistry
import com.expediagroup.graphql.dataloader.instrumentation.exceptions.MissingInstrumentationStateException
import com.expediagroup.graphql.dataloader.instrumentation.exceptions.MissingKotlinDataLoaderRegistryException
import com.expediagroup.graphql.dataloader.instrumentation.syncexhaustion.state.SyncExecutionExhaustedState
import graphql.schema.DataFetchingEnvironment
import org.dataloader.DataLoader
import org.dataloader.DataLoaderRegistry
import java.util.concurrent.CompletableFuture

/**
* Check if all futures collected on [KotlinDataLoaderRegistry.dispatchAll] were handled
* Check if all futures collected on [DataLoaderRegistry.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.
*
* @throws MissingInstrumentationStateException if a [SyncExecutionExhaustedState] instance is not present in the graphQLContext
*/
fun <V> CompletableFuture<V>.dispatchIfNeeded(
environment: DataFetchingEnvironment
): CompletableFuture<V> {
val dataLoaderRegistry = environment.dataLoaderRegistry as? KotlinDataLoaderRegistry ?: throw MissingKotlinDataLoaderRegistryException()
val dataLoaderRegistry = environment.dataLoaderRegistry
val syncExecutionExhaustedState = environment
.graphQlContext
.get<SyncExecutionExhaustedState>(SyncExecutionExhaustedState::class)
?: throw MissingInstrumentationStateException()

if (dataLoaderRegistry.dataLoadersInvokedOnDispatch()) {
when {
environment.graphQlContext.hasKey(SyncExecutionExhaustedState::class) -> {
environment
.graphQlContext.get<SyncExecutionExhaustedState>(SyncExecutionExhaustedState::class)
.ifAllSyncExecutionsExhausted {
dataLoaderRegistry.dispatchAll()
}
}
else -> throw MissingInstrumentationStateException()
}
if (syncExecutionExhaustedState.dataLoadersLoadInvokedAfterDispatchAll() && syncExecutionExhaustedState.allSyncExecutionsExhausted()) {
dataLoaderRegistry.dispatchAll()
}
return this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2025 Expedia, 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
*
* https://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.expediagroup.graphql.dataloader.instrumentation.syncexhaustion

import com.expediagroup.graphql.dataloader.instrumentation.syncexhaustion.state.DataLoaderRegistryState
import com.expediagroup.graphql.dataloader.instrumentation.syncexhaustion.state.SyncExecutionExhaustedState
import org.dataloader.DataLoader
import org.dataloader.instrumentation.DataLoaderInstrumentation
import org.dataloader.instrumentation.DataLoaderInstrumentationContext

/**
* Custom [DataLoaderInstrumentation] implementation that helps to calculate the state of [DataLoader]s in the
* [DataLoaderRegistryState] that lives inside the [syncExecutionExhaustedState]
*/
class DataLoaderSyncExecutionExhaustedDataLoaderDispatcher(
private val syncExecutionExhaustedState: SyncExecutionExhaustedState
) : DataLoaderInstrumentation {
private val contextForSyncExecutionExhausted: DataLoaderInstrumentationContext<Any?> =
object : DataLoaderInstrumentationContext<Any?> {
override fun onDispatched() {
syncExecutionExhaustedState.onDataLoaderLoadDispatched()
}
override fun onCompleted(result: Any?, t: Throwable?) {
syncExecutionExhaustedState.onDataLoaderLoadCompleted()
}
}

override fun beginLoad(
dataLoader: DataLoader<*, *>,
key: Any,
loadContext: Any?
): DataLoaderInstrumentationContext<Any?> =
contextForSyncExecutionExhausted
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 Expedia, Inc
* Copyright 2025 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 @@ -14,15 +14,13 @@
* limitations under the License.
*/

package com.expediagroup.graphql.dataloader.instrumentation.syncexhaustion.execution
package com.expediagroup.graphql.dataloader.instrumentation.syncexhaustion

import com.expediagroup.graphql.dataloader.instrumentation.extensions.isMutation
import com.expediagroup.graphql.dataloader.instrumentation.syncexhaustion.state.SyncExecutionExhaustedState
import graphql.ExecutionInput
import graphql.ExecutionResult
import graphql.GraphQLContext
import graphql.execution.ExecutionContext
import graphql.execution.ExecutionId
import graphql.execution.instrumentation.ExecuteObjectInstrumentationContext
import graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext
import graphql.execution.instrumentation.FieldFetchingInstrumentationContext
Expand All @@ -34,39 +32,19 @@ import graphql.execution.instrumentation.parameters.InstrumentationExecutionPara
import graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters

/**
* typealias that represents the signature of a callback that will be executed when sync execution is exhausted
*/
internal typealias OnSyncExecutionExhaustedCallback = (List<ExecutionId>) -> Unit

/**
* Custom GraphQL [Instrumentation] that calculate the synchronous execution exhaustion
* of all GraphQL operations sharing the same [GraphQLContext]
*/
abstract class AbstractSyncExecutionExhaustedInstrumentation : SimplePerformantInstrumentation() {
/**
* This is invoked each time instrumentation attempts to calculate exhaustion state, this can be called from either
* `beginFieldField.dispatch` or `beginFieldFetch.complete`.
*
* @param parameters contains information of which [ExecutionInput] caused the calculation
* @return [OnSyncExecutionExhaustedCallback] to invoke when the synchronous execution of all operations was exhausted
*/
abstract fun getOnSyncExecutionExhaustedCallback(
parameters: SyncExecutionExhaustedInstrumentationParameters
): OnSyncExecutionExhaustedCallback
class GraphQLSyncExecutionExhaustedDataLoaderDispatcher : SimplePerformantInstrumentation() {

override fun beginExecution(
parameters: InstrumentationExecutionParameters,
state: InstrumentationState?
): InstrumentationContext<ExecutionResult>? =
parameters.graphQLContext
?.get<SyncExecutionExhaustedState>(SyncExecutionExhaustedState::class)
?.beginExecution(
parameters,
this.getOnSyncExecutionExhaustedCallback(
SyncExecutionExhaustedInstrumentationParameters(parameters.executionInput)
)
)
?.beginExecution(parameters)

override fun beginExecutionStrategy(
parameters: InstrumentationExecutionStrategyParameters,
Expand Down Expand Up @@ -94,10 +72,5 @@ abstract class AbstractSyncExecutionExhaustedInstrumentation : SimplePerformantI
): FieldFetchingInstrumentationContext? =
parameters.executionContext.takeUnless(ExecutionContext::isMutation)
?.graphQLContext?.get<SyncExecutionExhaustedState>(SyncExecutionExhaustedState::class)
?.beginFieldFetching(
parameters,
this.getOnSyncExecutionExhaustedCallback(
SyncExecutionExhaustedInstrumentationParameters(parameters.executionContext.executionInput)
)
)
?.beginFieldFetching(parameters)
}

This file was deleted.

Loading
Loading