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