-
-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: When `batch` is set to true for a given subschemaConfig, batches all delegated root fields into a combined request passed to the executor. Moreover, batches all requests to a given subschema into the minimum number of requests, collecting queries and mutations separately, preserving operation order. Distributes properly pathed errors to the originating requests. Adapted from Gatsby query batcher by @vladar. Caveats: * Uses a Dataloader under the hood, which is created anew upon each request -- relies on a unique context argument per request to make this happen! * Passed `info` argument from first executor call to the batched executor call, making info argument unreliable. TODO: - Add testing! Related: gatsbyjs/gatsby#22347 (comment) #1710 (comment) #1959 (comment) #1954
- Loading branch information
Showing
9 changed files
with
539 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { getOperationAST } from 'graphql'; | ||
|
||
import isPromise from 'is-promise'; | ||
|
||
import DataLoader from 'dataloader'; | ||
|
||
import { ExecutionResult } from '@graphql-tools/utils'; | ||
|
||
import { SubschemaConfig, ExecutionParams } from './types'; | ||
import { memoize2of3 } from './memoize'; | ||
import { mergeExecutionParams } from './mergeExecutionParams'; | ||
import { splitResult } from './splitResult'; | ||
|
||
export const getBatchingExecutor = memoize2of3(function ( | ||
_context: Record<string, any>, | ||
subschemaConfig: SubschemaConfig, | ||
executor: ({ document, context, variables, info }: ExecutionParams) => ExecutionResult | Promise<ExecutionResult> | ||
) { | ||
const loader = new DataLoader( | ||
createLoadFn(executor ?? subschemaConfig.executor), | ||
subschemaConfig.batchingOptions?.dataLoaderOptions | ||
); | ||
return (executionParams: ExecutionParams) => loader.load(executionParams); | ||
}); | ||
|
||
function createLoadFn( | ||
executor: ({ document, context, variables, info }: ExecutionParams) => ExecutionResult | Promise<ExecutionResult> | ||
) { | ||
return async (execs: Array<ExecutionParams>): Promise<Array<ExecutionResult>> => { | ||
const execBatches: Array<Array<ExecutionParams>> = []; | ||
let index = 0; | ||
const exec = execs[index]; | ||
let currentBatch: Array<ExecutionParams> = [exec]; | ||
execBatches.push(currentBatch); | ||
const operationType = getOperationAST(exec.document, undefined).operation; | ||
while (++index < execs.length) { | ||
const currentOperationType = getOperationAST(execs[index].document, undefined).operation; | ||
if (operationType === currentOperationType) { | ||
currentBatch.push(execs[index]); | ||
} else { | ||
currentBatch = [execs[index]]; | ||
execBatches.push(currentBatch); | ||
} | ||
} | ||
|
||
let containsPromises = false; | ||
const executionResults: Array<ExecutionResult | Promise<ExecutionResult>> = []; | ||
execBatches.forEach(execBatch => { | ||
const mergedExecutionParams = mergeExecutionParams(execBatch); | ||
const executionResult = executor(mergedExecutionParams); | ||
|
||
if (isPromise(executionResult)) { | ||
containsPromises = true; | ||
} | ||
executionResults.push(executionResult); | ||
}); | ||
|
||
if (containsPromises) { | ||
return Promise.all(executionResults).then(resultBatches => { | ||
let results: Array<ExecutionResult> = []; | ||
resultBatches.forEach((resultBatch, index) => { | ||
results = results.concat(splitResult(resultBatch, execBatches[index].length)); | ||
}); | ||
return results; | ||
}); | ||
} | ||
|
||
let results: Array<ExecutionResult> = []; | ||
(executionResults as Array<ExecutionResult>).forEach((resultBatch, index) => { | ||
results = results.concat(splitResult(resultBatch, execBatches[index].length)); | ||
}); | ||
return results; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.