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

Move initialization of dataloader registry to the callback where we have GraphQLContext constructed #2131

Merged
merged 6 commits into from
Feb 25, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.netflix.graphql.dgs.internal.DgsDataLoaderProvider
import com.netflix.graphql.dgs.internal.DgsWebMvcRequestData
import com.netflix.graphql.dgs.springgraphql.autoconfig.DgsSpringGraphQLConfigurationProperties
import graphql.GraphQLContext
import org.dataloader.DataLoaderRegistry
import org.springframework.graphql.server.WebGraphQlInterceptor
import org.springframework.graphql.server.WebGraphQlRequest
import org.springframework.graphql.server.WebGraphQlResponse
Expand Down Expand Up @@ -56,21 +57,24 @@ class DgsWebMvcGraphQLInterceptor(
} else {
dgsContextBuilder.build(DgsWebMvcRequestData(request.extensions, request.headers))
}
val dataLoaderRegistry =
dgsDataLoaderProvider.buildRegistryWithContextSupplier {
val graphQLContext = request.toExecutionInput().graphQLContext
if (graphQLContextContributors.isNotEmpty()) {
val extensions = request.extensions
val requestData = dgsContext.requestData
val builderForContributors = GraphQLContext.newContext()
graphQLContextContributors.forEach { it.contribute(builderForContributors, extensions, requestData) }
graphQLContext.putAll(builderForContributors)
}

graphQLContext
}
var dataLoaderRegistry: DataLoaderRegistry? = null
request.configureExecutionInput { e, builder ->

dataLoaderRegistry =
dgsDataLoaderProvider.buildRegistryWithContextSupplier {
val graphQLContext = e.graphQLContext
if (graphQLContextContributors.isNotEmpty()) {
val extensions = request.extensions
val requestData = dgsContext.requestData
val builderForContributors = GraphQLContext.newContext()
graphQLContextContributors.forEach { it.contribute(builderForContributors, extensions, requestData) }
graphQLContext.putAll(builderForContributors)
}

graphQLContext
}

request.configureExecutionInput { _, builder ->
builder
.context(dgsContext)
.graphQLContext(dgsContext)
Expand All @@ -81,14 +85,14 @@ class DgsWebMvcGraphQLInterceptor(
return if (dgsSpringConfigurationProperties.webmvc.asyncdispatch.enabled) {
chain.next(request).doFinally {
if (dataLoaderRegistry is AutoCloseable) {
dataLoaderRegistry.close()
(dataLoaderRegistry as AutoCloseable).close()
}
}
} else {
@Suppress("BlockingMethodInNonBlockingContext")
val response = chain.next(request).block()!!
if (dataLoaderRegistry is AutoCloseable) {
dataLoaderRegistry.close()
(dataLoaderRegistry as AutoCloseable).close()
}
return Mono.just(response)
}
Expand Down
Loading