Skip to content

Commit

Permalink
Recover the DgsContext as the, now deprecated, context for backwards …
Browse files Browse the repository at this point in the history
…compatibility
  • Loading branch information
berngp committed Apr 27, 2022
1 parent a261166 commit d35b05c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
Expand Up @@ -29,9 +29,13 @@ import java.util.function.Consumer
* A WebClient instance configured for the graphql endpoint (at least an url) must be provided.
*
* Example:
* WebClientGraphQLClient webClientGraphQLClient = new WebClientGraphQLClient(WebClient.create("http://localhost:8080/graphql"));
* GraphQLResponse message = webClientGraphQLClient.reactiveExecuteQuery("{hello}").map(r -> r.extractValue<String>("hello"));
* ```java
* WebClientGraphQLClient webClientGraphQLClient =
* new WebClientGraphQLClient(WebClient.create("http://localhost:8080/graphql"));
* GraphQLResponse message = webClientGraphQLClient.reactiveExecuteQuery("{hello}")
* .map(r -> r.extractValue<String>("hello"));
* message.subscribe();
* ```
*/
class WebClientGraphQLClient(
private val webclient: WebClient,
Expand Down
Expand Up @@ -63,9 +63,7 @@ import kotlin.streams.toList
@Configuration
@EnableConfigurationProperties(value = [DgsConfigurationProperties::class, DgsIntrospectionConfigurationProperties::class])
@ImportAutoConfiguration(classes = [JacksonAutoConfiguration::class])
open class DgsAutoConfiguration(
private val configProps: DgsConfigurationProperties
) {
open class DgsAutoConfiguration(private val configProps: DgsConfigurationProperties) {

companion object {
const val AUTO_CONF_PREFIX = "dgs.graphql"
Expand Down Expand Up @@ -163,17 +161,17 @@ open class DgsAutoConfiguration(
defaultDataFetcherFactory: Optional<DataFetcherFactory<*>> = Optional.empty()
): DgsSchemaProvider {
return DgsSchemaProvider(
applicationContext,
federationResolver,
existingTypeDefinitionFactory,
mockProviders,
configProps.schemaLocations,
dataFetcherResultProcessors,
dataFetcherExceptionHandler,
cookieValueResolver,
inputObjectMapper.orElse(DefaultInputObjectMapper()),
entityFetcherRegistry,
defaultDataFetcherFactory
applicationContext = applicationContext,
federationResolver = federationResolver,
existingTypeDefinitionRegistry = existingTypeDefinitionFactory,
mockProviders = mockProviders,
schemaLocations = configProps.schemaLocations,
dataFetcherResultProcessors = dataFetcherResultProcessors,
dataFetcherExceptionHandler = dataFetcherExceptionHandler,
cookieValueResolver = cookieValueResolver,
inputObjectMapper = inputObjectMapper.orElse(DefaultInputObjectMapper()),
entityFetcherRegistry = entityFetcherRegistry,
defaultDataFetcherFactory = defaultDataFetcherFactory
)
}

Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.netflix.graphql.dgs.context

import com.netflix.graphql.dgs.internal.DgsRequestData
import graphql.ExecutionInput
import graphql.GraphQLContext
import graphql.schema.DataFetchingEnvironment
import org.dataloader.BatchLoaderEnvironment
Expand All @@ -28,22 +29,31 @@ import java.util.function.Consumer
*/
open class DgsContext(val customContext: Any? = null, val requestData: DgsRequestData?) : Consumer<GraphQLContext.Builder> {

companion object {
private enum class GraphQLContextKey { DGS_CONTEXT_KEY }

private const val GRAPHQL_CONTEXT_NAMESPACE_KEY = "netflix.graphql.dgs"
private const val CONTEXT_KEY = "$GRAPHQL_CONTEXT_NAMESPACE_KEY.context"
companion object {
@JvmStatic
fun from(graphQLContext: GraphQLContext): DgsContext {
return graphQLContext[GraphQLContextKey.DGS_CONTEXT_KEY]
}

@JvmStatic
fun from(dfe: DataFetchingEnvironment): DgsContext {
return dfe.graphQlContext[CONTEXT_KEY]
return from(dfe.graphQlContext)
}

@JvmStatic
fun from(ei: ExecutionInput): DgsContext {
return from(ei.graphQLContext)
}

@JvmStatic
fun <T> getCustomContext(dgsContext: Any): T {
fun <T> getCustomContext(context: Any): T {
@Suppress("UNCHECKED_CAST")
return when (dgsContext) {
is DgsContext -> dgsContext.customContext as T
else -> throw RuntimeException("The context object passed to getCustomContext is not a DgsContext. It is a ${dgsContext::class.java.name} instead.")
return when (context) {
is DgsContext -> context.customContext as T
is GraphQLContext -> getCustomContext(from(context))
else -> throw RuntimeException("The context object passed to getCustomContext is not a DgsContext. It is a ${context::class.java.name} instead.")
}
}

Expand Down Expand Up @@ -73,6 +83,6 @@ open class DgsContext(val customContext: Any? = null, val requestData: DgsReques
}

override fun accept(contextBuilder: GraphQLContext.Builder) {
contextBuilder.put(CONTEXT_KEY, this)
contextBuilder.put(GraphQLContextKey.DGS_CONTEXT_KEY, this)
}
}
Expand Up @@ -107,6 +107,7 @@ object BaseDgsQueryExecutor {
.operationName(operationName)
.variables(variables)
.dataLoaderRegistry(dataLoaderRegistry)
.context(dgsContext)
.graphQLContext(dgsContext)
.extensions(extensions.orEmpty())

Expand Down
Expand Up @@ -23,7 +23,6 @@ import com.netflix.graphql.dgs.internal.utils.TimeTracer
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.HttpHeaders
import org.springframework.http.server.reactive.ServerHttpRequest
import org.springframework.web.context.request.WebRequest
import java.util.*

Expand Down Expand Up @@ -77,8 +76,7 @@ interface DgsRequestData {
/**
* @param extensions Optional map of extensions - useful for customized GraphQL interactions between for example a gateway and dgs.
* @param headers Http Headers
* @param webRequest Spring [WebRequest]. This will only be available when deployed in a WebMVC (Servlet based) environment. See [serverRequest] for the WebFlux version.
* @param serverRequest Spring reactive [ServerHttpRequest]. This will only be available when deployed in a WebFlux (non-Servlet) environment. See [webRequest] for the WebMVC version.
* @param webRequest Spring [WebRequest]. This will only be available when deployed in a WebMVC (Servlet based) environment.
*/
data class DgsWebMvcRequestData(
override val extensions: Map<String, Any>? = null,
Expand Down

0 comments on commit d35b05c

Please sign in to comment.