24
24
import graphql .GraphqlErrorBuilder ;
25
25
import graphql .schema .DataFetchingEnvironment ;
26
26
import org .junit .jupiter .api .Test ;
27
+ import org .junit .jupiter .params .ParameterizedTest ;
28
+ import org .junit .jupiter .params .provider .ValueSource ;
27
29
import reactor .core .publisher .Flux ;
28
30
import reactor .core .publisher .Mono ;
29
31
@@ -113,8 +115,9 @@ void fetchEntitiesWithExceptions() {
113
115
assertAuthor (6 , "George" , "Orwell" , helper );
114
116
}
115
117
116
- @ Test
117
- void batching () {
118
+ @ ValueSource (classes = {BookListController .class , BookFluxController .class })
119
+ @ ParameterizedTest
120
+ void batching (Class <?> controllerClass ) {
118
121
Map <String , Object > variables =
119
122
Map .of ("representations" , List .of (
120
123
Map .of ("__typename" , "Book" , "id" , "1" ),
@@ -123,7 +126,7 @@ void batching() {
123
126
Map .of ("__typename" , "Book" , "id" , "42" ),
124
127
Map .of ("__typename" , "Book" , "id" , "53" )));
125
128
126
- ResponseHelper helper = executeWith (BookBatchController . class , variables );
129
+ ResponseHelper helper = executeWith (controllerClass , variables );
127
130
128
131
assertAuthor (0 , "George" , "Orwell" , helper );
129
132
assertAuthor (1 , "Virginia" , "Woolf" , helper );
@@ -132,30 +135,32 @@ void batching() {
132
135
assertAuthor (4 , "Vince" , "Gilligan" , helper );
133
136
}
134
137
135
- @ Test
136
- void batchingWithError () {
138
+ @ ValueSource (classes = {BookListController .class , BookFluxController .class })
139
+ @ ParameterizedTest
140
+ void batchingWithError (Class <?> controllerClass ) {
137
141
Map <String , Object > variables =
138
142
Map .of ("representations" , List .of (
139
143
Map .of ("__typename" , "Book" , "id" , "-97" ),
140
144
Map .of ("__typename" , "Book" , "id" , "4" ),
141
145
Map .of ("__typename" , "Book" , "id" , "5" )));
142
146
143
- ResponseHelper helper = executeWith (BookBatchController . class , variables );
147
+ ResponseHelper helper = executeWith (controllerClass , variables );
144
148
145
149
assertError (helper , 0 , "BAD_REQUEST" , "handled" );
146
150
assertError (helper , 1 , "BAD_REQUEST" , "handled" );
147
151
assertError (helper , 2 , "BAD_REQUEST" , "handled" );
148
152
}
149
153
150
- @ Test
151
- void batchingWithoutResult () {
154
+ @ ValueSource (classes = {BookListController .class , BookFluxController .class })
155
+ @ ParameterizedTest
156
+ void batchingWithoutResult (Class <?> controllerClass ) {
152
157
Map <String , Object > variables =
153
158
Map .of ("representations" , List .of (
154
159
Map .of ("__typename" , "Book" , "id" , "-99" ),
155
160
Map .of ("__typename" , "Book" , "id" , "4" ),
156
161
Map .of ("__typename" , "Book" , "id" , "5" )));
157
162
158
- ResponseHelper helper = executeWith (BookBatchController . class , variables );
163
+ ResponseHelper helper = executeWith (controllerClass , variables );
159
164
160
165
assertError (helper , 0 , "INTERNAL_ERROR" , "Entity fetcher returned null or completed empty" );
161
166
assertError (helper , 1 , "INTERNAL_ERROR" , "Entity fetcher returned null or completed empty" );
@@ -242,10 +247,53 @@ public GraphQLError handle(IllegalArgumentException ex, DataFetchingEnvironment
242
247
243
248
@ SuppressWarnings ("unused" )
244
249
@ Controller
245
- private static class BookBatchController {
250
+ private static class BookListController {
251
+
252
+ private final BookBatchService batchService = new BookBatchService ();
246
253
247
254
@ EntityMapping
248
255
public List <Book > book (@ Argument List <Integer > idList , List <Map <String , Object >> representations ) {
256
+ return this .batchService .book (idList , representations );
257
+ }
258
+
259
+ @ BatchMapping
260
+ public List <Author > author (List <Book > books ) {
261
+ return this .batchService .author (books );
262
+ }
263
+
264
+ @ GraphQlExceptionHandler
265
+ public GraphQLError handle (IllegalArgumentException ex , DataFetchingEnvironment env ) {
266
+ return this .batchService .handle (ex , env );
267
+ }
268
+ }
269
+
270
+
271
+ @ SuppressWarnings ("unused" )
272
+ @ Controller
273
+ private static class BookFluxController {
274
+
275
+ private final BookBatchService batchService = new BookBatchService ();
276
+
277
+ @ EntityMapping
278
+ public Flux <Book > book (@ Argument List <Integer > idList , List <Map <String , Object >> representations ) {
279
+ return Flux .fromIterable (this .batchService .book (idList , representations ));
280
+ }
281
+
282
+ @ BatchMapping
283
+ public Flux <Author > author (List <Book > books ) {
284
+ return Flux .fromIterable (this .batchService .author (books ));
285
+ }
286
+
287
+ @ GraphQlExceptionHandler
288
+ public GraphQLError handle (IllegalArgumentException ex , DataFetchingEnvironment env ) {
289
+ return this .batchService .handle (ex , env );
290
+ }
291
+ }
292
+
293
+
294
+ private static class BookBatchService {
295
+
296
+ public List <Book > book (List <Integer > idList , List <Map <String , Object >> representations ) {
249
297
250
298
if (idList .get (0 ) == -97 ) {
251
299
throw new IllegalArgumentException ("handled" );
@@ -265,12 +313,10 @@ public List<Book> book(@Argument List<Integer> idList, List<Map<String, Object>>
265
313
return idList .stream ().map (id -> new Book ((long ) id , null , (Long ) null )).toList ();
266
314
}
267
315
268
- @ BatchMapping
269
- public Flux <Author > author (List <Book > books ) {
270
- return Flux .fromIterable (books ).map (book -> BookSource .getBook (book .getId ()).getAuthor ());
316
+ public List <Author > author (List <Book > books ) {
317
+ return books .stream ().map (book -> BookSource .getBook (book .getId ()).getAuthor ()).toList ();
271
318
}
272
319
273
- @ GraphQlExceptionHandler
274
320
public GraphQLError handle (IllegalArgumentException ex , DataFetchingEnvironment env ) {
275
321
return GraphqlErrorBuilder .newError (env )
276
322
.errorType (ErrorType .BAD_REQUEST )
0 commit comments