|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.graphql.execution; |
| 18 | + |
| 19 | +import graphql.GraphQLContext; |
| 20 | +import io.micrometer.context.ContextSnapshot; |
| 21 | +import io.micrometer.context.ContextSnapshotFactory; |
| 22 | +import reactor.core.publisher.Flux; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | +import reactor.core.publisher.Sinks; |
| 25 | +import reactor.util.context.Context; |
| 26 | +import reactor.util.context.ContextView; |
| 27 | + |
| 28 | +import org.springframework.lang.Nullable; |
| 29 | + |
| 30 | +/** |
| 31 | + * Helper for propagating context values from and to Reactor and GraphQL contexts. |
| 32 | + * |
| 33 | + * @author Rossen Stoyanchev |
| 34 | + * @author Brian Clozel |
| 35 | + * @since 1.3.5 |
| 36 | + */ |
| 37 | +public abstract class ContextPropagationHelper { |
| 38 | + |
| 39 | + private static final ContextSnapshotFactory sharedInstance = ContextSnapshotFactory.builder().build(); |
| 40 | + |
| 41 | + private static final String CONTEXT_SNAPSHOT_FACTORY_KEY = ContextPropagationHelper.class.getName() + ".KEY"; |
| 42 | + |
| 43 | + private static final String CANCEL_PUBLISHER_KEY = ContextPropagationHelper.class.getName() + ".cancelled"; |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * Select a {@code ContextSnapshotFactory} instance to use, either the one |
| 48 | + * passed in if it is not {@code null}, or a shared, static instance. |
| 49 | + * @param factory the candidate factory instance to use if not {@code null} |
| 50 | + * @return the instance to use |
| 51 | + */ |
| 52 | + public static ContextSnapshotFactory selectInstance(@Nullable ContextSnapshotFactory factory) { |
| 53 | + if (factory != null) { |
| 54 | + return factory; |
| 55 | + } |
| 56 | + return sharedInstance; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Save the {@code ContextSnapshotFactory} in the given {@link Context}. |
| 61 | + * @param factory the instance to save |
| 62 | + * @param context the context to save the instance to |
| 63 | + * @return a new context with the saved instance |
| 64 | + */ |
| 65 | + public static Context saveInstance(ContextSnapshotFactory factory, Context context) { |
| 66 | + return context.put(CONTEXT_SNAPSHOT_FACTORY_KEY, factory); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Save the {@code ContextSnapshotFactory} in the given {@link Context}. |
| 71 | + * @param factory the instance to save |
| 72 | + * @param context the context to save the instance to |
| 73 | + */ |
| 74 | + public static void saveInstance(ContextSnapshotFactory factory, GraphQLContext context) { |
| 75 | + context.put(CONTEXT_SNAPSHOT_FACTORY_KEY, factory); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Access the {@code ContextSnapshotFactory} from the given {@link ContextView} |
| 80 | + * or return a shared, static instance. |
| 81 | + * @param contextView the context where the instance is saved |
| 82 | + * @return the instance to use |
| 83 | + */ |
| 84 | + public static ContextSnapshotFactory getInstance(ContextView contextView) { |
| 85 | + ContextSnapshotFactory factory = contextView.getOrDefault(CONTEXT_SNAPSHOT_FACTORY_KEY, null); |
| 86 | + return selectInstance(factory); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Access the {@code ContextSnapshotFactory} from the given {@link GraphQLContext} |
| 91 | + * or return a shared, static instance. |
| 92 | + * @param context the context where the instance is saved |
| 93 | + * @return the instance to use |
| 94 | + */ |
| 95 | + public static ContextSnapshotFactory getInstance(GraphQLContext context) { |
| 96 | + ContextSnapshotFactory factory = context.get(CONTEXT_SNAPSHOT_FACTORY_KEY); |
| 97 | + return selectInstance(factory); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Shortcut to obtain the {@code ContextSnapshotFactory} instance, and to |
| 102 | + * capture from the given {@link ContextView}. |
| 103 | + * @param contextView the context to capture from |
| 104 | + * @return a snapshot from the capture |
| 105 | + */ |
| 106 | + public static ContextSnapshot captureFrom(ContextView contextView) { |
| 107 | + ContextSnapshotFactory factory = getInstance(contextView); |
| 108 | + return selectInstance(factory).captureFrom(contextView); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Shortcut to obtain the {@code ContextSnapshotFactory} instance, and to |
| 113 | + * capture from the given {@link GraphQLContext}. |
| 114 | + * @param context the context to capture from |
| 115 | + * @return a snapshot from the capture |
| 116 | + */ |
| 117 | + public static ContextSnapshot captureFrom(GraphQLContext context) { |
| 118 | + ContextSnapshotFactory factory = getInstance(context); |
| 119 | + return selectInstance(factory).captureFrom(context); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Create a publisher and store it into the given {@link GraphQLContext}. |
| 124 | + * This publisher can then be used to propagate cancel signals to upstream publishers. |
| 125 | + * @param context the current GraphQL context |
| 126 | + * @since 1.3.5 |
| 127 | + */ |
| 128 | + public static Sinks.Empty<Void> createCancelPublisher(GraphQLContext context) { |
| 129 | + Sinks.Empty<Void> requestCancelled = Sinks.empty(); |
| 130 | + context.put(CANCEL_PUBLISHER_KEY, requestCancelled.asMono()); |
| 131 | + return requestCancelled; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Bind the source {@link Mono} to the publisher from the given {@link GraphQLContext}. |
| 136 | + * The returned {@code Mono} will be cancelled when this publisher completes. |
| 137 | + * Subscribers must use the returned {@code Mono} instance. |
| 138 | + * @param source the source {@code Mono} |
| 139 | + * @param context the current GraphQL context |
| 140 | + * @param <T> the type of published elements |
| 141 | + * @return the new {@code Mono} that will be cancelled when notified |
| 142 | + * @since 1.3.5 |
| 143 | + */ |
| 144 | + public static <T> Mono<T> bindCancelFrom(Mono<T> source, GraphQLContext context) { |
| 145 | + Mono<Void> cancelSignal = context.get(CANCEL_PUBLISHER_KEY); |
| 146 | + if (cancelSignal != null) { |
| 147 | + return source.takeUntilOther(cancelSignal); |
| 148 | + } |
| 149 | + return source; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Bind the source {@link Flux} to the publisher from the given {@link GraphQLContext}. |
| 154 | + * The returned {@code Flux} will be cancelled when this publisher completes. |
| 155 | + * Subscribers must use the returned {@code Mono} instance. |
| 156 | + * @param source the source {@code Mono} |
| 157 | + * @param context the current GraphQL context |
| 158 | + * @param <T> the type of published elements |
| 159 | + * @return the new {@code Mono} that will be cancelled when notified |
| 160 | + * @since 1.3.5 |
| 161 | + */ |
| 162 | + public static <T> Flux<T> bindCancelFrom(Flux<T> source, GraphQLContext context) { |
| 163 | + Mono<Void> cancelSignal = context.get(CANCEL_PUBLISHER_KEY); |
| 164 | + if (cancelSignal != null) { |
| 165 | + return source.takeUntilOther(cancelSignal); |
| 166 | + } |
| 167 | + return source; |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments