From 0a678f69699adc62a36b04ec333dcd5852e0e5e7 Mon Sep 17 00:00:00 2001 From: Eric Koslow Date: Sun, 17 May 2020 12:08:03 -0400 Subject: [PATCH] feat: add `skipBatch` to cacheConfig options for batchMiddleware (#100) 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. --- src/__mocks__/mockReq.js | 5 +++- src/definition.js | 1 + src/middlewares/__tests__/batch-test.js | 33 +++++++++++++++++++++++++ src/middlewares/batch.js | 5 ++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/__mocks__/mockReq.js b/src/__mocks__/mockReq.js index 9c71442..22ab611 100644 --- a/src/__mocks__/mockReq.js +++ b/src/__mocks__/mockReq.js @@ -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, }; @@ -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(); @@ -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); diff --git a/src/definition.js b/src/definition.js index 666a314..066923d 100644 --- a/src/definition.js +++ b/src/definition.js @@ -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; diff --git a/src/middlewares/__tests__/batch-test.js b/src/middlewares/__tests__/batch-test.js index e530fb0..2872e85 100644 --- a/src/middlewares/__tests__/batch-test.js +++ b/src/middlewares/__tests__/batch-test.js @@ -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(); diff --git a/src/middlewares/batch.js b/src/middlewares/batch.js index a117666..1367ef5 100644 --- a/src/middlewares/batch.js +++ b/src/middlewares/batch.js @@ -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,