Skip to content

Commit 735da03

Browse files
committed
Refactoring in ReactorContextManager
Use GraphQLContext as input instead of ExecutionInput, DataFetchingEnvironment and BatchLoaderEnvironment. See gh-316
1 parent c43a444 commit 735da03

9 files changed

+30
-45
lines changed

spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,7 +59,7 @@ private ContextDataFetcherDecorator(DataFetcher<?> delegate, boolean subscriptio
5959

6060
@Override
6161
public Object get(DataFetchingEnvironment environment) throws Exception {
62-
ContextView contextView = ReactorContextManager.getReactorContext(environment);
62+
ContextView contextView = ReactorContextManager.getReactorContext(environment.getGraphQlContext());
6363

6464
Object value;
6565
try {

spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolverAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,7 +90,7 @@ private List<GraphQLError> resolveInternal(Throwable ex, DataFetchingEnvironment
9090
if (!this.threadLocalContextAware) {
9191
return resolveToMultipleErrors(ex, env);
9292
}
93-
ContextView contextView = ReactorContextManager.getReactorContext(env);
93+
ContextView contextView = ReactorContextManager.getReactorContext(env.getGraphQlContext());
9494
try {
9595
ReactorContextManager.restoreThreadLocalValues(contextView);
9696
return resolveToMultipleErrors(ex, env);

spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public DataLoaderOptions getOptionsOrDefault(
188188

189189
@Override
190190
public CompletionStage<List<V>> load(List<K> keys, BatchLoaderEnvironment environment) {
191-
ContextView contextView = ReactorContextManager.getReactorContext(environment);
191+
ContextView contextView = ReactorContextManager.getReactorContext(environment.getContext());
192192
try {
193193
ReactorContextManager.restoreThreadLocalValues(contextView);
194194
return this.loader.apply(keys, environment).collectList().contextWrite(contextView).toFuture();
@@ -239,7 +239,7 @@ public DataLoaderOptions getOptionsOrDefault(
239239

240240
@Override
241241
public CompletionStage<Map<K, V>> load(Set<K> keys, BatchLoaderEnvironment environment) {
242-
ContextView contextView = ReactorContextManager.getReactorContext(environment);
242+
ContextView contextView = ReactorContextManager.getReactorContext(environment.getContext());
243243
try {
244244
ReactorContextManager.restoreThreadLocalValues(contextView);
245245
return this.loader.apply(keys, environment).contextWrite(contextView).toFuture();

spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultExecutionGraphQlService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public final Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest requ
7676
request.configureExecutionInput(RESET_EXECUTION_ID_CONFIGURER);
7777
}
7878
ExecutionInput executionInput = request.toExecutionInput();
79-
ReactorContextManager.setReactorContext(contextView, executionInput);
79+
ReactorContextManager.setReactorContext(contextView, executionInput.getGraphQLContext());
8080
ExecutionInput updatedExecutionInput = registerDataLoaders(executionInput);
8181
return Mono.fromFuture(this.graphQlSource.graphQl().executeAsync(updatedExecutionInput))
8282
.map(result -> new DefaultExecutionGraphQlResponse(updatedExecutionInput, result));

spring-graphql/src/main/java/org/springframework/graphql/execution/ExceptionResolversExceptionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public CompletableFuture<DataFetcherExceptionHandlerResult> handleException(Data
7878
.onErrorResume(resolverEx -> Mono.just(handleResolverError(resolverEx, exception, env)))
7979
.switchIfEmpty(Mono.fromCallable(() -> createInternalError(exception, env)))
8080
.contextWrite((context) -> {
81-
ContextView contextView = ReactorContextManager.getReactorContext(env);
81+
ContextView contextView = ReactorContextManager.getReactorContext(env.getGraphQlContext());
8282
return (contextView.isEmpty() ? context : context.putAll(contextView));
8383
})
8484
.toFuture();

spring-graphql/src/main/java/org/springframework/graphql/execution/ReactorContextManager.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,20 +19,17 @@
1919
import java.util.LinkedHashMap;
2020
import java.util.Map;
2121

22-
import graphql.ExecutionInput;
2322
import graphql.GraphQLContext;
24-
import graphql.schema.DataFetchingEnvironment;
25-
import org.dataloader.BatchLoaderEnvironment;
2623
import reactor.util.context.Context;
2724
import reactor.util.context.ContextView;
2825

2926
import org.springframework.lang.Nullable;
3027
import org.springframework.util.Assert;
3128

3229
/**
33-
* Provides helper methods to save Reactor context in the {@link ExecutionInput}
34-
* so it can be subsequently obtained from {@link DataFetchingEnvironment} and
35-
* propagated to data fetchers or exception handlers.
30+
* Provides helper methods to save Reactor context in the {@link GraphQLContext}
31+
* so it can be subsequently obtained and propagated to data fetchers, exception
32+
* handlers, and others.
3633
*
3734
* <p>The Reactor context is also used to carry ThreadLocal values that are also
3835
* restored around the execution of data fetchers and exceptions handlers.
@@ -51,36 +48,24 @@ public abstract class ReactorContextManager {
5148
private static final String THREAD_LOCAL_ACCESSOR_KEY = ReactorContextManager.class.getName() + ".THREAD_LOCAL_ACCESSOR";
5249

5350
/**
54-
* Save the given Reactor {@link ContextView} in the an {@link ExecutionInput} for
55-
* later access through the {@link DataFetchingEnvironment}.
56-
* @param contextView the reactor context view
57-
* @param input the input prepared from the GraphQL request
51+
* Save the given Reactor {@link ContextView} in the given {@link GraphQLContext}.
52+
* @param contextView the reactor {@code ContextView} to save
53+
* @param graphQLContext the {@code GraphQLContext} where to save
5854
*/
59-
static void setReactorContext(ContextView contextView, ExecutionInput input) {
60-
input.getGraphQLContext().put(CONTEXT_VIEW_KEY, contextView);
55+
static void setReactorContext(ContextView contextView, GraphQLContext graphQLContext) {
56+
graphQLContext.put(CONTEXT_VIEW_KEY, contextView);
6157
}
6258

6359
/**
64-
* Return the Reactor {@link ContextView} saved in the given DataFetchingEnvironment.
65-
* @param environment the DataFetchingEnvironment
60+
* Return the Reactor {@link ContextView} saved in the given {@link GraphQLContext}.
61+
* @param graphQlContext the DataFetchingEnvironment
6662
* @return the reactor {@link ContextView}
6763
*/
68-
static ContextView getReactorContext(DataFetchingEnvironment environment) {
69-
GraphQLContext graphQlContext = environment.getGraphQlContext();
64+
static ContextView getReactorContext(GraphQLContext graphQlContext) {
65+
Assert.notNull(graphQlContext, "GraphQLContext is required");
7066
return graphQlContext.getOrDefault(CONTEXT_VIEW_KEY, Context.empty());
7167
}
7268

73-
/**
74-
* Return the Reactor {@link ContextView} saved in the given BatchLoaderEnvironment.
75-
* @param environment the BatchLoaderEnvironment
76-
* @return the reactor {@link ContextView}
77-
*/
78-
static ContextView getReactorContext(BatchLoaderEnvironment environment) {
79-
Object context = environment.getContext();
80-
Assert.isTrue(context instanceof GraphQLContext, "Expected GraphQLContext in BatchLoaderEnvironment");
81-
return ((GraphQLContext) context).getOrDefault(CONTEXT_VIEW_KEY, Context.empty());
82-
}
83-
8469
/**
8570
* Use the given accessor to extract ThreadLocal values and save them in a
8671
* sub-map in the given {@link Context}, so those can be restored later

spring-graphql/src/test/java/org/springframework/graphql/execution/ContextDataFetcherDecoratorTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@ void monoDataFetcher() throws Exception {
5252
.toGraphQl();
5353

5454
ExecutionInput input = ExecutionInput.newExecutionInput().query("{ greeting }").build();
55-
ReactorContextManager.setReactorContext(Context.of("name", "007"), input);
55+
ReactorContextManager.setReactorContext(Context.of("name", "007"), input.getGraphQLContext());
5656

5757
ExecutionResult executionResult = graphQl.executeAsync(input).get();
5858

@@ -72,7 +72,7 @@ void fluxDataFetcher() throws Exception {
7272
.toGraphQl();
7373

7474
ExecutionInput input = ExecutionInput.newExecutionInput().query("{ greetings }").build();
75-
ReactorContextManager.setReactorContext(Context.of("name", "007"), input);
75+
ReactorContextManager.setReactorContext(Context.of("name", "007"), input.getGraphQLContext());
7676

7777
ExecutionResult result = graphQl.executeAsync(input).get();
7878

@@ -92,7 +92,7 @@ void fluxDataFetcherSubscription() throws Exception {
9292
.toGraphQl();
9393

9494
ExecutionInput input = ExecutionInput.newExecutionInput().query("subscription { greetings }").build();
95-
ReactorContextManager.setReactorContext(Context.of("name", "007"), input);
95+
ReactorContextManager.setReactorContext(Context.of("name", "007"), input.getGraphQLContext());
9696

9797
ExecutionResult executionResult = graphQl.executeAsync(input).get();
9898

@@ -116,7 +116,7 @@ void dataFetcherWithThreadLocalContext() {
116116

117117
ExecutionInput input = ExecutionInput.newExecutionInput().query("{ greeting }").build();
118118
ContextView view = ReactorContextManager.extractThreadLocalValues(accessor, Context.empty());
119-
ReactorContextManager.setReactorContext(view, input);
119+
ReactorContextManager.setReactorContext(view, input.getGraphQLContext());
120120

121121
Mono<ExecutionResult> resultMono = Mono.delay(Duration.ofMillis(10))
122122
.flatMap((aLong) -> Mono.fromFuture(graphQl.executeAsync(input)));

spring-graphql/src/test/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -111,7 +111,7 @@ void batchLoaderWithCustomNameAndOptions() {
111111

112112
private GraphQLContext initGraphQLContext(ContextView context) {
113113
ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("").build();
114-
ReactorContextManager.setReactorContext(context, executionInput);
114+
ReactorContextManager.setReactorContext(context, executionInput.getGraphQLContext());
115115
return executionInput.getGraphQLContext();
116116
}
117117

spring-graphql/src/test/java/org/springframework/graphql/execution/ExceptionResolversExceptionHandlerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void resolveExceptionWithReactorContext() throws Exception {
7676
.message("Resolved error: " + ex.getMessage() + ", name=" + view.get("name"))
7777
.errorType(ErrorType.BAD_REQUEST).build())));
7878

79-
ReactorContextManager.setReactorContext(Context.of("name", "007"), input);
79+
ReactorContextManager.setReactorContext(Context.of("name", "007"), input.getGraphQLContext());
8080

8181
ExecutionResult result = this.graphQlSetup.exceptionResolver(resolver).toGraphQl()
8282
.executeAsync(this.input).get();
@@ -102,7 +102,7 @@ void resolveExceptionWithThreadLocal() {
102102
resolver.setThreadLocalContextAware(true);
103103

104104
ContextView view = ReactorContextManager.extractThreadLocalValues(accessor, Context.empty());
105-
ReactorContextManager.setReactorContext(view, input);
105+
ReactorContextManager.setReactorContext(view, input.getGraphQLContext());
106106

107107
Mono<ExecutionResult> result = Mono.delay(Duration.ofMillis(10)).flatMap((aLong) ->
108108
Mono.fromFuture(this.graphQlSetup.exceptionResolver(resolver).toGraphQl().executeAsync(this.input)));

0 commit comments

Comments
 (0)