@@ -10,6 +10,16 @@ import {
1010} from 'relay-runtime' ;
1111import { Sink } from 'relay-runtime/lib/network/RelayObservable' ;
1212
13+ export interface BatchConfig {
14+ enabled : boolean ;
15+ timeoutMs ?: number ;
16+ shouldRequestBeBatched ?: (
17+ operation : RequestParameters ,
18+ variables : Variables ,
19+ cacheConfig ?: CacheConfig ,
20+ uploadables ?: UploadableMap ,
21+ ) => boolean ;
22+ }
1323export interface FetchOptions {
1424 url ?: string ;
1525 init ?: RequestInit | ( ( ) => RequestInit ) ;
@@ -22,12 +32,7 @@ export interface FetchOptions {
2232 headerName ?: string ;
2333 scheme ?: string ;
2434 } ;
25- batch ?:
26- | boolean
27- | {
28- enabled : boolean ;
29- timeoutMs ?: number ;
30- } ;
35+ batch ?: boolean | BatchConfig ;
3136}
3237
3338export interface Data {
@@ -65,9 +70,9 @@ let batcher: null | {
6570 sinks : Sink < GraphQLResponse > [ ] ;
6671} = null ;
6772
68- function normalizeBatch ( batch : FetchOptions [ 'batch' ] = true ) {
69- const p = typeof batch === 'boolean' ? { enabled : batch } : { } ;
70- return { timeoutMs : 0 , ...p } ;
73+ function normalizeBatch ( batch : FetchOptions [ 'batch' ] = true ) : BatchConfig {
74+ const p = typeof batch === 'boolean' ? { enabled : batch } : batch ;
75+ return { timeoutMs : 0 , shouldRequestBeBatched : ( ) => true , ...p } ;
7176}
7277
7378function normalizeAuth ( auth : FetchOptions [ 'authorization' ] = '' ) {
@@ -169,7 +174,7 @@ function createFetch({
169174 const resp = processJson ( batchResp [ idx ] ) ;
170175 sink . next ! ( resp ) ;
171176 sink . complete ! ( ) ;
172- } catch ( err ) {
177+ } catch ( err : any ) {
173178 sink . error ! ( err ) ;
174179 }
175180 } ) ;
@@ -190,7 +195,7 @@ function createFetch({
190195 function fetchFn (
191196 operation : RequestParameters ,
192197 variables : Variables ,
193- _cacheConfig ?: CacheConfig ,
198+ cacheConfig ?: CacheConfig ,
194199 uploadables ?: UploadableMap ,
195200 ) {
196201 // We use observables directly here instead of the promise value
@@ -210,7 +215,13 @@ function createFetch({
210215 if (
211216 batching . enabled &&
212217 ! uploadables &&
213- operation . operationKind !== 'mutation'
218+ operation . operationKind !== 'mutation' &&
219+ batching . shouldRequestBeBatched ! (
220+ operation ,
221+ variables ,
222+ cacheConfig ,
223+ uploadables ,
224+ )
214225 ) {
215226 addToBatch ( body as string , sink ) ;
216227
0 commit comments