Skip to content

Commit 02b74f9

Browse files
committed
Add maxRequestsPerBatch to batchMiddleware.
1 parent c70f0a1 commit 02b74f9

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/middlewares/batch.js

Lines changed: 7 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 = {};
@@ -94,6 +96,7 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
9496
batchUrl,
9597
singleton,
9698
maxBatchSize,
99+
maxRequestsPerBatch,
97100
fetchOpts,
98101
});
99102
};
@@ -111,6 +114,10 @@ function passThroughBatch(req: RelayRequest, next, opts) {
111114
singleton.batcher = prepareNewBatcher(next, opts);
112115
}
113116

117+
if(singleton.batcher && opts.maxRequestsPerBatch && singleton.batcher.requestList.length + 1 > opts.maxRequestsPerBatch) {
118+
singleton.batcher = prepareNewBatcher(next, opts);
119+
}
120+
114121
if (singleton.batcher.bodySize + bodyLength + 1 > opts.maxBatchSize) {
115122
singleton.batcher = prepareNewBatcher(next, opts);
116123
}

0 commit comments

Comments
 (0)