15
15
*/
16
16
package org .springframework .graphql .data .method .annotation .support ;
17
17
18
+ import java .util .Collections ;
18
19
import java .util .List ;
19
20
import java .util .concurrent .Callable ;
20
21
import java .util .concurrent .CompletableFuture ;
21
22
import java .util .concurrent .atomic .AtomicReference ;
23
+ import java .util .function .BiConsumer ;
22
24
23
25
import graphql .GraphQLContext ;
24
26
import graphql .GraphQLError ;
49
51
import org .springframework .graphql .data .method .annotation .SchemaMapping ;
50
52
import org .springframework .graphql .data .method .annotation .SubscriptionMapping ;
51
53
import org .springframework .graphql .execution .BatchLoaderRegistry ;
54
+ import org .springframework .graphql .execution .DataFetcherExceptionResolver ;
52
55
import org .springframework .graphql .execution .DefaultBatchLoaderRegistry ;
53
56
import org .springframework .graphql .execution .ErrorType ;
54
57
import org .springframework .graphql .execution .SubscriptionPublisherException ;
55
58
import org .springframework .stereotype .Controller ;
56
- import org .springframework .util .Assert ;
57
59
58
60
import static org .assertj .core .api .Assertions .assertThat ;
59
61
@@ -229,6 +231,34 @@ void handleExceptionFromQuery() {
229
231
assertThat (responseHelper .error (0 ).message ()).isEqualTo ("Rejected: Bad input" );
230
232
}
231
233
234
+ @ Test
235
+ void handleExceptionWithResolverWhenNoAnnotatedExceptionHandlerMatches () {
236
+ String document = "{ " +
237
+ " booksByCriteria(criteria: {author:\" Heller\" }) { " +
238
+ " id" +
239
+ " name" +
240
+ " }" +
241
+ "}" ;
242
+
243
+ DataFetcherExceptionResolver resolver = (ex , env ) ->
244
+ Mono .just (Collections .singletonList (
245
+ GraphQLError .newError ().errorType (ErrorType .INTERNAL_ERROR )
246
+ .message ("Rejected: " + ex .getMessage ())
247
+ .build ()));
248
+
249
+ ExecutionGraphQlService service = graphQlService ((configurer , setup ) -> {
250
+ setup .exceptionResolver (configurer .getExceptionResolver ()); // First @ControllerAdvice (no match)
251
+ setup .exceptionResolver (resolver ); // Then resolver
252
+ });
253
+
254
+ Mono <ExecutionGraphQlResponse > responseMono = service .execute (TestExecutionRequest .forDocument (document ));
255
+
256
+ ResponseHelper responseHelper = ResponseHelper .forResponse (responseMono );
257
+ assertThat (responseHelper .errorCount ()).isEqualTo (1 );
258
+ assertThat (responseHelper .error (0 ).errorType ()).isEqualTo ("INTERNAL_ERROR" );
259
+ assertThat (responseHelper .error (0 ).message ()).isEqualTo ("Rejected: Fetch failure" );
260
+ }
261
+
232
262
@ Test
233
263
void handleExceptionFromSubscription () {
234
264
String document = "subscription { " +
@@ -257,6 +287,10 @@ void handleExceptionFromSubscription() {
257
287
258
288
259
289
private ExecutionGraphQlService graphQlService () {
290
+ return graphQlService ((configurer , setup ) -> {});
291
+ }
292
+
293
+ private ExecutionGraphQlService graphQlService (BiConsumer <AnnotatedControllerConfigurer , GraphQlSetup > consumer ) {
260
294
BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry ();
261
295
262
296
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
@@ -269,10 +303,10 @@ private ExecutionGraphQlService graphQlService() {
269
303
configurer .setApplicationContext (context );
270
304
configurer .afterPropertiesSet ();
271
305
272
- return GraphQlSetup .schemaResource (BookSource .schema )
273
- . runtimeWiring (configurer )
274
- . dataLoaders ( registry )
275
- .toGraphQlService ();
306
+ GraphQlSetup setup = GraphQlSetup .schemaResource (BookSource .schema ). runtimeWiring ( configurer );
307
+ consumer . accept (configurer , setup );
308
+
309
+ return setup . dataLoaders ( registry ) .toGraphQlService ();
276
310
}
277
311
278
312
@@ -292,7 +326,10 @@ public Book bookById(@Argument Long id) {
292
326
293
327
@ QueryMapping
294
328
public List <Book > booksByCriteria (@ Argument BookCriteria criteria ) {
295
- Assert .isTrue (!criteria .getAuthor ().equalsIgnoreCase ("Fitzgerald" ), "Bad input" );
329
+ switch (criteria .getAuthor ()) {
330
+ case "Fitzgerald" -> throw new IllegalArgumentException ("Bad input" );
331
+ case "Heller" -> throw new IllegalStateException ("Fetch failure" );
332
+ }
296
333
return BookSource .findBooksByAuthor (criteria .getAuthor ());
297
334
}
298
335
0 commit comments