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 bug in Spring query executors #1864

Merged
merged 1 commit into from
Apr 2, 2024
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 @@ -30,16 +30,14 @@ import com.netflix.graphql.dgs.internal.DgsQueryExecutorRequestCustomizer
import com.netflix.graphql.dgs.internal.DgsWebMvcRequestData
import graphql.ExecutionResult
import graphql.GraphQLContext
import graphql.GraphQLError
import org.springframework.graphql.ExecutionGraphQlService
import org.springframework.graphql.support.DefaultExecutionGraphQlRequest
import org.springframework.http.HttpHeaders
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletWebRequest
import org.springframework.web.context.request.WebRequest
import java.util.concurrent.CompletableFuture

class SpringGraphQLDgsQueryExecutor(val executionService: ExecutionGraphQlService, private val dgsContextBuilder: DefaultDgsGraphQLContextBuilder, private val dgsDataLoaderProvider: DgsDataLoaderProvider, private val requestCustomizer: DgsQueryExecutorRequestCustomizer = DgsQueryExecutorRequestCustomizer.DEFAULT_REQUEST_CUSTOMIZER) : DgsQueryExecutor {
class SpringGraphQLDgsQueryExecutor(private val executionService: ExecutionGraphQlService, private val dgsContextBuilder: DefaultDgsGraphQLContextBuilder, private val dgsDataLoaderProvider: DgsDataLoaderProvider, private val requestCustomizer: DgsQueryExecutorRequestCustomizer = DgsQueryExecutorRequestCustomizer.DEFAULT_REQUEST_CUSTOMIZER) : DgsQueryExecutor {

override fun execute(
query: String,
Expand All @@ -60,8 +58,8 @@ class SpringGraphQLDgsQueryExecutor(val executionService: ExecutionGraphQlServic

val httpRequest = requestCustomizer.apply(webRequest ?: RequestContextHolder.getRequestAttributes() as? WebRequest, headers)
val dgsContext = dgsContextBuilder.build(DgsWebMvcRequestData(request.extensions, headers, httpRequest))
val graphQLContextFuture = CompletableFuture<GraphQLContext>()
val dataLoaderRegistry = dgsDataLoaderProvider.buildRegistryWithContextSupplier { graphQLContextFuture.get() }
lateinit var graphQLContext: GraphQLContext
val dataLoaderRegistry = dgsDataLoaderProvider.buildRegistryWithContextSupplier { graphQLContext }

request.configureExecutionInput { _, builder ->
builder
Expand All @@ -70,16 +68,11 @@ class SpringGraphQLDgsQueryExecutor(val executionService: ExecutionGraphQlServic
.dataLoaderRegistry(dataLoaderRegistry).build()
}

graphQLContextFuture.complete(request.toExecutionInput().graphQLContext)
graphQLContext = request.toExecutionInput().graphQLContext

val execute = executionService.execute(
request
).block() ?: throw IllegalStateException("Unexpected null response from Spring GraphQL client")
val response = executionService.execute(request).block() ?: throw IllegalStateException("Unexpected null response from Spring GraphQL client")

return ExecutionResult.newExecutionResult()
.data(execute.getData())
.errors(execute.errors.map { GraphQLError.newError().message(it.message).build() })
.build()
return response.executionResult
}

override fun <T : Any?> executeAndExtractJsonPath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ import com.netflix.graphql.dgs.reactive.internal.DefaultDgsReactiveGraphQLContex
import com.netflix.graphql.dgs.reactive.internal.DgsReactiveRequestData
import graphql.ExecutionResult
import graphql.GraphQLContext
import graphql.GraphQLError
import org.intellij.lang.annotations.Language
import org.springframework.graphql.ExecutionGraphQlService
import org.springframework.graphql.support.DefaultExecutionGraphQlRequest
import org.springframework.http.HttpHeaders
import org.springframework.web.reactive.function.server.ServerRequest
import reactor.core.publisher.Mono
import java.util.concurrent.CompletableFuture

class SpringGraphQLDgsReactiveQueryExecutor(
val executionService: ExecutionGraphQlService,
private val executionService: ExecutionGraphQlService,
private val dgsContextBuilder: DefaultDgsReactiveGraphQLContextBuilder,
private val dgsDataLoaderProvider: DgsDataLoaderProvider
) : DgsReactiveQueryExecutor {
Expand All @@ -49,7 +47,7 @@ class SpringGraphQLDgsReactiveQueryExecutor(
extensions: Map<String, Any>?,
headers: HttpHeaders?,
operationName: String?,
serverRequest: org.springframework.web.reactive.function.server.ServerRequest?
serverRequest: ServerRequest?
): Mono<ExecutionResult> {
val request = DefaultExecutionGraphQlRequest(
query,
Expand All @@ -60,8 +58,8 @@ class SpringGraphQLDgsReactiveQueryExecutor(
null
)

val graphQLContextFuture = CompletableFuture<GraphQLContext>()
val dataLoaderRegistry = dgsDataLoaderProvider.buildRegistryWithContextSupplier { graphQLContextFuture.get() }
lateinit var graphQLContext: GraphQLContext
val dataLoaderRegistry = dgsDataLoaderProvider.buildRegistryWithContextSupplier { graphQLContext }
return dgsContextBuilder.build(DgsReactiveRequestData(request.extensions, headers, serverRequest))
.flatMap { context ->
request.configureExecutionInput { _, builder ->
Expand All @@ -71,16 +69,13 @@ class SpringGraphQLDgsReactiveQueryExecutor(
.dataLoaderRegistry(dataLoaderRegistry).build()
}

graphQLContextFuture.complete(request.toExecutionInput().graphQLContext)
graphQLContext = request.toExecutionInput().graphQLContext

executionService.execute(
request
) ?: throw IllegalStateException("Unexpected null response from Spring GraphQL client")
}.map { execute ->
ExecutionResult.newExecutionResult()
.data(execute.getData())
.errors(execute.errors.map { GraphQLError.newError().message(it.message).build() })
.build()
) ?: Mono.error(IllegalStateException("Unexpected null response from Spring GraphQL client"))
}.map { response ->
response.executionResult
}
}

Expand Down
Loading