@graphql-tools/batch-execute
Advanced tools
Comparing version 8.1.1-alpha-f6c67111.0 to 8.1.1
import DataLoader from 'dataloader'; | ||
import { ExecutionRequest, Executor } from '@graphql-tools/utils'; | ||
import { Executor, ExecutionRequest } 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; |
59
index.js
@@ -13,6 +13,6 @@ 'use strict'; | ||
function createPrefix(index) { | ||
return `graphqlTools${index}_`; | ||
return `_${index}_`; | ||
} | ||
function parseKey(prefixedKey) { | ||
const match = /^graphqlTools([\d]+)_(.*)$/.exec(prefixedKey); | ||
const match = /^_([\d]+)_(.*)$/.exec(prefixedKey); | ||
if (match && match.length === 3 && !isNaN(Number(match[1])) && match[2]) { | ||
@@ -59,3 +59,3 @@ return { index: Number(match[1]), originalKey: match[2] }; | ||
*/ | ||
function mergeRequests(requests, extensionsReducer) { | ||
function mergeRequests(operationType, requests, extensionsReducer) { | ||
const mergedVariables = Object.create(null); | ||
@@ -68,3 +68,3 @@ const mergedVariableDefinitions = []; | ||
const request = requests[index]; | ||
const prefixedRequests = prefixRequest(createPrefix(index), request); | ||
const prefixedRequests = prefixRequest(createPrefix(index), request, operationType); | ||
for (const def of prefixedRequests.document.definitions) { | ||
@@ -86,3 +86,3 @@ if (isOperationDefinition(def)) { | ||
kind: graphql.Kind.OPERATION_DEFINITION, | ||
operation: requests[0].operationType, | ||
operation: operationType, | ||
variableDefinitions: mergedVariableDefinitions, | ||
@@ -103,6 +103,6 @@ selectionSet: { | ||
info: requests[0].info, | ||
operationType: requests[0].operationType, | ||
operationType, | ||
}; | ||
} | ||
function prefixRequest(prefix, request) { | ||
function prefixRequest(prefix, request, operationType) { | ||
var _a; | ||
@@ -115,7 +115,13 @@ const executionVariables = (_a = request.variables) !== null && _a !== void 0 ? _a : {}; | ||
const executionVariableNames = Object.keys(executionVariables); | ||
if (executionVariableNames.length > 0) { | ||
const hasFragmentDefinitions = request.document.definitions.some(def => isFragmentDefinition(def)); | ||
const fragmentSpreadImpl = {}; | ||
if (executionVariableNames.length > 0 || hasFragmentDefinitions) { | ||
prefixedDocument = graphql.visit(prefixedDocument, { | ||
[graphql.Kind.VARIABLE]: prefixNode, | ||
[graphql.Kind.FRAGMENT_DEFINITION]: prefixNode, | ||
[graphql.Kind.FRAGMENT_SPREAD]: prefixNode, | ||
[graphql.Kind.FRAGMENT_SPREAD]: node => { | ||
node = prefixNodeName(node, prefix); | ||
fragmentSpreadImpl[node.name.value] = true; | ||
return node; | ||
}, | ||
}); | ||
@@ -127,6 +133,14 @@ } | ||
} | ||
if (hasFragmentDefinitions) { | ||
prefixedDocument = { | ||
...prefixedDocument, | ||
definitions: prefixedDocument.definitions.filter(def => { | ||
return !isFragmentDefinition(def) || fragmentSpreadImpl[def.name.value]; | ||
}), | ||
}; | ||
} | ||
return { | ||
document: prefixedDocument, | ||
variables: prefixedVariables, | ||
operationType: request.operationType, | ||
operationType, | ||
}; | ||
@@ -296,5 +310,11 @@ } | ||
const newError = utils.relocatedError(error, [originalKey, ...error.path.slice(1)]); | ||
const errors = (splitResults[index].errors = (splitResults[index].errors || [])); | ||
errors.push(newError); | ||
const resultErrors = (splitResults[index].errors = (splitResults[index].errors || [])); | ||
resultErrors.push(newError); | ||
} | ||
else { | ||
splitResults.forEach(result => { | ||
const resultErrors = (result.errors = (result.errors || [])); | ||
resultErrors.push(new graphql.GraphQLError(error.message)); | ||
}); | ||
} | ||
} | ||
@@ -320,12 +340,13 @@ } | ||
const operationType = request.operationType; | ||
if (operationType == null) { | ||
throw new Error('could not identify operation type of document'); | ||
} | ||
while (++index < requests.length) { | ||
const currentOperationType = requests[index].operationType; | ||
if (operationType == null) { | ||
throw new Error('Could not identify operation type of document.'); | ||
} | ||
const currentRequest = requests[index]; | ||
const currentOperationType = currentRequest.operationType; | ||
if (operationType === currentOperationType) { | ||
currentBatch.push(requests[index]); | ||
currentBatch.push(currentRequest); | ||
} | ||
else { | ||
currentBatch = [requests[index]]; | ||
currentBatch = [currentRequest]; | ||
execBatches.push(currentBatch); | ||
@@ -335,3 +356,3 @@ } | ||
const results = await Promise.all(execBatches.map(async (execBatch) => { | ||
const mergedRequests = mergeRequests(execBatch, extensionsReducer); | ||
const mergedRequests = mergeRequests(execBatch[0].operationType, execBatch, extensionsReducer); | ||
const resultBatches = (await executor(mergedRequests)); | ||
@@ -338,0 +359,0 @@ return splitResult(resultBatches, execBatch.length); |
@@ -0,1 +1,2 @@ | ||
import { OperationTypeNode } from 'graphql'; | ||
import { ExecutionRequest } from '@graphql-tools/utils'; | ||
@@ -36,2 +37,2 @@ /** | ||
*/ | ||
export declare function mergeRequests(requests: Array<ExecutionRequest>, extensionsReducer: (mergedExtensions: Record<string, any>, request: ExecutionRequest) => Record<string, any>): ExecutionRequest; | ||
export declare function mergeRequests(operationType: OperationTypeNode, requests: Array<ExecutionRequest>, extensionsReducer: (mergedExtensions: Record<string, any>, request: ExecutionRequest) => Record<string, any>): ExecutionRequest; |
{ | ||
"name": "@graphql-tools/batch-execute", | ||
"version": "8.1.1-alpha-f6c67111.0", | ||
"version": "8.1.1", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -10,3 +10,3 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@graphql-tools/utils": "9.0.0-alpha-f6c67111.0", | ||
"@graphql-tools/utils": "^8.2.4", | ||
"dataloader": "2.0.0", | ||
@@ -13,0 +13,0 @@ "tslib": "~2.3.0", |
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
29910
768
1
+ Added@graphql-tools/utils@8.13.1(transitive)
+ Addedtslib@2.8.1(transitive)
- Removed@graphql-tools/utils@9.0.0-alpha-f6c67111.0(transitive)
Updated@graphql-tools/utils@^8.2.4