Skip to content

Commit 9ff1691

Browse files
committed
Upgrade to Context Propagation 1.0.3
Closes gh-717
1 parent 2d135bf commit 9ff1691

10 files changed

+38
-97
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/InvocableHandlerMethodSupport.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -26,7 +26,6 @@
2626

2727
import graphql.GraphQLContext;
2828
import io.micrometer.context.ContextSnapshot;
29-
import io.micrometer.context.ContextSnapshotFactory;
3029
import reactor.core.publisher.Mono;
3130

3231
import org.springframework.core.CoroutinesUtils;
@@ -45,8 +44,6 @@ public abstract class InvocableHandlerMethodSupport extends HandlerMethod {
4544

4645
private static final Object NO_VALUE = new Object();
4746

48-
private static final ContextSnapshotFactory SNAPSHOT_FACTORY = ContextSnapshotFactory.builder().build();
49-
5047

5148
private final boolean hasCallableReturnValue;
5249

@@ -111,12 +108,13 @@ protected Object doInvoke(GraphQLContext graphQLContext, Object... argValues) {
111108
}
112109

113110
@Nullable
111+
@SuppressWarnings("deprecation")
114112
private Object handleReturnValue(GraphQLContext graphQLContext, @Nullable Object result) {
115113
if (this.hasCallableReturnValue && result != null) {
116114
return CompletableFuture.supplyAsync(
117115
() -> {
118116
try {
119-
return SNAPSHOT_FACTORY.captureFrom(graphQLContext).wrap((Callable<?>) result).call();
117+
return ContextSnapshot.captureFrom(graphQLContext).wrap((Callable<?>) result).call();
120118
}
121119
catch (Exception ex) {
122120
throw new IllegalStateException(

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import graphql.util.TraversalControl;
3232
import graphql.util.TraverserContext;
3333
import io.micrometer.context.ContextSnapshot;
34-
import io.micrometer.context.ContextSnapshotFactory;
3534
import org.reactivestreams.Publisher;
3635
import reactor.core.publisher.Flux;
3736
import reactor.core.publisher.Mono;
@@ -58,8 +57,6 @@ final class ContextDataFetcherDecorator implements DataFetcher<Object> {
5857

5958
private final SubscriptionExceptionResolver subscriptionExceptionResolver;
6059

61-
private final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
62-
6360
private ContextDataFetcherDecorator(
6461
DataFetcher<?> delegate, boolean subscription,
6562
SubscriptionExceptionResolver subscriptionExceptionResolver) {
@@ -72,15 +69,21 @@ private ContextDataFetcherDecorator(
7269
}
7370

7471
@Override
72+
@SuppressWarnings("deprecation")
7573
public Object get(DataFetchingEnvironment environment) throws Exception {
7674

77-
ContextSnapshot snapshot;
75+
GraphQLContext context;
76+
// temporarily merge global and local graphql context until https://github.com/micrometer-metrics/context-propagation/pull/98
7877
if (environment.getLocalContext() instanceof GraphQLContext localContext) {
79-
snapshot = snapshotFactory.captureFrom(environment.getGraphQlContext(), localContext);
78+
context = GraphQLContext.newContext()
79+
.of(environment.getGraphQlContext())
80+
.of(localContext)
81+
.build();
8082
}
8183
else {
82-
snapshot = snapshotFactory.captureFrom(environment.getGraphQlContext());
84+
context = environment.getGraphQlContext();
8385
}
86+
ContextSnapshot snapshot = ContextSnapshot.captureFrom(context);
8487
Object value = snapshot.wrap(() -> this.delegate.get(environment)).call();
8588

8689
if (this.subscription) {

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -22,7 +22,6 @@
2222
import graphql.GraphQLError;
2323
import graphql.schema.DataFetchingEnvironment;
2424
import io.micrometer.context.ContextSnapshot;
25-
import io.micrometer.context.ContextSnapshotFactory;
2625
import io.micrometer.context.ThreadLocalAccessor;
2726
import org.apache.commons.logging.Log;
2827
import org.apache.commons.logging.LogFactory;
@@ -53,8 +52,6 @@ public abstract class DataFetcherExceptionResolverAdapter implements DataFetcher
5352

5453
protected final Log logger = LogFactory.getLog(getClass());
5554

56-
protected final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
57-
5855
private boolean threadLocalContextAware;
5956

6057

@@ -96,12 +93,13 @@ public final Mono<List<GraphQLError>> resolveException(Throwable ex, DataFetchin
9693
}
9794

9895
@Nullable
96+
@SuppressWarnings("deprecation")
9997
private List<GraphQLError> resolveInternal(Throwable exception, DataFetchingEnvironment env) {
10098
if (!this.threadLocalContextAware) {
10199
return resolveToMultipleErrors(exception, env);
102100
}
103101
try {
104-
return snapshotFactory.captureFrom(env.getGraphQlContext())
102+
return ContextSnapshot.captureFrom(env.getGraphQlContext())
105103
.wrap(() -> resolveToMultipleErrors(exception, env))
106104
.call();
107105
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -27,7 +27,6 @@
2727

2828
import graphql.GraphQLContext;
2929
import io.micrometer.context.ContextSnapshot;
30-
import io.micrometer.context.ContextSnapshotFactory;
3130
import org.dataloader.BatchLoaderContextProvider;
3231
import org.dataloader.BatchLoaderEnvironment;
3332
import org.dataloader.BatchLoaderWithContext;
@@ -53,16 +52,13 @@
5352
*/
5453
public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry {
5554

56-
private static final ContextSnapshotFactory SNAPSHOT_FACTORY = ContextSnapshotFactory.builder().build();
57-
5855
private final List<ReactorBatchLoader<?,?>> loaders = new ArrayList<>();
5956

6057
private final List<ReactorMappedBatchLoader<?,?>> mappedLoaders = new ArrayList<>();
6158

6259
private final Supplier<DataLoaderOptions> defaultOptionsSupplier;
6360

6461

65-
6662
/**
6763
* Default constructor
6864
*/
@@ -232,9 +228,10 @@ public DataLoaderOptions getOptions() {
232228
}
233229

234230
@Override
231+
@SuppressWarnings("deprecation")
235232
public CompletionStage<List<V>> load(List<K> keys, BatchLoaderEnvironment environment) {
236233
GraphQLContext graphQLContext = environment.getContext();
237-
ContextSnapshot snapshot = SNAPSHOT_FACTORY.captureFrom(graphQLContext);
234+
ContextSnapshot snapshot = ContextSnapshot.captureFrom(graphQLContext);
238235
try {
239236
return snapshot.wrap(() ->
240237
this.loader.apply(keys, environment)
@@ -282,9 +279,10 @@ public DataLoaderOptions getOptions() {
282279
}
283280

284281
@Override
282+
@SuppressWarnings("deprecation")
285283
public CompletionStage<Map<K, V>> load(Set<K> keys, BatchLoaderEnvironment environment) {
286284
GraphQLContext graphQLContext = environment.getContext();
287-
ContextSnapshot snapshot = SNAPSHOT_FACTORY.captureFrom(graphQLContext);
285+
ContextSnapshot snapshot = ContextSnapshot.captureFrom(graphQLContext);
288286
try {
289287
return snapshot.wrap(() ->
290288
this.loader.apply(keys, environment)

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -25,7 +25,6 @@
2525
import graphql.GraphQLContext;
2626
import graphql.execution.ExecutionIdProvider;
2727
import io.micrometer.context.ContextSnapshot;
28-
import io.micrometer.context.ContextSnapshotFactory;
2928
import org.dataloader.DataLoaderRegistry;
3029
import reactor.core.publisher.Mono;
3130

@@ -46,7 +45,6 @@ public class DefaultExecutionGraphQlService implements ExecutionGraphQlService {
4645
private static final BiFunction<ExecutionInput, ExecutionInput.Builder, ExecutionInput> RESET_EXECUTION_ID_CONFIGURER =
4746
(executionInput, builder) -> builder.executionId(null).build();
4847

49-
private final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
5048

5149
private final GraphQlSource graphQlSource;
5250

@@ -73,13 +71,14 @@ public void addDataLoaderRegistrar(DataLoaderRegistrar registrar) {
7371

7472

7573
@Override
74+
@SuppressWarnings("deprecation")
7675
public final Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest request) {
7776
return Mono.deferContextual((contextView) -> {
7877
if (!this.isDefaultExecutionIdProvider && request.getExecutionId() == null) {
7978
request.configureExecutionInput(RESET_EXECUTION_ID_CONFIGURER);
8079
}
8180
ExecutionInput executionInput = request.toExecutionInput();
82-
snapshotFactory.captureFrom(contextView).updateContext(executionInput.getGraphQLContext());
81+
ContextSnapshot.captureFrom(contextView).updateContext(executionInput.getGraphQLContext());
8382
ExecutionInput updatedExecutionInput = registerDataLoaders(executionInput);
8483
return Mono.fromFuture(this.graphQlSource.graphQl().executeAsync(updatedExecutionInput))
8584
.map(result -> new DefaultExecutionGraphQlResponse(updatedExecutionInput, result));

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import graphql.execution.ExecutionId;
3030
import graphql.schema.DataFetchingEnvironment;
3131
import io.micrometer.context.ContextSnapshot;
32-
import io.micrometer.context.ContextSnapshotFactory;
3332
import org.apache.commons.logging.Log;
3433
import org.apache.commons.logging.LogFactory;
3534
import reactor.core.publisher.Flux;
@@ -48,8 +47,6 @@ class ExceptionResolversExceptionHandler implements DataFetcherExceptionHandler
4847

4948
private static final Log logger = LogFactory.getLog(ExceptionResolversExceptionHandler.class);
5049

51-
private final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
52-
5350
private final List<DataFetcherExceptionResolver> resolvers;
5451

5552
/**
@@ -63,10 +60,11 @@ class ExceptionResolversExceptionHandler implements DataFetcherExceptionHandler
6360

6461

6562
@Override
63+
@SuppressWarnings("deprecation")
6664
public CompletableFuture<DataFetcherExceptionHandlerResult> handleException(DataFetcherExceptionHandlerParameters params) {
6765
Throwable exception = unwrapException(params);
6866
DataFetchingEnvironment env = params.getDataFetchingEnvironment();
69-
ContextSnapshot snapshot = snapshotFactory.captureFrom(env.getGraphQlContext());
67+
ContextSnapshot snapshot = ContextSnapshot.captureFrom(env.getGraphQlContext());
7068
try {
7169
return Flux.fromIterable(this.resolvers)
7270
.flatMap(resolver -> resolver.resolveException(exception, env))

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

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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.
@@ -72,31 +72,13 @@ private <V> void setValueInternal(Object value) {
7272
}
7373

7474
@Override
75-
public void setValue() {
76-
this.delegate.setValue();
77-
}
78-
79-
@Override
80-
@Deprecated
75+
@SuppressWarnings("deprecation")
8176
public void reset() {
8277
this.delegate.reset();
8378
}
8479

85-
@Override
86-
public void restore(Object previousValue) {
87-
restoreInternal(previousValue);
88-
}
89-
90-
@SuppressWarnings("unchecked")
91-
public <V> void restoreInternal(Object previousValue) {
92-
((ThreadLocalAccessor<V>) this.delegate).restore((V) previousValue);
93-
}
94-
95-
@Override
96-
public void restore() {
97-
this.delegate.restore();
98-
}
99-
80+
81+
@SuppressWarnings("deprecation")
10082
private static class DelegateAccessor implements ThreadLocalAccessor<Object> {
10183

10284
@Override
@@ -115,29 +97,14 @@ public void setValue(Object value) {
11597
}
11698

11799
@Override
118-
public void setValue() {
119-
SecurityContextHolder.clearContext();
120-
}
121-
122-
@Override
123-
public void restore(Object previousValue) {
124-
SecurityContextHolder.setContext((SecurityContext) previousValue);
125-
}
126-
127-
@Override
128-
public void restore() {
129-
SecurityContextHolder.clearContext();
130-
}
131-
132-
@Override
133-
@Deprecated
134100
public void reset() {
135101
SecurityContextHolder.clearContext();
136102
}
137103

138104
}
139105

140106

107+
@SuppressWarnings("deprecation")
141108
private static class NoOpAccessor implements ThreadLocalAccessor<Object> {
142109

143110
@Override
@@ -155,19 +122,6 @@ public void setValue(Object value) {
155122
}
156123

157124
@Override
158-
public void setValue() {
159-
}
160-
161-
@Override
162-
public void restore(Object previousValue) {
163-
}
164-
165-
@Override
166-
public void restore() {
167-
}
168-
169-
@Override
170-
@Deprecated
171125
public void reset() {
172126
}
173127

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -22,7 +22,6 @@
2222

2323
import graphql.GraphQLError;
2424
import io.micrometer.context.ContextSnapshot;
25-
import io.micrometer.context.ContextSnapshotFactory;
2625
import io.micrometer.context.ThreadLocalAccessor;
2726
import org.apache.commons.logging.Log;
2827
import org.apache.commons.logging.LogFactory;
@@ -51,8 +50,6 @@ public abstract class SubscriptionExceptionResolverAdapter implements Subscripti
5150

5251
protected final Log logger = LogFactory.getLog(getClass());
5352

54-
protected final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
55-
5653
private boolean threadLocalContextAware;
5754

5855

@@ -81,12 +78,12 @@ public boolean isThreadLocalContextAware() {
8178
}
8279

8380

84-
@SuppressWarnings({"unused", "try"})
81+
@SuppressWarnings({"unused", "try", "deprecation"})
8582
@Override
8683
public final Mono<List<GraphQLError>> resolveException(Throwable exception) {
8784
if (this.threadLocalContextAware) {
8885
return Mono.deferContextual(contextView -> {
89-
ContextSnapshot snapshot = snapshotFactory.captureFrom(contextView);
86+
ContextSnapshot snapshot = ContextSnapshot.captureFrom(contextView);
9087
try {
9188
List<GraphQLError> errors = snapshot.wrap(() -> resolveToMultipleErrors(exception)).call();
9289
return Mono.justOrEmpty(errors);

0 commit comments

Comments
 (0)