File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed
Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 : { } } ] ,
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 . name ) ,
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' ,
Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
5959 const batchUrl = opts . batchUrl || '/graphql/batch' ;
6060 const maxBatchSize = opts . maxBatchSize || DEFAULT_BATCH_SIZE ;
6161 const maxRequestsPerBatch = opts . maxRequestsPerBatch || 0 ; // 0 is the same as no limit
62+ const allowOperation = opts . allowOperation || true ;
6263 const singleton = { } ;
6364
6465 const fetchOpts = { } ;
@@ -81,6 +82,10 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
8182 ) ;
8283 }
8384
85+ if ( isFunction ( opts . allowOperation ) && ! opts . allowOperation ( req . operation ) ) {
86+ return next ( req ) ;
87+ }
88+
8489 // req with FormData can not be batched
8590 if ( req . isFormData ( ) ) {
8691 return next ( req ) ;
@@ -97,6 +102,7 @@ export default function batchMiddleware(options?: BatchMiddlewareOpts): Middlewa
97102 singleton,
98103 maxBatchSize,
99104 maxRequestsPerBatch,
105+ allowOperation,
100106 fetchOpts,
101107 } ) ;
102108 } ;
You can’t perform that action at this time.
0 commit comments