@graphql-tools/batch-execute
Advanced tools
Comparing version 9.0.0 to 9.0.1-rc-20230809113950-f4100f90
@@ -6,12 +6,22 @@ "use strict"; | ||
const dataloader_1 = tslib_1.__importDefault(require("dataloader")); | ||
const value_or_promise_1 = require("value-or-promise"); | ||
const utils_1 = require("@graphql-tools/utils"); | ||
const mergeRequests_js_1 = require("./mergeRequests.js"); | ||
const splitResult_js_1 = require("./splitResult.js"); | ||
const value_or_promise_1 = require("value-or-promise"); | ||
function createBatchingExecutor(executor, dataLoaderOptions, extensionsReducer = defaultExtensionsReducer) { | ||
const loadFn = createLoadFn(executor, extensionsReducer); | ||
const loader = new dataloader_1.default(loadFn, dataLoaderOptions); | ||
const queryLoader = new dataloader_1.default(loadFn, dataLoaderOptions); | ||
const mutationLoader = new dataloader_1.default(loadFn, dataLoaderOptions); | ||
return function batchingExecutor(request) { | ||
const operationAst = (0, utils_1.getOperationASTFromRequest)(request); | ||
return operationAst.operation === 'subscription' ? executor(request) : loader.load(request); | ||
const operationType = request.operationType ?? (0, utils_1.getOperationASTFromRequest)(request)?.operation; | ||
switch (operationType) { | ||
case 'query': | ||
return queryLoader.load(request); | ||
case 'mutation': | ||
return mutationLoader.load(request); | ||
case 'subscription': | ||
return executor(request); | ||
default: | ||
throw new Error(`Invalid operation type "${operationType}"`); | ||
} | ||
}; | ||
@@ -27,28 +37,9 @@ } | ||
} | ||
const execBatches = []; | ||
let index = 0; | ||
const request = requests[index]; | ||
let currentBatch = [request]; | ||
execBatches.push(currentBatch); | ||
const operationAst = (0, utils_1.getOperationASTFromRequest)(request); | ||
const operationType = operationAst.operation; | ||
if (operationType == null) { | ||
throw new Error('could not identify operation type of document'); | ||
} | ||
while (++index < requests.length) { | ||
const currentRequest = requests[index]; | ||
const currentOperationAST = (0, utils_1.getOperationASTFromRequest)(currentRequest); | ||
const currentOperationType = currentOperationAST.operation; | ||
if (operationType === currentOperationType) { | ||
currentBatch.push(currentRequest); | ||
const mergedRequests = (0, mergeRequests_js_1.mergeRequests)(requests, extensionsReducer); | ||
return new value_or_promise_1.ValueOrPromise(() => executor(mergedRequests)).then(resultBatches => { | ||
if ((0, utils_1.isAsyncIterable)(resultBatches)) { | ||
throw new Error('Executor must not return incremental results for batching'); | ||
} | ||
else { | ||
currentBatch = [currentRequest]; | ||
execBatches.push(currentBatch); | ||
} | ||
} | ||
return value_or_promise_1.ValueOrPromise.all(execBatches.map(execBatch => new value_or_promise_1.ValueOrPromise(() => { | ||
const mergedRequests = (0, mergeRequests_js_1.mergeRequests)(execBatch, extensionsReducer); | ||
return executor(mergedRequests); | ||
}).then(resultBatches => (0, splitResult_js_1.splitResult)(resultBatches, execBatch.length)))).then(results => results.flat()); | ||
return (0, splitResult_js_1.splitResult)(resultBatches, requests.length); | ||
}); | ||
}; | ||
@@ -55,0 +46,0 @@ } |
@@ -93,2 +93,3 @@ "use strict"; | ||
operationType, | ||
rootValue: requests[0].rootValue, | ||
}; | ||
@@ -95,0 +96,0 @@ } |
@@ -36,3 +36,4 @@ "use strict"; | ||
const newError = (0, utils_1.relocatedError)(error, [originalKey, ...error.path.slice(1)]); | ||
const resultErrors = (splitResults[index].errors = (splitResults[index].errors || [])); | ||
const resultErrors = (splitResults[index].errors = (splitResults[index].errors || | ||
[])); | ||
resultErrors.push(newError); | ||
@@ -39,0 +40,0 @@ } |
import DataLoader from 'dataloader'; | ||
import { getOperationASTFromRequest } from '@graphql-tools/utils'; | ||
import { ValueOrPromise } from 'value-or-promise'; | ||
import { getOperationASTFromRequest, isAsyncIterable, } from '@graphql-tools/utils'; | ||
import { mergeRequests } from './mergeRequests.js'; | ||
import { splitResult } from './splitResult.js'; | ||
import { ValueOrPromise } from 'value-or-promise'; | ||
export function createBatchingExecutor(executor, dataLoaderOptions, extensionsReducer = defaultExtensionsReducer) { | ||
const loadFn = createLoadFn(executor, extensionsReducer); | ||
const loader = new DataLoader(loadFn, dataLoaderOptions); | ||
const queryLoader = new DataLoader(loadFn, dataLoaderOptions); | ||
const mutationLoader = new DataLoader(loadFn, dataLoaderOptions); | ||
return function batchingExecutor(request) { | ||
const operationAst = getOperationASTFromRequest(request); | ||
return operationAst.operation === 'subscription' ? executor(request) : loader.load(request); | ||
const operationType = request.operationType ?? getOperationASTFromRequest(request)?.operation; | ||
switch (operationType) { | ||
case 'query': | ||
return queryLoader.load(request); | ||
case 'mutation': | ||
return mutationLoader.load(request); | ||
case 'subscription': | ||
return executor(request); | ||
default: | ||
throw new Error(`Invalid operation type "${operationType}"`); | ||
} | ||
}; | ||
@@ -21,28 +31,9 @@ } | ||
} | ||
const execBatches = []; | ||
let index = 0; | ||
const request = requests[index]; | ||
let currentBatch = [request]; | ||
execBatches.push(currentBatch); | ||
const operationAst = getOperationASTFromRequest(request); | ||
const operationType = operationAst.operation; | ||
if (operationType == null) { | ||
throw new Error('could not identify operation type of document'); | ||
} | ||
while (++index < requests.length) { | ||
const currentRequest = requests[index]; | ||
const currentOperationAST = getOperationASTFromRequest(currentRequest); | ||
const currentOperationType = currentOperationAST.operation; | ||
if (operationType === currentOperationType) { | ||
currentBatch.push(currentRequest); | ||
const mergedRequests = mergeRequests(requests, extensionsReducer); | ||
return new ValueOrPromise(() => executor(mergedRequests)).then(resultBatches => { | ||
if (isAsyncIterable(resultBatches)) { | ||
throw new Error('Executor must not return incremental results for batching'); | ||
} | ||
else { | ||
currentBatch = [currentRequest]; | ||
execBatches.push(currentBatch); | ||
} | ||
} | ||
return ValueOrPromise.all(execBatches.map(execBatch => new ValueOrPromise(() => { | ||
const mergedRequests = mergeRequests(execBatch, extensionsReducer); | ||
return executor(mergedRequests); | ||
}).then(resultBatches => splitResult(resultBatches, execBatch.length)))).then(results => results.flat()); | ||
return splitResult(resultBatches, requests.length); | ||
}); | ||
}; | ||
@@ -49,0 +40,0 @@ } |
// adapted from https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js | ||
import { visit, Kind, } from 'graphql'; | ||
import { Kind, visit, } from 'graphql'; | ||
import { getOperationASTFromRequest } from '@graphql-tools/utils'; | ||
@@ -90,2 +90,3 @@ import { createPrefix } from './prefix.js'; | ||
operationType, | ||
rootValue: requests[0].rootValue, | ||
}; | ||
@@ -92,0 +93,0 @@ } |
@@ -33,3 +33,4 @@ // adapted from https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js | ||
const newError = relocatedError(error, [originalKey, ...error.path.slice(1)]); | ||
const resultErrors = (splitResults[index].errors = (splitResults[index].errors || [])); | ||
const resultErrors = (splitResults[index].errors = (splitResults[index].errors || | ||
[])); | ||
resultErrors.push(newError); | ||
@@ -36,0 +37,0 @@ } |
{ | ||
"name": "@graphql-tools/batch-execute", | ||
"version": "9.0.0", | ||
"version": "9.0.1-rc-20230809113950-f4100f90", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -10,3 +10,3 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@graphql-tools/utils": "^10.0.0", | ||
"@graphql-tools/utils": "10.0.5-rc-20230809113950-f4100f90", | ||
"dataloader": "^2.2.2", | ||
@@ -13,0 +13,0 @@ "tslib": "^2.4.0", |
import DataLoader from 'dataloader'; | ||
import { Executor, ExecutionRequest } from '@graphql-tools/utils'; | ||
import { ExecutionRequest, Executor } from '@graphql-tools/utils'; | ||
export declare function createBatchingExecutor(executor: Executor, dataLoaderOptions?: DataLoader.Options<any, any, any>, extensionsReducer?: (mergedExtensions: Record<string, any>, request: ExecutionRequest) => Record<string, any>): Executor; |
@@ -36,2 +36,2 @@ import { ExecutionRequest } from '@graphql-tools/utils'; | ||
*/ | ||
export declare function mergeRequests(requests: Array<ExecutionRequest>, extensionsReducer: (mergedExtensions: Record<string, any>, request: ExecutionRequest) => Record<string, any>): ExecutionRequest; | ||
export declare function mergeRequests(requests: ReadonlyArray<ExecutionRequest>, extensionsReducer: (mergedExtensions: Record<string, any>, request: ExecutionRequest) => Record<string, any>): ExecutionRequest; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
35709
819
2
+ Added@graphql-tools/utils@10.0.5-rc-20230809113950-f4100f90(transitive)
- Removed@graphql-tools/utils@10.3.1(transitive)
- Removedcross-inspect@1.0.0(transitive)
Updated@graphql-tools/utils@10.0.5-rc-20230809113950-f4100f90