Skip to content

Commit e33c0d9

Browse files
authored
Add maxRequestsPerBatch to batchMiddleware. (#1)
* Update package.json + CI/CD for @tensor-hq org. * Add `maxRequestsPerBatch` to `batchMiddleware`.
1 parent 7f366ea commit e33c0d9

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

.github/workflows/nodejs.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ jobs:
3232
run: yarn flow
3333
- name: Publish Test Report
3434
if: ${{ always() && matrix.node-version == '14' }}
35-
uses: mikepenz/action-junit-report@v2
35+
uses: mikepenz/action-junit-report@127c778ac944abc0f48a5103964304bab7eb208b
3636
with:
3737
check_name: JUnit Annotations for Node ${{ matrix.node-version }}
38-
report_paths: "**/coverage/junit/**/*.xml"
38+
report_paths: '**/coverage/junit/**/*.xml'
3939

4040
publish:
4141
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/alpha' || github.ref == 'refs/heads/beta'
4242
needs: [tests]
4343
runs-on: ubuntu-latest
44+
permissions:
45+
contents: write
46+
packages: write
4447
steps:
4548
- uses: actions/checkout@v3
4649
- name: Use Node.js 14
@@ -55,4 +58,5 @@ jobs:
5558
run: yarn semantic-release
5659
env:
5760
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
58-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
61+
NPM_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"name": "react-relay-network-modern",
2+
"name": "@tensor-hq/react-relay-network-modern",
33
"version": "0.0.0-semantically-released",
4+
"private": false,
45
"description": "Network Layer for React Relay and Express (Batch Queries, AuthToken, Logging, Retry)",
56
"engines": {
67
"node": ">=14.21.3"
@@ -11,9 +12,9 @@
1112
],
1213
"main": "lib/index.js",
1314
"types": "lib/index.d.ts",
14-
"repository": {
15-
"type": "git",
16-
"url": "https://github.com/relay-tools/react-relay-network-modern.git"
15+
"repository": "[email protected]:tensor-hq/react-relay-network-modern.git",
16+
"publishConfig": {
17+
"registry": "https://npm.pkg.github.com"
1718
},
1819
"keywords": [
1920
"relay",

src/middlewares/__tests__/batch-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,4 +554,56 @@ describe('middlewares/batch', () => {
554554
);
555555
});
556556
});
557+
558+
describe('option `maxRequestsPerBatch`', () => {
559+
beforeEach(() => {
560+
fetchMock.restore();
561+
});
562+
563+
it('should split batch requests based on max requests limit', async () => {
564+
// Set up mocks for single and batch requests
565+
fetchMock.mock({
566+
matcher: '/graphql',
567+
response: {
568+
status: 200,
569+
body: { data: {} },
570+
},
571+
method: 'POST',
572+
});
573+
574+
fetchMock.mock({
575+
matcher: '/graphql/batch',
576+
response: {
577+
status: 200,
578+
body: [{ data: {} }, { data: {} }],
579+
},
580+
method: 'POST',
581+
});
582+
583+
// Create a network layer with maxRequestsPerBatch set to 2
584+
const rnl = new RelayNetworkLayer([batchMiddleware({ maxRequestsPerBatch: 2 })]);
585+
586+
// Create 5 mock requests
587+
const req1 = mockReq(1);
588+
const req2 = mockReq(2);
589+
const req3 = mockReq(3);
590+
const req4 = mockReq(4);
591+
const req5 = mockReq(5);
592+
593+
// Execute all requests simultaneously
594+
await Promise.all([
595+
req1.execute(rnl),
596+
req2.execute(rnl),
597+
req3.execute(rnl),
598+
req4.execute(rnl),
599+
req5.execute(rnl),
600+
]);
601+
602+
// Check if the requests were properly split into batches
603+
const batchReqs = fetchMock.calls('/graphql/batch');
604+
const singleReqs = fetchMock.calls('/graphql');
605+
expect(batchReqs).toHaveLength(2);
606+
expect(singleReqs).toHaveLength(1);
607+
});
608+
});
557609
});

src/middlewares/batch.js

Lines changed: 10 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,13 @@ function passThroughBatch(req: RelayRequest, next, opts) {
111114
singleton.batcher = prepareNewBatcher(next, opts);
112115
}
113116

117+
if (
118+
opts.maxRequestsPerBatch &&
119+
singleton.batcher.requestList.length + 1 > opts.maxRequestsPerBatch
120+
) {
121+
singleton.batcher = prepareNewBatcher(next, opts);
122+
}
123+
114124
if (singleton.batcher.bodySize + bodyLength + 1 > opts.maxBatchSize) {
115125
singleton.batcher = prepareNewBatcher(next, opts);
116126
}

0 commit comments

Comments
 (0)