@@ -5,7 +5,7 @@ import { isFunction } from '../utils';
55import RelayRequestBatch from '../RelayRequestBatch' ;
66import RelayRequest from '../RelayRequest' ;
77import type RelayResponse from '../RelayResponse' ;
8- import type { Middleware , FetchOpts } from '../definition' ;
8+ import type { Middleware , FetchOpts , ConcreteBatch } from '../definition' ;
99import 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