@graphql-tools/batch-execute
Advanced tools
Comparing version 7.1.3-alpha-77a90f25.0 to 7.1.3-alpha-81b1d7b7.0
import DataLoader from 'dataloader'; | ||
import { ExecutionParams, Executor } from '@graphql-tools/utils'; | ||
export declare const getBatchingExecutor: (A1: Record<string, any>, A2: Executor<Record<string, any>>, A3: DataLoader.Options<any, any, any>, A4: (mergedExtensions: Record<string, any>, executionParams: ExecutionParams) => Record<string, any>) => Executor<Record<string, any>>; | ||
export declare const getBatchingExecutor: (A1: Record<string, any>, A2: Executor<Record<string, any>, Record<string, any>>, A3: DataLoader.Options<any, any, any> | undefined, A4: ((mergedExtensions: Record<string, any>, executionParams: ExecutionParams) => Record<string, any>) | undefined) => Executor<Record<string, any>, Record<string, any>>; |
153
es5/index.js
@@ -61,2 +61,3 @@ 'use strict'; | ||
function mergeExecutionParams(execs, extensionsReducer) { | ||
var e_1, _a; | ||
var mergedVariables = Object.create(null); | ||
@@ -68,18 +69,33 @@ var mergedVariableDefinitions = []; | ||
var operation; | ||
execs.forEach(function (executionParams, index) { | ||
for (var index in execs) { | ||
var executionParams = execs[index]; | ||
var prefixedExecutionParams = prefixExecutionParams(createPrefix(index), executionParams); | ||
prefixedExecutionParams.document.definitions.forEach(function (def) { | ||
var _a; | ||
if (isOperationDefinition(def)) { | ||
operation = def.operation; | ||
mergedSelections.push.apply(mergedSelections, tslib.__spreadArray([], tslib.__read(def.selectionSet.selections))); | ||
mergedVariableDefinitions.push.apply(mergedVariableDefinitions, tslib.__spreadArray([], tslib.__read(((_a = def.variableDefinitions) !== null && _a !== void 0 ? _a : [])))); | ||
try { | ||
for (var _b = (e_1 = void 0, tslib.__values(prefixedExecutionParams.document.definitions)), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var def = _c.value; | ||
if (isOperationDefinition(def)) { | ||
operation = def.operation; | ||
mergedSelections.push.apply(mergedSelections, tslib.__spreadArray([], tslib.__read(def.selectionSet.selections))); | ||
if (def.variableDefinitions) { | ||
mergedVariableDefinitions.push.apply(mergedVariableDefinitions, tslib.__spreadArray([], tslib.__read(def.variableDefinitions))); | ||
} | ||
} | ||
if (isFragmentDefinition(def)) { | ||
mergedFragmentDefinitions.push(def); | ||
} | ||
} | ||
if (isFragmentDefinition(def)) { | ||
mergedFragmentDefinitions.push(def); | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
}); | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
Object.assign(mergedVariables, prefixedExecutionParams.variables); | ||
mergedExtensions = extensionsReducer(mergedExtensions, executionParams); | ||
}); | ||
} | ||
if (operation == null) { | ||
throw new Error('Could not identify operation type. Did the document only include fragment definitions?'); | ||
} | ||
var mergedOperationDefinition = { | ||
@@ -107,4 +123,6 @@ kind: graphql.Kind.OPERATION_DEFINITION, | ||
var _a; | ||
var _b; | ||
var document = aliasTopLevelFields(prefix, executionParams.document); | ||
var variableNames = Object.keys(executionParams.variables); | ||
var executionVariables = (_b = executionParams.variables) !== null && _b !== void 0 ? _b : {}; | ||
var variableNames = Object.keys(executionVariables); | ||
if (variableNames.length === 0) { | ||
@@ -119,3 +137,3 @@ return tslib.__assign(tslib.__assign({}, executionParams), { document: document }); | ||
var prefixedVariables = variableNames.reduce(function (acc, name) { | ||
acc[prefix + name] = executionParams.variables[name]; | ||
acc[prefix + name] = executionVariables[name]; | ||
return acc; | ||
@@ -241,3 +259,5 @@ }, Object.create(null)); | ||
*/ | ||
function splitResult(mergedResult, numResults) { | ||
function splitResult(_a, numResults) { | ||
var _b, e_1, _c; | ||
var data = _a.data, errors = _a.errors; | ||
var splitResults = []; | ||
@@ -247,45 +267,41 @@ for (var i = 0; i < numResults; i++) { | ||
} | ||
var data = mergedResult.data; | ||
if (data) { | ||
Object.keys(data).forEach(function (prefixedKey) { | ||
var _a; | ||
var _b = parseKey(prefixedKey), index = _b.index, originalKey = _b.originalKey; | ||
if (!splitResults[index].data) { | ||
splitResults[index].data = (_a = {}, _a[originalKey] = data[prefixedKey], _a); | ||
for (var prefixedKey in data) { | ||
var parsedKey = parseKey(prefixedKey); | ||
utils.assertSome(parsedKey, "'parsedKey' should not be null."); | ||
var index = parsedKey.index, originalKey = parsedKey.originalKey; | ||
var result = splitResults[index]; | ||
if (result == null) { | ||
continue; | ||
} | ||
if (result.data == null) { | ||
result.data = (_b = {}, _b[originalKey] = data[prefixedKey], _b); | ||
} | ||
else { | ||
splitResults[index].data[originalKey] = data[prefixedKey]; | ||
result.data[originalKey] = data[prefixedKey]; | ||
} | ||
}); | ||
} | ||
} | ||
var errors = mergedResult.errors; | ||
if (errors) { | ||
var newErrors_1 = Object.create(null); | ||
errors.forEach(function (error) { | ||
if (error.path) { | ||
var parsedKey = parseKey(error.path[0]); | ||
if (parsedKey) { | ||
var index = parsedKey.index, originalKey = parsedKey.originalKey; | ||
var newError = utils.relocatedError(error, tslib.__spreadArray([originalKey], tslib.__read(error.path.slice(1)))); | ||
if (!newErrors_1[index]) { | ||
newErrors_1[index] = [newError]; | ||
try { | ||
for (var errors_1 = tslib.__values(errors), errors_1_1 = errors_1.next(); !errors_1_1.done; errors_1_1 = errors_1.next()) { | ||
var error = errors_1_1.value; | ||
if (error.path) { | ||
var parsedKey = parseKey(error.path[0]); | ||
if (parsedKey) { | ||
var index = parsedKey.index, originalKey = parsedKey.originalKey; | ||
var newError = utils.relocatedError(error, tslib.__spreadArray([originalKey], tslib.__read(error.path.slice(1)))); | ||
var errors_2 = (splitResults[index].errors = (splitResults[index].errors || [])); | ||
errors_2.push(newError); | ||
} | ||
else { | ||
newErrors_1[index].push(newError); | ||
} | ||
return; | ||
} | ||
} | ||
splitResults.forEach(function (_splitResult, index) { | ||
if (!newErrors_1[index]) { | ||
newErrors_1[index] = [error]; | ||
} | ||
else { | ||
newErrors_1[index].push(error); | ||
} | ||
}); | ||
}); | ||
Object.keys(newErrors_1).forEach(function (index) { | ||
splitResults[index].errors = newErrors_1[index]; | ||
}); | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (errors_1_1 && !errors_1_1.done && (_c = errors_1.return)) _c.call(errors_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
} | ||
@@ -296,4 +312,10 @@ return splitResults; | ||
function createBatchingExecutor(executor, dataLoaderOptions, extensionsReducer) { | ||
var loader = new DataLoader(createLoadFn(executor, extensionsReducer !== null && extensionsReducer !== void 0 ? extensionsReducer : defaultExtensionsReducer), dataLoaderOptions); | ||
return function (executionParams) { return loader.load(executionParams); }; | ||
if (extensionsReducer === void 0) { extensionsReducer = defaultExtensionsReducer; } | ||
var loader = new DataLoader(createLoadFn(executor, extensionsReducer), dataLoaderOptions); | ||
return function (executionParams) { | ||
var _a; | ||
return ((_a = executionParams.info) === null || _a === void 0 ? void 0 : _a.operation.operation) === 'subscription' | ||
? executor(executionParams) | ||
: loader.load(executionParams); | ||
}; | ||
} | ||
@@ -304,3 +326,4 @@ function createLoadFn(executor, extensionsReducer) { | ||
var execBatches, index, exec, currentBatch, operationType, currentOperationType, executionResults; | ||
return tslib.__generator(this, function (_a) { | ||
var _a, _b; | ||
return tslib.__generator(this, function (_c) { | ||
execBatches = []; | ||
@@ -311,5 +334,11 @@ index = 0; | ||
execBatches.push(currentBatch); | ||
operationType = graphql.getOperationAST(exec.document, undefined).operation; | ||
operationType = (_a = graphql.getOperationAST(exec.document, undefined)) === null || _a === void 0 ? void 0 : _a.operation; | ||
if (operationType == null) { | ||
throw new Error('Could not identify operation type of document.'); | ||
} | ||
while (++index < execs.length) { | ||
currentOperationType = graphql.getOperationAST(execs[index].document, undefined).operation; | ||
currentOperationType = (_b = graphql.getOperationAST(execs[index].document, undefined)) === null || _b === void 0 ? void 0 : _b.operation; | ||
if (operationType == null) { | ||
throw new Error('Could not identify operation type of document.'); | ||
} | ||
if (operationType === currentOperationType) { | ||
@@ -323,14 +352,11 @@ currentBatch.push(execs[index]); | ||
} | ||
executionResults = []; | ||
execBatches.forEach(function (execBatch) { | ||
executionResults = execBatches.map(function (execBatch) { | ||
var mergedExecutionParams = mergeExecutionParams(execBatch, extensionsReducer); | ||
executionResults.push(new valueOrPromise.ValueOrPromise(function () { return executor(mergedExecutionParams); })); | ||
return new valueOrPromise.ValueOrPromise(function () { return executor(mergedExecutionParams); }); | ||
}); | ||
return [2 /*return*/, valueOrPromise.ValueOrPromise.all(executionResults).then(function (resultBatches) { | ||
var results = []; | ||
resultBatches.forEach(function (resultBatch, index) { | ||
results = results.concat(splitResult(resultBatch, execBatches[index].length)); | ||
}); | ||
return results; | ||
}).resolve()]; | ||
return [2 /*return*/, valueOrPromise.ValueOrPromise.all(executionResults) | ||
.then(function (resultBatches) { | ||
return resultBatches.reduce(function (results, resultBatch, index) { return results.concat(splitResult(resultBatch, execBatches[index].length)); }, new Array()); | ||
}) | ||
.resolve()]; | ||
}); | ||
@@ -383,2 +409,1 @@ }); }; | ||
exports.getBatchingExecutor = getBatchingExecutor; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@graphql-tools/batch-execute/es5", | ||
"version": "7.1.3-alpha-77a90f25.0", | ||
"version": "7.1.3-alpha-81b1d7b7.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -10,3 +10,3 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@graphql-tools/utils": "8.0.0-alpha-77a90f25.0", | ||
"@graphql-tools/utils": "8.0.0-alpha-81b1d7b7.0", | ||
"dataloader": "2.0.0", | ||
@@ -13,0 +13,0 @@ "tslib": "~2.3.0", |
@@ -1,5 +0,5 @@ | ||
export declare function createPrefix(index: number): string; | ||
export declare function parseKey(prefixedKey: string): { | ||
export declare function createPrefix(index: string): string; | ||
export declare function parseKey(prefixedKey: string): null | { | ||
index: number; | ||
originalKey: string; | ||
}; |
@@ -5,2 +5,2 @@ import { ExecutionResult } from 'graphql'; | ||
*/ | ||
export declare function splitResult(mergedResult: ExecutionResult, numResults: number): Array<ExecutionResult>; | ||
export declare function splitResult({ data, errors }: ExecutionResult, numResults: number): Array<ExecutionResult>; |
import DataLoader from 'dataloader'; | ||
import { ExecutionParams, Executor } from '@graphql-tools/utils'; | ||
export declare const getBatchingExecutor: (A1: Record<string, any>, A2: Executor<Record<string, any>>, A3: DataLoader.Options<any, any, any>, A4: (mergedExtensions: Record<string, any>, executionParams: ExecutionParams) => Record<string, any>) => Executor<Record<string, any>>; | ||
export declare const getBatchingExecutor: (A1: Record<string, any>, A2: Executor<Record<string, any>, Record<string, any>>, A3: DataLoader.Options<any, any, any> | undefined, A4: ((mergedExtensions: Record<string, any>, executionParams: ExecutionParams) => Record<string, any>) | undefined) => Executor<Record<string, any>, Record<string, any>>; |
108
index.js
@@ -66,10 +66,12 @@ 'use strict'; | ||
let operation; | ||
execs.forEach((executionParams, index) => { | ||
for (const index in execs) { | ||
const executionParams = execs[index]; | ||
const prefixedExecutionParams = prefixExecutionParams(createPrefix(index), executionParams); | ||
prefixedExecutionParams.document.definitions.forEach(def => { | ||
var _a; | ||
for (const def of prefixedExecutionParams.document.definitions) { | ||
if (isOperationDefinition(def)) { | ||
operation = def.operation; | ||
mergedSelections.push(...def.selectionSet.selections); | ||
mergedVariableDefinitions.push(...((_a = def.variableDefinitions) !== null && _a !== void 0 ? _a : [])); | ||
if (def.variableDefinitions) { | ||
mergedVariableDefinitions.push(...def.variableDefinitions); | ||
} | ||
} | ||
@@ -79,6 +81,9 @@ if (isFragmentDefinition(def)) { | ||
} | ||
}); | ||
} | ||
Object.assign(mergedVariables, prefixedExecutionParams.variables); | ||
mergedExtensions = extensionsReducer(mergedExtensions, executionParams); | ||
}); | ||
} | ||
if (operation == null) { | ||
throw new Error('Could not identify operation type. Did the document only include fragment definitions?'); | ||
} | ||
const mergedOperationDefinition = { | ||
@@ -105,4 +110,6 @@ kind: graphql.Kind.OPERATION_DEFINITION, | ||
function prefixExecutionParams(prefix, executionParams) { | ||
var _a; | ||
let document = aliasTopLevelFields(prefix, executionParams.document); | ||
const variableNames = Object.keys(executionParams.variables); | ||
const executionVariables = (_a = executionParams.variables) !== null && _a !== void 0 ? _a : {}; | ||
const variableNames = Object.keys(executionVariables); | ||
if (variableNames.length === 0) { | ||
@@ -117,3 +124,3 @@ return { ...executionParams, document }; | ||
const prefixedVariables = variableNames.reduce((acc, name) => { | ||
acc[prefix + name] = executionParams.variables[name]; | ||
acc[prefix + name] = executionVariables[name]; | ||
return acc; | ||
@@ -262,3 +269,3 @@ }, Object.create(null)); | ||
*/ | ||
function splitResult(mergedResult, numResults) { | ||
function splitResult({ data, errors }, numResults) { | ||
const splitResults = []; | ||
@@ -268,18 +275,21 @@ for (let i = 0; i < numResults; i++) { | ||
} | ||
const data = mergedResult.data; | ||
if (data) { | ||
Object.keys(data).forEach(prefixedKey => { | ||
const { index, originalKey } = parseKey(prefixedKey); | ||
if (!splitResults[index].data) { | ||
splitResults[index].data = { [originalKey]: data[prefixedKey] }; | ||
for (const prefixedKey in data) { | ||
const parsedKey = parseKey(prefixedKey); | ||
utils.assertSome(parsedKey, "'parsedKey' should not be null."); | ||
const { index, originalKey } = parsedKey; | ||
const result = splitResults[index]; | ||
if (result == null) { | ||
continue; | ||
} | ||
if (result.data == null) { | ||
result.data = { [originalKey]: data[prefixedKey] }; | ||
} | ||
else { | ||
splitResults[index].data[originalKey] = data[prefixedKey]; | ||
result.data[originalKey] = data[prefixedKey]; | ||
} | ||
}); | ||
} | ||
} | ||
const errors = mergedResult.errors; | ||
if (errors) { | ||
const newErrors = Object.create(null); | ||
errors.forEach(error => { | ||
for (const error of errors) { | ||
if (error.path) { | ||
@@ -290,23 +300,7 @@ const parsedKey = parseKey(error.path[0]); | ||
const newError = utils.relocatedError(error, [originalKey, ...error.path.slice(1)]); | ||
if (!newErrors[index]) { | ||
newErrors[index] = [newError]; | ||
} | ||
else { | ||
newErrors[index].push(newError); | ||
} | ||
return; | ||
const errors = (splitResults[index].errors = (splitResults[index].errors || [])); | ||
errors.push(newError); | ||
} | ||
} | ||
splitResults.forEach((_splitResult, index) => { | ||
if (!newErrors[index]) { | ||
newErrors[index] = [error]; | ||
} | ||
else { | ||
newErrors[index].push(error); | ||
} | ||
}); | ||
}); | ||
Object.keys(newErrors).forEach(index => { | ||
splitResults[index].errors = newErrors[index]; | ||
}); | ||
} | ||
} | ||
@@ -316,8 +310,14 @@ return splitResults; | ||
function createBatchingExecutor(executor, dataLoaderOptions, extensionsReducer) { | ||
const loader = new DataLoader(createLoadFn(executor, extensionsReducer !== null && extensionsReducer !== void 0 ? extensionsReducer : defaultExtensionsReducer), dataLoaderOptions); | ||
return (executionParams) => loader.load(executionParams); | ||
function createBatchingExecutor(executor, dataLoaderOptions, extensionsReducer = defaultExtensionsReducer) { | ||
const loader = new DataLoader(createLoadFn(executor, extensionsReducer), dataLoaderOptions); | ||
return (executionParams) => { | ||
var _a; | ||
return ((_a = executionParams.info) === null || _a === void 0 ? void 0 : _a.operation.operation) === 'subscription' | ||
? executor(executionParams) | ||
: loader.load(executionParams); | ||
}; | ||
} | ||
function createLoadFn(executor, extensionsReducer) { | ||
return async (execs) => { | ||
var _a, _b; | ||
const execBatches = []; | ||
@@ -328,5 +328,11 @@ let index = 0; | ||
execBatches.push(currentBatch); | ||
const operationType = graphql.getOperationAST(exec.document, undefined).operation; | ||
const operationType = (_a = graphql.getOperationAST(exec.document, undefined)) === null || _a === void 0 ? void 0 : _a.operation; | ||
if (operationType == null) { | ||
throw new Error('Could not identify operation type of document.'); | ||
} | ||
while (++index < execs.length) { | ||
const currentOperationType = graphql.getOperationAST(execs[index].document, undefined).operation; | ||
const currentOperationType = (_b = graphql.getOperationAST(execs[index].document, undefined)) === null || _b === void 0 ? void 0 : _b.operation; | ||
if (operationType == null) { | ||
throw new Error('Could not identify operation type of document.'); | ||
} | ||
if (operationType === currentOperationType) { | ||
@@ -340,14 +346,9 @@ currentBatch.push(execs[index]); | ||
} | ||
const executionResults = []; | ||
execBatches.forEach(execBatch => { | ||
const executionResults = execBatches.map(execBatch => { | ||
const mergedExecutionParams = mergeExecutionParams(execBatch, extensionsReducer); | ||
executionResults.push(new valueOrPromise.ValueOrPromise(() => executor(mergedExecutionParams))); | ||
return new valueOrPromise.ValueOrPromise(() => executor(mergedExecutionParams)); | ||
}); | ||
return valueOrPromise.ValueOrPromise.all(executionResults).then(resultBatches => { | ||
let results = []; | ||
resultBatches.forEach((resultBatch, index) => { | ||
results = results.concat(splitResult(resultBatch, execBatches[index].length)); | ||
}); | ||
return results; | ||
}).resolve(); | ||
return valueOrPromise.ValueOrPromise.all(executionResults) | ||
.then(resultBatches => resultBatches.reduce((results, resultBatch, index) => results.concat(splitResult(resultBatch, execBatches[index].length)), new Array())) | ||
.resolve(); | ||
}; | ||
@@ -399,2 +400,1 @@ } | ||
exports.getBatchingExecutor = getBatchingExecutor; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@graphql-tools/batch-execute", | ||
"version": "7.1.3-alpha-77a90f25.0", | ||
"version": "7.1.3-alpha-81b1d7b7.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -10,3 +10,3 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@graphql-tools/utils": "8.0.0-alpha-77a90f25.0", | ||
"@graphql-tools/utils": "8.0.0-alpha-81b1d7b7.0", | ||
"dataloader": "2.0.0", | ||
@@ -13,0 +13,0 @@ "tslib": "~2.3.0", |
@@ -1,5 +0,5 @@ | ||
export declare function createPrefix(index: number): string; | ||
export declare function parseKey(prefixedKey: string): { | ||
export declare function createPrefix(index: string): string; | ||
export declare function parseKey(prefixedKey: string): null | { | ||
index: number; | ||
originalKey: string; | ||
}; |
@@ -5,2 +5,2 @@ import { ExecutionResult } from 'graphql'; | ||
*/ | ||
export declare function splitResult(mergedResult: ExecutionResult, numResults: number): Array<ExecutionResult>; | ||
export declare function splitResult({ data, errors }: ExecutionResult, numResults: number): Array<ExecutionResult>; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1638
0
67395
22
+ Added@graphql-tools/utils@8.0.0-alpha-81b1d7b7.0(transitive)
- Removed@graphql-tools/utils@8.0.0-alpha-77a90f25.0(transitive)