Skip to content

Commit 0948e61

Browse files
committed
Merge branch '1.3.x'
2 parents 16aa33a + af74edb commit 0948e61

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,8 +22,11 @@
2222
import java.util.function.BiFunction;
2323

2424
import graphql.ExecutionInput;
25+
import graphql.ExecutionResult;
2526
import graphql.GraphQL;
2627
import graphql.GraphQLContext;
28+
import graphql.GraphQLError;
29+
import graphql.GraphQLException;
2730
import graphql.execution.ExecutionIdProvider;
2831
import graphql.execution.instrumentation.dataloader.EmptyDataLoaderRegistryInstance;
2932
import io.micrometer.context.ContextSnapshotFactory;
@@ -102,6 +105,12 @@ public final Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest requ
102105
ExecutionInput executionInputToUse = registerDataLoaders(executionInput);
103106

104107
return Mono.fromFuture(this.graphQlSource.graphQl().executeAsync(executionInputToUse))
108+
.onErrorResume(GraphQLException.class, (exception) -> {
109+
if (exception instanceof GraphQLError graphQLError) {
110+
return Mono.just(ExecutionResult.newExecutionResult().addError(graphQLError).build());
111+
}
112+
return Mono.error(exception);
113+
})
105114
.map((result) -> new DefaultExecutionGraphQlResponse(executionInputToUse, result));
106115
});
107116
}

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.Map;
2020

21+
import graphql.ErrorType;
2122
import org.dataloader.DataLoaderRegistry;
2223
import org.junit.jupiter.api.Test;
2324
import reactor.core.publisher.Flux;
@@ -28,14 +29,14 @@
2829
import org.springframework.graphql.ExecutionGraphQlResponse;
2930
import org.springframework.graphql.GraphQlSetup;
3031
import org.springframework.graphql.TestExecutionRequest;
32+
import org.springframework.graphql.support.DefaultExecutionGraphQlRequest;
3133

3234
import static org.assertj.core.api.Assertions.assertThat;
3335

3436
/**
3537
* Unit tests for {@link DefaultExecutionGraphQlService}.
3638
*
3739
* @author Rossen Stoyanchev
38-
* @since 1.2.4
3940
*/
4041
public class DefaultExecutionGraphQlServiceTests {
4142

@@ -64,4 +65,18 @@ void customDataLoaderRegistry() {
6465
assertThat(dataLoaderRegistry.getDataLoaders()).hasSize(1);
6566
}
6667

68+
@Test
69+
void shouldHandleGraphQlErrors() {
70+
GraphQlSource graphQlSource = GraphQlSetup.schemaContent("type Query { greeting: String }")
71+
.queryFetcher("greeting", (env) -> "hi")
72+
.toGraphQlSource();
73+
DefaultExecutionGraphQlService graphQlService = new DefaultExecutionGraphQlService(graphQlSource);
74+
75+
ExecutionGraphQlRequest request = new DefaultExecutionGraphQlRequest("{ greeting }", "unknown",
76+
null, null, "uniqueId", null);
77+
ExecutionGraphQlResponse response = graphQlService.execute(request).block();
78+
assertThat(response.getExecutionResult().getErrors()).singleElement()
79+
.hasFieldOrPropertyWithValue("errorType", ErrorType.ValidationError);
80+
}
81+
6782
}

0 commit comments

Comments
 (0)