Skip to content

Commit d30429f

Browse files
kilinkbclozel
authored andcommitted
Fix dropping of extensions in ContextDataFetcherDecorator
ContextDataFetcherDecorator was not retaining extensions for DataFetchers returning a DataFetcherResult instance. Closes gh-1199 Signed-off-by: Patrick Strawderman <[email protected]> [[email protected]: apply code conventions] Signed-off-by: Brian Clozel <[email protected]>
1 parent ccf9f8a commit d30429f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ public Object get(DataFetchingEnvironment env) throws Exception {
8787
Object value = snapshot.wrap(() -> this.delegate.get(env)).call();
8888

8989
if (value instanceof DataFetcherResult<?> dataFetcherResult) {
90-
Object adapted = updateValue(dataFetcherResult.getData(), snapshot, graphQlContext);
91-
value = DataFetcherResult.newResult()
92-
.data(adapted)
93-
.errors(dataFetcherResult.getErrors())
94-
.localContext(dataFetcherResult.getLocalContext()).build();
90+
value = dataFetcherResult.map((data) -> updateValue(data, snapshot, graphQlContext));
9591
}
9692
else {
9793
value = updateValue(value, snapshot, graphQlContext);

spring-graphql/src/test/java/org/springframework/graphql/execution/ContextDataFetcherDecoratorTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020
import java.util.Collections;
2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.concurrent.CompletableFuture;
2324
import java.util.concurrent.atomic.AtomicBoolean;
2425
import java.util.function.BiConsumer;
@@ -347,4 +348,20 @@ void cancelFluxDataFetcherSubscriptionWhenRequestCancelled() throws Exception {
347348
assertThat(dataFetcherCancelled).isTrue();
348349
}
349350

351+
@Test
352+
void testExtensionsAreRetained() throws Exception {
353+
GraphQL graphQl = GraphQlSetup.schemaContent(SCHEMA_CONTENT)
354+
.queryFetcher("greeting", (env) ->
355+
DataFetcherResult.newResult().data("Hello")
356+
.extensions(Map.of("foo", "bar")).build())
357+
.toGraphQl();
358+
359+
ExecutionInput input = ExecutionInput.newExecutionInput().query("{ greeting }").build();
360+
ExecutionResult executionResult = graphQl.executeAsync(input).get();
361+
362+
String greeting = ResponseHelper.forResult(executionResult).toEntity("greeting", String.class);
363+
assertThat(greeting).isEqualTo("Hello");
364+
365+
assertThat(executionResult.getExtensions()).containsEntry("foo", "bar");
366+
}
350367
}

0 commit comments

Comments
 (0)