|
16 | 16 |
|
17 | 17 | package org.springframework.graphql.observation;
|
18 | 18 |
|
| 19 | +import graphql.GraphQLContext; |
19 | 20 | import graphql.GraphqlErrorBuilder;
|
20 | 21 | import graphql.execution.DataFetcherResult;
|
21 | 22 | import graphql.schema.AsyncDataFetcher;
|
@@ -279,4 +280,39 @@ void currentObservationSetInDataFetcherContext() {
|
279 | 280 | ResponseHelper.forResponse(responseMono);
|
280 | 281 | }
|
281 | 282 |
|
| 283 | + @Test |
| 284 | + void shouldNotOverrideExistingLocalContext() { |
| 285 | + |
| 286 | + String document = """ |
| 287 | + { |
| 288 | + bookById(id: 1) { |
| 289 | + author { |
| 290 | + firstName, |
| 291 | + lastName |
| 292 | + } |
| 293 | + } |
| 294 | + } |
| 295 | + """; |
| 296 | + DataFetcher<DataFetcherResult<Object>> bookDataFetcher = environment -> DataFetcherResult.newResult() |
| 297 | + .data(BookSource.getBook(1L)) |
| 298 | + .localContext(GraphQLContext.newContext().of("test", "value").build()) |
| 299 | + .build(); |
| 300 | + DataFetcher<Author> authorDataFetcher = environment -> BookSource.getAuthor(101L); |
| 301 | + DataFetcher<String> authorFirstNameDataFetcher = environment -> { |
| 302 | + GraphQLContext context = environment.getLocalContext(); |
| 303 | + String value = context.get("test"); |
| 304 | + assertThat(value).isEqualTo("value"); |
| 305 | + return BookSource.getAuthor(101L).getFirstName(); |
| 306 | + }; |
| 307 | + |
| 308 | + ExecutionGraphQlRequest request = TestExecutionRequest.forDocument(document); |
| 309 | + Mono<ExecutionGraphQlResponse> responseMono = graphQlSetup |
| 310 | + .queryFetcher("bookById", bookDataFetcher) |
| 311 | + .dataFetcher("Book", "author", authorDataFetcher) |
| 312 | + .dataFetcher("Author", "firstName", authorFirstNameDataFetcher) |
| 313 | + .toGraphQlService() |
| 314 | + .execute(request); |
| 315 | + ResponseHelper.forResponse(responseMono); |
| 316 | + } |
| 317 | + |
282 | 318 | }
|
0 commit comments