Skip to content

Commit

Permalink
Add test case for gh-761
Browse files Browse the repository at this point in the history
This commit also ensures that a new local context is created, copying
the existing values. This avoids mutating the parent local context and
polluting it with local values.
This could cause unintended side effects on other child datafetchers.

Fixes gh-761
  • Loading branch information
bclozel committed Aug 16, 2023
1 parent 58bd58b commit 94ba9fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ else if (result.getLocalContext() instanceof GraphQLContext) {
}
else {
GraphQLContext localContext = dataFetcherLocalContext == null ?
GraphQLContext.getDefault() : dataFetcherLocalContext;
GraphQLContext.getDefault() : GraphQLContext.newContext().of(dataFetcherLocalContext).build();
return DataFetcherResult.newResult()
.data(value)
.localContext(localContext.put(ObservationThreadLocalAccessor.KEY, dataFetcherObservation))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.graphql.observation;

import graphql.GraphQLContext;
import graphql.GraphqlErrorBuilder;
import graphql.execution.DataFetcherResult;
import graphql.schema.AsyncDataFetcher;
Expand Down Expand Up @@ -279,4 +280,39 @@ void currentObservationSetInDataFetcherContext() {
ResponseHelper.forResponse(responseMono);
}

@Test
void shouldNotOverrideExistingLocalContext() {

String document = """
{
bookById(id: 1) {
author {
firstName,
lastName
}
}
}
""";
DataFetcher<DataFetcherResult<Object>> bookDataFetcher = environment -> DataFetcherResult.newResult()
.data(BookSource.getBook(1L))
.localContext(GraphQLContext.newContext().of("test", "value").build())
.build();
DataFetcher<Author> authorDataFetcher = environment -> BookSource.getAuthor(101L);
DataFetcher<String> authorFirstNameDataFetcher = environment -> {
GraphQLContext context = environment.getLocalContext();
String value = context.get("test");
assertThat(value).isEqualTo("value");
return BookSource.getAuthor(101L).getFirstName();
};

ExecutionGraphQlRequest request = TestExecutionRequest.forDocument(document);
Mono<ExecutionGraphQlResponse> responseMono = graphQlSetup
.queryFetcher("bookById", bookDataFetcher)
.dataFetcher("Book", "author", authorDataFetcher)
.dataFetcher("Author", "firstName", authorFirstNameDataFetcher)
.toGraphQlService()
.execute(request);
ResponseHelper.forResponse(responseMono);
}

}

0 comments on commit 94ba9fe

Please sign in to comment.