|
19 | 19 | import java.time.Duration;
|
20 | 20 | import java.util.Collections;
|
21 | 21 | import java.util.List;
|
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.function.BiConsumer; |
22 | 24 |
|
23 | 25 | import graphql.ExecutionInput;
|
24 | 26 | import graphql.ExecutionResult;
|
25 | 27 | import graphql.GraphQL;
|
26 | 28 | import graphql.GraphQLError;
|
27 | 29 | import graphql.GraphqlErrorBuilder;
|
| 30 | +import graphql.schema.DataFetcher; |
| 31 | +import graphql.schema.DataFetcherFactories; |
| 32 | +import graphql.schema.GraphQLFieldDefinition; |
| 33 | +import graphql.schema.idl.SchemaDirectiveWiring; |
| 34 | +import graphql.schema.idl.SchemaDirectiveWiringEnvironment; |
28 | 35 | import org.junit.jupiter.api.Test;
|
29 | 36 | import reactor.core.publisher.Flux;
|
30 | 37 | import reactor.core.publisher.Mono;
|
|
42 | 49 | * Tests for {@link ContextDataFetcherDecorator}.
|
43 | 50 | * @author Rossen Stoyanchev
|
44 | 51 | */
|
| 52 | +@SuppressWarnings("ReactiveStreamsUnusedPublisher") |
45 | 53 | public class ContextDataFetcherDecoratorTests {
|
46 | 54 |
|
47 |
| - private static final String SCHEMA_CONTENT = |
48 |
| - "type Query { greeting: String, greetings: [String] } type Subscription { greetings: String }"; |
| 55 | + private static final String SCHEMA_CONTENT = "" + |
| 56 | + "directive @UpperCase on FIELD_DEFINITION " + |
| 57 | + "type Query { " + |
| 58 | + " greeting: String @UpperCase, " + |
| 59 | + " greetings: [String] " + |
| 60 | + "} " + |
| 61 | + "type Subscription { " + |
| 62 | + " greetings: String " + |
| 63 | + "}"; |
49 | 64 |
|
50 | 65 |
|
51 | 66 | @Test
|
@@ -112,7 +127,7 @@ void fluxDataFetcherSubscription() throws Exception {
|
112 | 127 | }
|
113 | 128 |
|
114 | 129 | @Test
|
115 |
| - void fluxDataFetcherSubscriptionThrowException() throws Exception { |
| 130 | + void fluxDataFetcherSubscriptionThrowingException() throws Exception { |
116 | 131 |
|
117 | 132 | SubscriptionExceptionResolver resolver =
|
118 | 133 | SubscriptionExceptionResolver.forSingleError(exception ->
|
@@ -177,4 +192,51 @@ void dataFetcherWithThreadLocalContext() {
|
177 | 192 | }
|
178 | 193 | }
|
179 | 194 |
|
| 195 | + @Test // gh-440 |
| 196 | + void dataFetcherDecoratedWithDataFetcherFactories() { |
| 197 | + |
| 198 | + SchemaDirectiveWiring directiveWiring = new SchemaDirectiveWiring() { |
| 199 | + |
| 200 | + @SuppressWarnings("unchecked") |
| 201 | + @Override |
| 202 | + public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> env) { |
| 203 | + if (env.getDirective("UpperCase") != null) { |
| 204 | + return env.setFieldDataFetcher(DataFetcherFactories.wrapDataFetcher( |
| 205 | + env.getFieldDataFetcher(), |
| 206 | + ((dataFetchingEnv, value) -> { |
| 207 | + if (value instanceof String) { |
| 208 | + return ((String) value).toUpperCase(); |
| 209 | + } |
| 210 | + else if (value instanceof Mono) { |
| 211 | + return ((Mono<String>) value).map(String::toUpperCase); |
| 212 | + } |
| 213 | + else { |
| 214 | + throw new IllegalArgumentException(); |
| 215 | + } |
| 216 | + }))); |
| 217 | + } |
| 218 | + else { |
| 219 | + return env.getElement(); |
| 220 | + } |
| 221 | + } |
| 222 | + }; |
| 223 | + |
| 224 | + BiConsumer<SchemaDirectiveWiring, DataFetcher<?>> tester = (schemaDirectiveWiring, dataFetcher) -> { |
| 225 | + |
| 226 | + GraphQL graphQl = GraphQlSetup.schemaContent(SCHEMA_CONTENT) |
| 227 | + .queryFetcher("greeting", dataFetcher) |
| 228 | + .runtimeWiring(builder -> builder.directiveWiring(directiveWiring)) |
| 229 | + .toGraphQl(); |
| 230 | + |
| 231 | + ExecutionInput input = ExecutionInput.newExecutionInput().query("{ greeting }").build(); |
| 232 | + Mono<ExecutionResult> resultMono = Mono.fromFuture(graphQl.executeAsync(input)); |
| 233 | + |
| 234 | + String greeting = ResponseHelper.forResult(resultMono).toEntity("greeting", String.class); |
| 235 | + assertThat(greeting).isEqualTo("HELLO"); |
| 236 | + }; |
| 237 | + |
| 238 | + tester.accept(directiveWiring, env -> CompletableFuture.completedFuture("hello")); |
| 239 | + tester.accept(directiveWiring, env -> Mono.just("hello")); |
| 240 | + } |
| 241 | + |
180 | 242 | }
|
0 commit comments