@@ -4,6 +4,7 @@ pub mod graphiql;
4
4
pub mod playground;
5
5
6
6
use serde:: {
7
+ de,
7
8
ser:: { self , SerializeMap } ,
8
9
Deserialize , Serialize ,
9
10
} ;
@@ -216,7 +217,7 @@ where
216
217
}
217
218
}
218
219
219
- /// Simple wrapper around GraphQLRequest to allow the handling of Batch requests
220
+ /// Simple wrapper around GraphQLRequest to allow the handling of Batch requests.
220
221
#[ derive( Debug , Deserialize , PartialEq ) ]
221
222
#[ serde( untagged) ]
222
223
#[ serde( bound = "InputValue<S>: Deserialize<'de>" ) ]
@@ -226,10 +227,29 @@ where
226
227
{
227
228
/// A single operation request.
228
229
Single ( GraphQLRequest < S > ) ,
230
+
229
231
/// A batch operation request.
232
+ ///
233
+ /// Empty batch is considered as invalid value, so cannot be deserialized.
234
+ #[ serde( deserialize_with = "deserialize_non_empty_vec" ) ]
230
235
Batch ( Vec < GraphQLRequest < S > > ) ,
231
236
}
232
237
238
+ fn deserialize_non_empty_vec < ' de , D , T > ( deserializer : D ) -> Result < Vec < T > , D :: Error >
239
+ where
240
+ D : de:: Deserializer < ' de > ,
241
+ T : Deserialize < ' de > ,
242
+ {
243
+ use de:: Error as _;
244
+
245
+ let v = Vec :: < T > :: deserialize ( deserializer) ?;
246
+ if v. is_empty ( ) {
247
+ Err ( D :: Error :: invalid_length ( 0 , & "a positive integer" ) )
248
+ } else {
249
+ Ok ( v)
250
+ }
251
+ }
252
+
233
253
impl < S > GraphQLBatchRequest < S >
234
254
where
235
255
S : ScalarValue ,
@@ -373,6 +393,9 @@ pub mod tests {
373
393
println ! ( " - test_batched_post" ) ;
374
394
test_batched_post ( integration) ;
375
395
396
+ println ! ( " - test_empty_batched_post" ) ;
397
+ test_empty_batched_post ( integration) ;
398
+
376
399
println ! ( " - test_invalid_json" ) ;
377
400
test_invalid_json ( integration) ;
378
401
@@ -499,6 +522,11 @@ pub mod tests {
499
522
) ;
500
523
}
501
524
525
+ fn test_empty_batched_post < T : HTTPIntegration > ( integration : & T ) {
526
+ let response = integration. post ( "/" , "[]" ) ;
527
+ assert_eq ! ( response. status_code, 400 ) ;
528
+ }
529
+
502
530
fn test_invalid_json < T : HTTPIntegration > ( integration : & T ) {
503
531
let response = integration. get ( "/?query=blah" ) ;
504
532
assert_eq ! ( response. status_code, 400 ) ;
0 commit comments