Skip to content

Commit

Permalink
feat: add skipBatch to cacheConfig options for batchMiddleware (#100)
Browse files Browse the repository at this point in the history
When `cacheConfig.skipBatch` is truthy, we skip batching for that
request even when the batchMiddleware is present. This allows users to
better control what requests are batched together.
  • Loading branch information
ekosz committed May 17, 2020
1 parent 509f97d commit 0a678f6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/__mocks__/mockReq.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import type RelayNetworkLayer from '../RelayNetworkLayer';
import type RelayResponse from '../RelayResponse';
import type { CacheConfig } from '../definition';

type ReqData = {
query?: string,
variables?: Object,
cacheConfig?: CacheConfig,
files?: any,
};

Expand All @@ -17,6 +19,7 @@ class MockReq {
reqData: ReqData;
error: Error;
payload: Object;
cacheConfig: Object;

constructor(reqid?: ReqId, reqData?: ReqData = {}) {
this.reqid = reqid || Math.random().toString();
Expand Down Expand Up @@ -60,7 +63,7 @@ class MockReq {
operationKind,
}: any);
const variables = this.getVariables() || {};
const cacheConfig = {};
const cacheConfig = this.reqData.cacheConfig || {};
const uploadables = this.getFiles();

const res = (rnl.fetchFn(operation, variables, cacheConfig, uploadables): any);
Expand Down
1 change: 1 addition & 0 deletions src/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export type CacheConfig = {
force?: ?boolean,
poll?: ?number,
rerunParamExperimental?: ?any,
skipBatch?: ?boolean,
};
export type Disposable = { dispose(): void };
export type Uploadable = File | Blob;
Expand Down
33 changes: 33 additions & 0 deletions src/middlewares/__tests__/batch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,39 @@ describe('middlewares/batch', () => {
expect(fetchMock.lastOptions()).toMatchSnapshot();
});

it('should not batch requests cacheConfig `skipBatch=true`', async () => {
fetchMock.mock({
matcher: '/graphql',
response: {
status: 200,
body: { data: { ok: 1 } },
},
method: 'POST',
});

fetchMock.mock({
matcher: '/graphql/batch',
response: {
status: 200,
body: [{ data: { ok: 2 } }, { data: { ok: 3 } }],
},
method: 'POST',
});

const rnl = new RelayNetworkLayer([batchMiddleware()]);
const req1 = mockReq(1, { cacheConfig: { skipBatch: true } });
const req2 = mockReq(2);
const req3 = mockReq(3);
const [res1, res2, res3] = await Promise.all([
req1.execute(rnl),
req2.execute(rnl),
req3.execute(rnl),
]);
expect(res1.data).toEqual({ ok: 1 });
expect(res2.data).toEqual({ ok: 2 });
expect(res3.data).toEqual({ ok: 3 });
});

describe('option `batchTimeout`', () => {
beforeEach(() => {
fetchMock.restore();
Expand Down
5 changes: 5 additions & 0 deletions src/middlewares/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
return next(req);
}

// skip batching if request explicitly opts out
if (req.cacheConfig.skipBatch) {
return next(req);
}

return passThroughBatch(req, next, {
batchTimeout,
batchUrl,
Expand Down

0 comments on commit 0a678f6

Please sign in to comment.