diff --git a/graphql-dgs-spring-graphql-example-java/src/test/java/com/netflix/graphql/dgs/example/datafetcher/GraphQLContextContributorTest.java b/graphql-dgs-spring-graphql-example-java/src/test/java/com/netflix/graphql/dgs/example/datafetcher/GraphQLContextContributorTest.java index 18370f762..b0768210e 100644 --- a/graphql-dgs-spring-graphql-example-java/src/test/java/com/netflix/graphql/dgs/example/datafetcher/GraphQLContextContributorTest.java +++ b/graphql-dgs-spring-graphql-example-java/src/test/java/com/netflix/graphql/dgs/example/datafetcher/GraphQLContextContributorTest.java @@ -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; diff --git a/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/SpringGraphQLDgsQueryExecutor.kt b/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/SpringGraphQLDgsQueryExecutor.kt index 127df774b..9f4050baf 100644 --- a/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/SpringGraphQLDgsQueryExecutor.kt +++ b/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/SpringGraphQLDgsQueryExecutor.kt @@ -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 @@ -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, ) : DgsQueryExecutor { override fun execute( query: String, @@ -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 @@ -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") diff --git a/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/autoconfig/DgsSpringGraphQLAutoConfiguration.kt b/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/autoconfig/DgsSpringGraphQLAutoConfiguration.kt index b1f1e6ca5..b7d97181b 100644 --- a/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/autoconfig/DgsSpringGraphQLAutoConfiguration.kt +++ b/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/autoconfig/DgsSpringGraphQLAutoConfiguration.kt @@ -120,7 +120,6 @@ open class DgsSpringGraphQLAutoConfiguration { ): GraphQlSourceBuilderCustomizer = GraphQlSourceBuilderCustomizer { builder -> builder.configureGraphQl { graphQlBuilder -> - if (preparsedDocumentProvider.isPresent) { graphQlBuilder .preparsedDocumentProvider(preparsedDocumentProvider.get()) @@ -160,12 +159,14 @@ open class DgsSpringGraphQLAutoConfiguration { dgsContextBuilder: DefaultDgsGraphQLContextBuilder, dgsDataLoaderProvider: DgsDataLoaderProvider, requestCustomizer: ObjectProvider, + graphQLContextContributors: List, ): DgsQueryExecutor = SpringGraphQLDgsQueryExecutor( executionService, dgsContextBuilder, dgsDataLoaderProvider, requestCustomizer = requestCustomizer.getIfAvailable(DgsQueryExecutorRequestCustomizer::DEFAULT_REQUEST_CUSTOMIZER), + graphQLContextContributors, ) @Configuration(proxyBeanMethods = false) diff --git a/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/webmvc/DgsWebMvcGraphQLInterceptor.kt b/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/webmvc/DgsWebMvcGraphQLInterceptor.kt index f85e5029e..ca0092b9a 100644 --- a/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/webmvc/DgsWebMvcGraphQLInterceptor.kt +++ b/graphql-dgs-spring-graphql/src/main/kotlin/com/netflix/graphql/dgs/springgraphql/webmvc/DgsWebMvcGraphQLInterceptor.kt @@ -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 @@ -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, @@ -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 @@ -79,9 +78,6 @@ class DgsWebMvcGraphQLInterceptor( .build() } - - - return if (dgsSpringConfigurationProperties.webmvc.asyncdispatch.enabled) { chain.next(request).doFinally { if (dataLoaderRegistry is AutoCloseable) {