Skip to content

Commit 51e5781

Browse files
committed
Add maxRequestsPerBatch to batchMiddleware.
1 parent c70f0a1 commit 51e5781

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/middlewares/batch.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type BatchMiddlewareOpts = {|
2020
| ((requestList: RequestWrapper[]) => string | Promise<string>),
2121
batchTimeout?: number,
2222
maxBatchSize?: number,
23+
maxRequestsPerBatch?: number,
2324
allowMutations?: boolean,
2425
method?: 'POST' | 'GET',
2526
headers?: Headers | Promise<Headers> | ((req: RelayRequestBatch) => Headers | Promise<Headers>),
@@ -57,6 +58,7 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
5758
const allowMutations = opts.allowMutations || false;
5859
const batchUrl = opts.batchUrl || '/graphql/batch';
5960
const maxBatchSize = opts.maxBatchSize || DEFAULT_BATCH_SIZE;
61+
const maxRequestsPerBatch = opts.maxRequestsPerBatch || 0; // 0 is the same as no limit
6062
const singleton = {};
6163

6264
const fetchOpts = {};
@@ -111,6 +113,10 @@ function passThroughBatch(req: RelayRequest, next, opts) {
111113
singleton.batcher = prepareNewBatcher(next, opts);
112114
}
113115

116+
if(singleton.batcher && opts.maxRequestsPerBatch && singleton.batcher.requestList.length + 1 > opts.maxRequestsPerBatch) {
117+
singleton.batcher = prepareNewBatcher(next, opts);
118+
}
119+
114120
if (singleton.batcher.bodySize + bodyLength + 1 > opts.maxBatchSize) {
115121
singleton.batcher = prepareNewBatcher(next, opts);
116122
}

0 commit comments

Comments
 (0)