Skip to content

Commit 0916ebf

Browse files
authored
Add allowOperation to batchMiddleware. (#2)
* Add `allowOperation` to `batchMiddleware`. * Fix test. * Test console. * Fix test once again + missing attribute.
1 parent 1cb287f commit 0916ebf

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export type BatchMiddlewareOpts = {
148148
maxBatchSize?: number;
149149
maxRequestsPerBatch?: number;
150150
allowMutations?: boolean;
151+
allowOperation?: (operation: ConcreteBatch) => boolean;
151152
method?: 'POST' | 'GET';
152153
headers?: Headers | Promise<Headers> | ((req: RelayRequestBatch) => Headers | Promise<Headers>);
153154
// Available request modes in fetch options. For details see https://fetch.spec.whatwg.org/#requests

src/middlewares/__tests__/batch-test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,60 @@ describe('middlewares/batch', () => {
438438
});
439439
});
440440

441+
describe('option `allowOperation`', () => {
442+
beforeEach(() => {
443+
fetchMock.restore();
444+
});
445+
446+
it('should not batch requests that return false', async () => {
447+
fetchMock.mock({
448+
matcher: '/graphql',
449+
response: {
450+
status: 200,
451+
body: { data: {} },
452+
},
453+
method: 'POST',
454+
});
455+
fetchMock.mock({
456+
matcher: '/graphql/batch',
457+
response: {
458+
status: 200,
459+
body: [{ data: {} }, { data: {} }, { data: {} }],
460+
},
461+
method: 'POST',
462+
});
463+
464+
const req1 = mockReq(1, {
465+
query: 'abc',
466+
});
467+
const req2 = mockReq(2);
468+
const req3 = mockReq(3);
469+
const req4 = mockReq(4, {
470+
query: 'def',
471+
});
472+
const req5 = mockReq(4, {
473+
query: 'no',
474+
});
475+
476+
const rnl = new RelayNetworkLayer([
477+
batchMiddleware({
478+
allowOperation: (op) => !['abc', 'def'].includes(op.text),
479+
}),
480+
]);
481+
await Promise.all([
482+
req1.execute(rnl),
483+
req2.execute(rnl),
484+
req3.execute(rnl),
485+
req4.execute(rnl),
486+
req5.execute(rnl),
487+
]);
488+
const batchReqs = fetchMock.calls('/graphql/batch');
489+
const singleReqs = fetchMock.calls('/graphql');
490+
expect(batchReqs).toHaveLength(1);
491+
expect(singleReqs).toHaveLength(2);
492+
});
493+
});
494+
441495
it('should pass fetch options', async () => {
442496
fetchMock.mock({
443497
matcher: '/graphql/batch',

src/middlewares/batch.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { isFunction } from '../utils';
55
import RelayRequestBatch from '../RelayRequestBatch';
66
import RelayRequest from '../RelayRequest';
77
import type RelayResponse from '../RelayResponse';
8-
import type { Middleware, FetchOpts } from '../definition';
8+
import type { Middleware, FetchOpts, ConcreteBatch } from '../definition';
99
import RRNLError from '../RRNLError';
1010

1111
// Max out at roughly 100kb (express-graphql imposed max)
@@ -22,6 +22,7 @@ export type BatchMiddlewareOpts = {|
2222
maxBatchSize?: number,
2323
maxRequestsPerBatch?: number,
2424
allowMutations?: boolean,
25+
allowOperation?: (operation: ConcreteBatch) => boolean,
2526
method?: 'POST' | 'GET',
2627
headers?: Headers | Promise<Headers> | ((req: RelayRequestBatch) => Headers | Promise<Headers>),
2728
// Avaliable request modes in fetch options. For details see https://fetch.spec.whatwg.org/#requests
@@ -59,6 +60,7 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
5960
const batchUrl = opts.batchUrl || '/graphql/batch';
6061
const maxBatchSize = opts.maxBatchSize || DEFAULT_BATCH_SIZE;
6162
const maxRequestsPerBatch = opts.maxRequestsPerBatch || 0; // 0 is the same as no limit
63+
const allowOperation = opts.allowOperation || true;
6264
const singleton = {};
6365

6466
const fetchOpts = {};
@@ -81,6 +83,10 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
8183
);
8284
}
8385

86+
if (isFunction(opts.allowOperation) && !opts.allowOperation(req.operation)) {
87+
return next(req);
88+
}
89+
8490
// req with FormData can not be batched
8591
if (req.isFormData()) {
8692
return next(req);
@@ -97,6 +103,7 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
97103
singleton,
98104
maxBatchSize,
99105
maxRequestsPerBatch,
106+
allowOperation,
100107
fetchOpts,
101108
});
102109
};

0 commit comments

Comments
 (0)