Skip to content

Commit 931cebc

Browse files
committed
Add allowOperation to batchMiddleware. (#2)
* Add `allowOperation` to `batchMiddleware`. * Fix test. * Test console. * Fix test once again + missing attribute.
1 parent 7f366ea commit 931cebc

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
@@ -147,6 +147,7 @@ export type BatchMiddlewareOpts = {
147147
batchTimeout?: number;
148148
maxBatchSize?: number;
149149
allowMutations?: boolean;
150+
allowOperation?: (operation: ConcreteBatch) => boolean;
150151
method?: 'POST' | 'GET';
151152
headers?: Headers | Promise<Headers> | ((req: RelayRequestBatch) => Headers | Promise<Headers>);
152153
// 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)
@@ -21,6 +21,7 @@ export type BatchMiddlewareOpts = {|
2121
batchTimeout?: number,
2222
maxBatchSize?: number,
2323
allowMutations?: boolean,
24+
allowOperation?: (operation: ConcreteBatch) => boolean,
2425
method?: 'POST' | 'GET',
2526
headers?: Headers | Promise<Headers> | ((req: RelayRequestBatch) => Headers | Promise<Headers>),
2627
// Avaliable request modes in fetch options. For details see https://fetch.spec.whatwg.org/#requests
@@ -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 allowOperation = opts.allowOperation || true;
6062
const singleton = {};
6163

6264
const fetchOpts = {};
@@ -79,6 +81,10 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
7981
);
8082
}
8183

84+
if (isFunction(opts.allowOperation) && !opts.allowOperation(req.operation)) {
85+
return next(req);
86+
}
87+
8288
// req with FormData can not be batched
8389
if (req.isFormData()) {
8490
return next(req);
@@ -94,6 +100,7 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
94100
batchUrl,
95101
singleton,
96102
maxBatchSize,
103+
allowOperation,
97104
fetchOpts,
98105
});
99106
};

0 commit comments

Comments
 (0)