@@ -1246,6 +1246,68 @@ test('bail if errorHandler does not return an error', async () => {
1246
1246
} ) ;
1247
1247
} ) ;
1248
1248
1249
+ test ( 'batch endpoint with propertyBatchKey and maxBatchSize' , async ( ) => {
1250
+ const config = {
1251
+ resources : {
1252
+ foo : {
1253
+ isBatchResource : true ,
1254
+ docsLink : 'example.com/docs/bar' ,
1255
+ batchKey : 'foo_ids' ,
1256
+ newKey : 'foo_id' ,
1257
+ propertyBatchKey : 'properties' , // This is required for maxBatchSize to work
1258
+ responseKey : 'id' ,
1259
+ maxBatchSize : 2 , // Set a maximum batch size of 2
1260
+ } ,
1261
+ } ,
1262
+ } ;
1263
+
1264
+ // Track each batch of IDs that the resource function receives
1265
+ const receivedBatches = [ ] ;
1266
+
1267
+ const resources = {
1268
+ foo : ( { foo_ids, properties } ) => {
1269
+ receivedBatches . push ( [ ...foo_ids ] ) ;
1270
+ return Promise . resolve (
1271
+ foo_ids . map ( id => ( {
1272
+ id,
1273
+ name : `name-${ id } ` ,
1274
+ rating : id + 1
1275
+ } ) )
1276
+ ) ;
1277
+ } ,
1278
+ } ;
1279
+
1280
+ await createDataLoaders ( config , async ( getLoaders ) => {
1281
+ const loaders = getLoaders ( resources ) ;
1282
+
1283
+ // Request 5 items at once, which should be split into 3 batches (2 + 2 + 1)
1284
+ const results = await Promise . all ( [
1285
+ loaders . foo . load ( { foo_id : 1 , properties : [ 'name' , 'rating' ] } ) ,
1286
+ loaders . foo . load ( { foo_id : 2 , properties : [ 'name' , 'rating' ] } ) ,
1287
+ loaders . foo . load ( { foo_id : 3 , properties : [ 'name' , 'rating' ] } ) ,
1288
+ loaders . foo . load ( { foo_id : 4 , properties : [ 'name' , 'rating' ] } ) ,
1289
+ loaders . foo . load ( { foo_id : 5 , properties : [ 'name' , 'rating' ] } ) ,
1290
+ ] ) ;
1291
+
1292
+ // Verify that all results were returned correctly
1293
+ expect ( results ) . toEqual ( [
1294
+ { id : 1 , name : 'name-1' , rating : 2 } ,
1295
+ { id : 2 , name : 'name-2' , rating : 3 } ,
1296
+ { id : 3 , name : 'name-3' , rating : 4 } ,
1297
+ { id : 4 , name : 'name-4' , rating : 5 } ,
1298
+ { id : 5 , name : 'name-5' , rating : 6 } ,
1299
+ ] ) ;
1300
+
1301
+ // Verify that the requests were batched correctly
1302
+ // We should have 3 batches with max 2 IDs.
1303
+ expect ( receivedBatches . map ( batch => batch . length ) ) . toEqual ( [ 2 , 2 , 1 ] ) ;
1304
+
1305
+ // Verify that all IDs were requested
1306
+ const allRequestedIds = receivedBatches . flat ( ) . sort ( ) ;
1307
+ expect ( allRequestedIds ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
1308
+ } ) ;
1309
+ } ) ;
1310
+
1249
1311
test ( 'batch endpoint (multiple requests) with propertyBatchKey' , async ( ) => {
1250
1312
const config = {
1251
1313
resources : {
0 commit comments