|
1 | 1 | /*
|
2 |
| - * Copyright 2020-2023 the original author or authors. |
| 2 | + * Copyright 2020-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
31 | 31 | import org.junit.jupiter.params.ParameterizedTest;
|
32 | 32 | import org.junit.jupiter.params.provider.Arguments;
|
33 | 33 | import org.junit.jupiter.params.provider.MethodSource;
|
34 |
| -import org.springframework.graphql.*; |
| 34 | + |
| 35 | +import org.springframework.graphql.Author; |
| 36 | +import org.springframework.graphql.Book; |
| 37 | +import org.springframework.graphql.BookSource; |
| 38 | +import org.springframework.graphql.ExecutionGraphQlRequest; |
| 39 | +import org.springframework.graphql.ExecutionGraphQlResponse; |
| 40 | +import org.springframework.graphql.GraphQlSetup; |
| 41 | +import org.springframework.graphql.ResponseHelper; |
| 42 | +import org.springframework.graphql.TestExecutionRequest; |
35 | 43 | import org.springframework.graphql.execution.DataFetcherExceptionResolver;
|
36 | 44 | import org.springframework.graphql.execution.ErrorType;
|
37 | 45 | import reactor.core.publisher.Mono;
|
@@ -319,4 +327,42 @@ void shouldNotOverrideExistingLocalContext() {
|
319 | 327 | ResponseHelper.forResponse(responseMono);
|
320 | 328 | }
|
321 | 329 |
|
| 330 | + @Test |
| 331 | + void shouldNotOverrideCustomLocalContext() { |
| 332 | + |
| 333 | + String document = """ |
| 334 | + { |
| 335 | + bookById(id: 1) { |
| 336 | + author { |
| 337 | + firstName, |
| 338 | + lastName |
| 339 | + } |
| 340 | + } |
| 341 | + } |
| 342 | + """; |
| 343 | + DataFetcher<DataFetcherResult<Object>> bookDataFetcher = environment -> DataFetcherResult.newResult() |
| 344 | + .data(BookSource.getBook(1L)) |
| 345 | + .localContext(new CustomLocalContext()) |
| 346 | + .build(); |
| 347 | + DataFetcher<Author> authorDataFetcher = environment -> BookSource.getAuthor(101L); |
| 348 | + DataFetcher<String> authorFirstNameDataFetcher = environment -> { |
| 349 | + Object context = environment.getLocalContext(); |
| 350 | + assertThat(context).isInstanceOf(CustomLocalContext.class); |
| 351 | + return BookSource.getAuthor(101L).getFirstName(); |
| 352 | + }; |
| 353 | + |
| 354 | + ExecutionGraphQlRequest request = TestExecutionRequest.forDocument(document); |
| 355 | + Mono<ExecutionGraphQlResponse> responseMono = graphQlSetup |
| 356 | + .queryFetcher("bookById", bookDataFetcher) |
| 357 | + .dataFetcher("Book", "author", authorDataFetcher) |
| 358 | + .dataFetcher("Author", "firstName", authorFirstNameDataFetcher) |
| 359 | + .toGraphQlService() |
| 360 | + .execute(request); |
| 361 | + ResponseHelper.forResponse(responseMono); |
| 362 | + } |
| 363 | + |
| 364 | + static class CustomLocalContext { |
| 365 | + |
| 366 | + } |
| 367 | + |
322 | 368 | }
|
0 commit comments