@@ -9,7 +9,7 @@ import crypto from 'crypto'
9
9
10
10
import type { DataContext } from '..'
11
11
import getenv from 'getenv'
12
- import { print , DocumentNode , ExecutionResult , GraphQLResolveInfo , OperationTypeNode } from 'graphql'
12
+ import { print , DocumentNode , ExecutionResult , GraphQLResolveInfo , OperationTypeNode , visit , OperationDefinitionNode } from 'graphql'
13
13
import {
14
14
createClient ,
15
15
dedupExchange ,
@@ -50,6 +50,7 @@ export interface CloudExecuteQuery {
50
50
}
51
51
52
52
export interface CloudExecuteRemote extends CloudExecuteQuery {
53
+ shouldBatch ?: boolean
53
54
operationType ?: OperationTypeNode
54
55
requestPolicy ?: RequestPolicy
55
56
onUpdatedResult ?: ( data : any ) => any
@@ -81,14 +82,9 @@ export class CloudDataSource {
81
82
82
83
constructor ( private params : CloudDataSourceParams ) {
83
84
this . #cloudUrqlClient = this . reset ( )
84
- this . #batchExecutor = createBatchingExecutor ( ( { document, variables } ) => {
85
- debug ( `Executing remote dashboard request %s, %j` , print ( document ) , variables )
86
-
87
- return this . #cloudUrqlClient. query ( document , variables ?? { } , {
88
- ...this . #additionalHeaders,
89
- requestPolicy : 'network-only' ,
90
- } ) . toPromise ( )
91
- } )
85
+ this . #batchExecutor = createBatchingExecutor ( ( config ) => {
86
+ return this . #executeQuery( namedExecutionDocument ( config . document ) , config . variables )
87
+ } , { maxBatchSize : 20 } )
92
88
93
89
this . #batchExecutorBatcher = this . #makeBatchExecutionBatcher( )
94
90
}
@@ -213,7 +209,11 @@ export class CloudDataSource {
213
209
return loading
214
210
}
215
211
216
- loading = this . #batchExecutorBatcher. load ( config ) . then ( this . #formatWithErrors)
212
+ const query = config . shouldBatch
213
+ ? this . #batchExecutorBatcher. load ( config )
214
+ : this . #executeQuery( config . operationDoc , config . operationVariables )
215
+
216
+ loading = query . then ( this . #formatWithErrors)
217
217
. then ( ( op ) => {
218
218
this . #pendingPromises. delete ( stableKey )
219
219
@@ -235,6 +235,12 @@ export class CloudDataSource {
235
235
return loading
236
236
}
237
237
238
+ #executeQuery ( operationDoc : DocumentNode , operationVariables : object = { } ) {
239
+ debug ( `Executing remote dashboard request %s, %j` , print ( operationDoc ) , operationVariables )
240
+
241
+ return this . #cloudUrqlClient. query ( operationDoc , operationVariables , { requestPolicy : 'network-only' } ) . toPromise ( )
242
+ }
243
+
238
244
isResolving ( config : CloudExecuteQuery ) {
239
245
const stableKey = this . #hashRemoteRequest( config )
240
246
@@ -330,18 +336,6 @@ export class CloudDataSource {
330
336
*/
331
337
#makeBatchExecutionBatcher ( ) {
332
338
return new DataLoader < CloudExecuteRemote , any > ( async ( toBatch ) => {
333
- const first = toBatch [ 0 ]
334
-
335
- // If we only have a single entry, we can just hit the query directly,
336
- // without rewriting anything - this makes the queries simpler in most cases in the app
337
- if ( toBatch . length === 1 && first ) {
338
- return [ this . #cloudUrqlClient. query ( first . operation , first . operationVariables ?? { } , {
339
- ...this . #additionalHeaders,
340
- requestPolicy : 'network-only' ,
341
- } ) . toPromise ( ) ]
342
- }
343
-
344
- // Otherwise run this through batchExecutor:
345
339
return Promise . allSettled ( toBatch . map ( ( b ) => {
346
340
return this . #batchExecutor( {
347
341
operationType : 'query' ,
@@ -358,3 +352,37 @@ export class CloudDataSource {
358
352
return val instanceof Error ? val : new Error ( val )
359
353
}
360
354
}
355
+
356
+ /**
357
+ * Adds "batchExecutionQuery" to the query that we generate from the batch loader,
358
+ * useful to key off of in the tests.
359
+ */
360
+ function namedExecutionDocument ( document : DocumentNode ) {
361
+ let hasReplaced = false
362
+
363
+ return visit ( document , {
364
+ enter ( ) {
365
+ if ( hasReplaced ) {
366
+ return false
367
+ }
368
+
369
+ return
370
+ } ,
371
+ OperationDefinition ( op ) {
372
+ if ( op . name ) {
373
+ return op
374
+ }
375
+
376
+ hasReplaced = true
377
+ const namedOperationNode : OperationDefinitionNode = {
378
+ ...op ,
379
+ name : {
380
+ kind : 'Name' ,
381
+ value : 'batchTestRunnerExecutionQuery' ,
382
+ } ,
383
+ }
384
+
385
+ return namedOperationNode
386
+ } ,
387
+ } )
388
+ }
0 commit comments