Skip to content

Commit

Permalink
Call context contributors in the SpringGraphQLDgsQueryExecutor for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jjacobs44 committed Nov 20, 2024
1 parent a201aaf commit 7ddf06b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,12 @@
* limitations under the License.
*/

package com.netflix.graphql.dgs.example.datafetcher;import com.netflix.graphql.dgs.DgsQueryExecutor;
import com.netflix.graphql.dgs.autoconfig.DgsAutoConfiguration;
import com.netflix.graphql.dgs.autoconfig.DgsExtendedScalarsAutoConfiguration;
import com.netflix.graphql.dgs.example.context.MyContextBuilder;
import com.netflix.graphql.dgs.example.datafetcher.HelloDataFetcher;
import com.netflix.graphql.dgs.example.shared.context.ExampleGraphQLContextContributor;
import com.netflix.graphql.dgs.example.shared.dataLoader.ExampleLoaderWithContext;
import com.netflix.graphql.dgs.example.shared.dataLoader.ExampleLoaderWithGraphQLContext;
package com.netflix.graphql.dgs.example.datafetcher;

import com.netflix.graphql.dgs.DgsQueryExecutor;
import com.netflix.graphql.dgs.example.shared.datafetcher.RequestHeadersDataFetcher;
import com.netflix.graphql.dgs.example.shared.instrumentation.ExampleInstrumentationDependingOnContextContributor;
import com.netflix.graphql.dgs.example.shared.datafetcher.MovieDataFetcher;
import com.netflix.graphql.dgs.pagination.DgsPaginationAutoConfiguration;
import com.netflix.graphql.dgs.test.EnableDgsTest;
import graphql.scalars.ExtendedScalars;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.TypeRef
import com.jayway.jsonpath.spi.mapper.MappingException
import com.netflix.graphql.dgs.DgsQueryExecutor
import com.netflix.graphql.dgs.context.GraphQLContextContributor
import com.netflix.graphql.dgs.exceptions.DgsQueryExecutionDataExtractionException
import com.netflix.graphql.dgs.exceptions.QueryException
import com.netflix.graphql.dgs.internal.BaseDgsQueryExecutor
Expand All @@ -42,6 +43,7 @@ class SpringGraphQLDgsQueryExecutor(
private val dgsContextBuilder: DefaultDgsGraphQLContextBuilder,
private val dgsDataLoaderProvider: DgsDataLoaderProvider,
private val requestCustomizer: DgsQueryExecutorRequestCustomizer = DgsQueryExecutorRequestCustomizer.DEFAULT_REQUEST_CUSTOMIZER,
private val graphQLContextContributors: List<GraphQLContextContributor>,
) : DgsQueryExecutor {
override fun execute(
query: String,
Expand All @@ -63,8 +65,18 @@ class SpringGraphQLDgsQueryExecutor(

val httpRequest = requestCustomizer.apply(webRequest ?: RequestContextHolder.getRequestAttributes() as? WebRequest, headers)
val dgsContext = dgsContextBuilder.build(DgsWebMvcRequestData(request.extensions, headers, httpRequest))
lateinit var graphQLContext: GraphQLContext
val dataLoaderRegistry = dgsDataLoaderProvider.buildRegistryWithContextSupplier { graphQLContext }
val dataLoaderRegistry =
dgsDataLoaderProvider.buildRegistryWithContextSupplier {
val graphQLContext = request.toExecutionInput().graphQLContext
if (graphQLContextContributors.isNotEmpty()) {
val requestData = dgsContext.requestData
val builderForContributors = GraphQLContext.newContext()
graphQLContextContributors.forEach { it.contribute(builderForContributors, extensions, requestData) }
graphQLContext.putAll(builderForContributors)
}

graphQLContext
}

request.configureExecutionInput { _, builder ->
builder
Expand All @@ -74,8 +86,6 @@ class SpringGraphQLDgsQueryExecutor(
.build()
}

graphQLContext = request.toExecutionInput().graphQLContext

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ open class DgsSpringGraphQLAutoConfiguration {
): GraphQlSourceBuilderCustomizer =
GraphQlSourceBuilderCustomizer { builder ->
builder.configureGraphQl { graphQlBuilder ->

if (preparsedDocumentProvider.isPresent) {
graphQlBuilder
.preparsedDocumentProvider(preparsedDocumentProvider.get())
Expand Down Expand Up @@ -160,12 +159,14 @@ open class DgsSpringGraphQLAutoConfiguration {
dgsContextBuilder: DefaultDgsGraphQLContextBuilder,
dgsDataLoaderProvider: DgsDataLoaderProvider,
requestCustomizer: ObjectProvider<DgsQueryExecutorRequestCustomizer>,
graphQLContextContributors: List<GraphQLContextContributor>,
): DgsQueryExecutor =
SpringGraphQLDgsQueryExecutor(
executionService,
dgsContextBuilder,
dgsDataLoaderProvider,
requestCustomizer = requestCustomizer.getIfAvailable(DgsQueryExecutorRequestCustomizer::DEFAULT_REQUEST_CUSTOMIZER),
graphQLContextContributors,
)

@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.netflix.graphql.dgs.springgraphql.webmvc

import com.netflix.graphql.dgs.context.DgsContext
import com.netflix.graphql.dgs.context.GraphQLContextContributor
import com.netflix.graphql.dgs.internal.DefaultDgsGraphQLContextBuilder
import com.netflix.graphql.dgs.internal.DgsDataLoaderProvider
Expand All @@ -31,7 +30,6 @@ import org.springframework.web.context.request.ServletRequestAttributes
import org.springframework.web.context.request.ServletWebRequest
import org.springframework.web.context.request.WebRequest
import reactor.core.publisher.Mono
import java.util.concurrent.CompletableFuture

class DgsWebMvcGraphQLInterceptor(
private val dgsDataLoaderProvider: DgsDataLoaderProvider,
Expand All @@ -58,18 +56,19 @@ 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)
}
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
}
graphQLContext
}

request.configureExecutionInput { _, builder ->
builder
Expand All @@ -79,9 +78,6 @@ class DgsWebMvcGraphQLInterceptor(
.build()
}




return if (dgsSpringConfigurationProperties.webmvc.asyncdispatch.enabled) {
chain.next(request).doFinally {
if (dataLoaderRegistry is AutoCloseable) {
Expand Down

0 comments on commit 7ddf06b

Please sign in to comment.