@graphql-tools/delegate
Advanced tools
Comparing version 6.0.11-alpha-029b3b1.0 to 6.0.11-alpha-0a4ce2b.0
167
index.cjs.js
@@ -730,6 +730,24 @@ 'use strict'; | ||
if (errors.length) { | ||
if (errors.length > 1) { | ||
return new AggregateError(errors.map(error => error.graphQLError)); | ||
if (errors.some(error => !error.path || error.path.length < 2)) { | ||
if (errors.length > 1) { | ||
const combinedError = new AggregateError(errors); | ||
return combinedError; | ||
} | ||
const error = errors[0]; | ||
return error.originalError || utils.relocatedError(error, null); | ||
} | ||
return errors[0].graphQLError; | ||
else if (errors.some(error => typeof error.path[1] === 'string')) { | ||
const childErrors = utils.getErrorsByPathSegment(errors); | ||
const result = {}; | ||
Object.keys(childErrors).forEach(pathSegment => { | ||
result[pathSegment] = handleNull(childErrors[pathSegment]); | ||
}); | ||
return result; | ||
} | ||
const childErrors = utils.getErrorsByPathSegment(errors); | ||
const result = []; | ||
Object.keys(childErrors).forEach(pathSegment => { | ||
result.push(handleNull(childErrors[pathSegment])); | ||
}); | ||
return result; | ||
} | ||
@@ -750,3 +768,3 @@ return null; | ||
} | ||
utils.setErrors(object, errors); | ||
utils.setErrors(object, errors.map(error => utils.relocatedError(error, error.path != null ? error.path.slice(1) : undefined))); | ||
setObjectSubschema(object, subschema); | ||
@@ -769,10 +787,9 @@ newParent = object; | ||
result[utils.ERROR_SYMBOL] = parent[utils.ERROR_SYMBOL].map((error) => { | ||
const path = error.relativePath.slice(); | ||
const pathSegment = path.pop(); | ||
const expandedPathSegment = pathSegment.split(delimeter); | ||
return { | ||
relativePath: path.concat(expandedPathSegment), | ||
// setting path to null will cause issues for errors that bubble up from non nullable fields | ||
graphQLError: utils.relocatedError(error.graphQLError, null), | ||
}; | ||
if (error.path != null) { | ||
const path = error.path.slice(); | ||
const pathSegment = path.shift(); | ||
const expandedPathSegment = pathSegment.split(delimeter); | ||
return utils.relocatedError(error, expandedPathSegment.concat(path)); | ||
} | ||
return error; | ||
}); | ||
@@ -783,4 +800,4 @@ result[OBJECT_SUBSCHEMA_SYMBOL] = parent[OBJECT_SUBSCHEMA_SYMBOL]; | ||
function mergeProxiedResults(target, ...sources) { | ||
const errors = target[utils.ERROR_SYMBOL].concat(...sources.map((source) => source[utils.ERROR_SYMBOL])); | ||
const fieldSubschemaMap = sources.reduce((acc, source) => { | ||
const results = sources.filter(source => !(source instanceof Error)); | ||
const fieldSubschemaMap = results.reduce((acc, source) => { | ||
const subschema = source[OBJECT_SUBSCHEMA_SYMBOL]; | ||
@@ -792,11 +809,12 @@ Object.keys(source).forEach(key => { | ||
}, {}); | ||
const result = sources.reduce(utils.mergeDeep, target); | ||
result[utils.ERROR_SYMBOL] = errors; | ||
const result = results.reduce(utils.mergeDeep, target); | ||
result[FIELD_SUBSCHEMA_MAP_SYMBOL] = target[FIELD_SUBSCHEMA_MAP_SYMBOL] | ||
? utils.mergeDeep(target[FIELD_SUBSCHEMA_MAP_SYMBOL], fieldSubschemaMap) | ||
: fieldSubschemaMap; | ||
const errors = sources.map((source) => (source instanceof Error ? source : source[utils.ERROR_SYMBOL])); | ||
result[utils.ERROR_SYMBOL] = target[utils.ERROR_SYMBOL].concat(...errors); | ||
return result; | ||
} | ||
function buildDelegationPlan(mergedTypeInfo, originalSelections, sourceSubschemas, targetSubschemas) { | ||
function buildDelegationPlan(mergedTypeInfo, fieldNodes, sourceSubschemas, targetSubschemas) { | ||
// 1. calculate if possible to delegate to given subschema | ||
@@ -818,43 +836,44 @@ // TODO: change logic so that required selection set can be spread across multiple subschemas? | ||
const { uniqueFields, nonUniqueFields } = mergedTypeInfo; | ||
const unproxiableSelections = []; | ||
const unproxiableFieldNodes = []; | ||
// 2. for each selection: | ||
const delegationMap = new Map(); | ||
originalSelections.forEach(selection => { | ||
if (selection.name.value === '__typename') { | ||
fieldNodes.forEach(fieldNode => { | ||
if (fieldNode.name.value === '__typename') { | ||
return; | ||
} | ||
// 2a. use uniqueFields map to assign fields to subschema if one of possible subschemas | ||
const uniqueSubschema = uniqueFields[selection.name.value]; | ||
const uniqueSubschema = uniqueFields[fieldNode.name.value]; | ||
if (uniqueSubschema != null) { | ||
if (proxiableSubschemas.includes(uniqueSubschema)) { | ||
const existingSubschema = delegationMap.get(uniqueSubschema); | ||
if (existingSubschema != null) { | ||
existingSubschema.push(selection); | ||
} | ||
else { | ||
delegationMap.set(uniqueSubschema, [selection]); | ||
} | ||
if (!proxiableSubschemas.includes(uniqueSubschema)) { | ||
unproxiableFieldNodes.push(fieldNode); | ||
return; | ||
} | ||
const existingSubschema = delegationMap.get(uniqueSubschema); | ||
if (existingSubschema != null) { | ||
existingSubschema.push(fieldNode); | ||
} | ||
else { | ||
unproxiableSelections.push(selection); | ||
delegationMap.set(uniqueSubschema, [fieldNode]); | ||
} | ||
return; | ||
} | ||
// 2b. use nonUniqueFields to assign to a possible subschema, | ||
// preferring one of the subschemas already targets of delegation | ||
let nonUniqueSubschemas = nonUniqueFields[fieldNode.name.value]; | ||
if (nonUniqueSubschemas == null) { | ||
unproxiableFieldNodes.push(fieldNode); | ||
return; | ||
} | ||
nonUniqueSubschemas = nonUniqueSubschemas.filter(s => proxiableSubschemas.includes(s)); | ||
if (nonUniqueSubschemas == null) { | ||
unproxiableFieldNodes.push(fieldNode); | ||
return; | ||
} | ||
const subschemas = Array.from(delegationMap.keys()); | ||
const existingSubschema = nonUniqueSubschemas.find(s => subschemas.includes(s)); | ||
if (existingSubschema != null) { | ||
delegationMap.get(existingSubschema).push(fieldNode); | ||
} | ||
else { | ||
// 2b. use nonUniqueFields to assign to a possible subschema, | ||
// preferring one of the subschemas already targets of delegation | ||
let nonUniqueSubschemas = nonUniqueFields[selection.name.value]; | ||
nonUniqueSubschemas = nonUniqueSubschemas.filter(s => proxiableSubschemas.includes(s)); | ||
if (nonUniqueSubschemas != null) { | ||
const subschemas = Array.from(delegationMap.keys()); | ||
const existingSubschema = nonUniqueSubschemas.find(s => subschemas.includes(s)); | ||
if (existingSubschema != null) { | ||
delegationMap.get(existingSubschema).push(selection); | ||
} | ||
else { | ||
delegationMap.set(nonUniqueSubschemas[0], [selection]); | ||
} | ||
} | ||
else { | ||
unproxiableSelections.push(selection); | ||
} | ||
delegationMap.set(nonUniqueSubschemas[0], [fieldNode]); | ||
} | ||
@@ -864,3 +883,3 @@ }); | ||
delegationMap, | ||
unproxiableSelections, | ||
unproxiableFieldNodes, | ||
proxiableSubschemas, | ||
@@ -870,7 +889,7 @@ nonProxiableSubschemas, | ||
} | ||
function mergeFields(mergedTypeInfo, typeName, object, originalSelections, sourceSubschemas, targetSubschemas, context, info) { | ||
if (!originalSelections.length) { | ||
function mergeFields(mergedTypeInfo, typeName, object, fieldNodes, sourceSubschemas, targetSubschemas, context, info) { | ||
if (!fieldNodes.length) { | ||
return object; | ||
} | ||
const { delegationMap, unproxiableSelections, proxiableSubschemas, nonProxiableSubschemas } = buildDelegationPlan(mergedTypeInfo, originalSelections, sourceSubschemas, targetSubschemas); | ||
const { delegationMap, unproxiableFieldNodes, proxiableSubschemas, nonProxiableSubschemas } = buildDelegationPlan(mergedTypeInfo, fieldNodes, sourceSubschemas, targetSubschemas); | ||
if (!delegationMap.size) { | ||
@@ -895,4 +914,4 @@ return object; | ||
return containsPromises | ||
? Promise.all(maybePromises).then(results => mergeFields(mergedTypeInfo, typeName, mergeProxiedResults(object, ...results), unproxiableSelections, sourceSubschemas.concat(proxiableSubschemas), nonProxiableSubschemas, context, info)) | ||
: mergeFields(mergedTypeInfo, typeName, mergeProxiedResults(object, ...maybePromises), unproxiableSelections, sourceSubschemas.concat(proxiableSubschemas), nonProxiableSubschemas, context, info); | ||
? Promise.all(maybePromises).then(results => mergeFields(mergedTypeInfo, typeName, mergeProxiedResults(object, ...results), unproxiableFieldNodes, sourceSubschemas.concat(proxiableSubschemas), nonProxiableSubschemas, context, info)) | ||
: mergeFields(mergedTypeInfo, typeName, mergeProxiedResults(object, ...maybePromises), unproxiableFieldNodes, sourceSubschemas.concat(proxiableSubschemas), nonProxiableSubschemas, context, info); | ||
} | ||
@@ -903,3 +922,3 @@ | ||
const stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a.stitchingInfo; | ||
utils.setErrors(object, errors); | ||
utils.setErrors(object, errors.map(error => utils.slicedError(error))); | ||
setObjectSubschema(object, subschema); | ||
@@ -922,5 +941,4 @@ if (skipTypeMerging || !stitchingInfo) { | ||
} | ||
const subFields = collectSubFields(info, object.__typename); | ||
const selections = getFieldsNotInSubschema(subFields, subschema, mergedTypeInfo, object.__typename); | ||
return mergeFields(mergedTypeInfo, typeName, object, selections, [subschema], targetSubschemas, context, info); | ||
const fieldNodes = getFieldsNotInSubschema(info, subschema, mergedTypeInfo, object.__typename); | ||
return mergeFields(mergedTypeInfo, typeName, object, fieldNodes, [subschema], targetSubschemas, context, info); | ||
} | ||
@@ -930,21 +948,30 @@ function collectSubFields(info, typeName) { | ||
const visitedFragmentNames = Object.create(null); | ||
const type = info.schema.getType(typeName); | ||
const partialExecutionContext = { | ||
schema: info.schema, | ||
variableValues: info.variableValues, | ||
fragments: info.fragments, | ||
}; | ||
info.fieldNodes.forEach(fieldNode => { | ||
subFieldNodes = utils.collectFields({ | ||
schema: info.schema, | ||
variableValues: info.variableValues, | ||
fragments: info.fragments, | ||
}, info.schema.getType(typeName), fieldNode.selectionSet, subFieldNodes, visitedFragmentNames); | ||
subFieldNodes = utils.collectFields(partialExecutionContext, type, fieldNode.selectionSet, subFieldNodes, visitedFragmentNames); | ||
}); | ||
const selectionSetsByField = info.schema.extensions.stitchingInfo.selectionSetsByField; | ||
Object.keys(subFieldNodes).forEach(responseName => { | ||
const fieldName = subFieldNodes[responseName][0].name.value; | ||
if (selectionSetsByField[typeName] && selectionSetsByField[typeName][fieldName]) { | ||
subFieldNodes = utils.collectFields(partialExecutionContext, type, selectionSetsByField[typeName][fieldName], subFieldNodes, visitedFragmentNames); | ||
} | ||
}); | ||
return subFieldNodes; | ||
} | ||
function getFieldsNotInSubschema(subFieldNodes, subschema, mergedTypeInfo, typeName) { | ||
function getFieldsNotInSubschema(info, subschema, mergedTypeInfo, typeName) { | ||
const typeMap = isSubschemaConfig(subschema) ? mergedTypeInfo.typeMaps.get(subschema) : subschema.getTypeMap(); | ||
const fields = typeMap[typeName].getFields(); | ||
const fieldsNotInSchema = []; | ||
const subFieldNodes = collectSubFields(info, typeName); | ||
let fieldsNotInSchema = []; | ||
Object.keys(subFieldNodes).forEach(responseName => { | ||
subFieldNodes[responseName].forEach(subFieldNode => { | ||
if (!(subFieldNode.name.value in fields)) { | ||
fieldsNotInSchema.push(subFieldNode); | ||
} | ||
}); | ||
const fieldName = subFieldNodes[responseName][0].name.value; | ||
if (!(fieldName in fields)) { | ||
fieldsNotInSchema = fieldsNotInSchema.concat(subFieldNodes[responseName]); | ||
} | ||
}); | ||
@@ -1003,3 +1030,3 @@ return fieldsNotInSchema; | ||
function checkResultAndHandleErrors(result, context, info, responseKey = utils.getResponseKeyFromInfo(info), subschema, returnType = info.returnType, skipTypeMerging) { | ||
const errors = result.errors != null ? utils.toRelativeErrors(result.errors, info) : []; | ||
const errors = result.errors != null ? result.errors : []; | ||
const data = result.data != null ? result.data[responseKey] : undefined; | ||
@@ -1006,0 +1033,0 @@ return handleResult(data, errors, subschema, context, info, returnType, skipTypeMerging); |
169
index.esm.js
import { Kind, typeFromAST, visit, visitWithTypeInfo, getNamedType, isAbstractType, TypeInfo, isObjectType, isInterfaceType, TypeNameMetaFieldDef, getNullableType, isLeafType, isCompositeType, isListType, isSchema, getOperationAST, execute, subscribe, validate, defaultFieldResolver, parse } from 'graphql'; | ||
import { applySchemaTransforms, serializeInputValue, updateArgument, implementsAbstractType, ERROR_SYMBOL, relocatedError, mergeDeep, getErrors, setErrors, collectFields, getErrorsByPathSegment, getResponseKeyFromInfo, toRelativeErrors, mapAsyncIterator, concatInlineFragments } from '@graphql-tools/utils'; | ||
import { applySchemaTransforms, serializeInputValue, updateArgument, implementsAbstractType, relocatedError, getErrorsByPathSegment, ERROR_SYMBOL, mergeDeep, getErrors, setErrors, slicedError, collectFields, getResponseKeyFromInfo, mapAsyncIterator, concatInlineFragments } from '@graphql-tools/utils'; | ||
import AggregateError from 'aggregate-error'; | ||
@@ -724,6 +724,24 @@ | ||
if (errors.length) { | ||
if (errors.length > 1) { | ||
return new AggregateError(errors.map(error => error.graphQLError)); | ||
if (errors.some(error => !error.path || error.path.length < 2)) { | ||
if (errors.length > 1) { | ||
const combinedError = new AggregateError(errors); | ||
return combinedError; | ||
} | ||
const error = errors[0]; | ||
return error.originalError || relocatedError(error, null); | ||
} | ||
return errors[0].graphQLError; | ||
else if (errors.some(error => typeof error.path[1] === 'string')) { | ||
const childErrors = getErrorsByPathSegment(errors); | ||
const result = {}; | ||
Object.keys(childErrors).forEach(pathSegment => { | ||
result[pathSegment] = handleNull(childErrors[pathSegment]); | ||
}); | ||
return result; | ||
} | ||
const childErrors = getErrorsByPathSegment(errors); | ||
const result = []; | ||
Object.keys(childErrors).forEach(pathSegment => { | ||
result.push(handleNull(childErrors[pathSegment])); | ||
}); | ||
return result; | ||
} | ||
@@ -744,3 +762,3 @@ return null; | ||
} | ||
setErrors(object, errors); | ||
setErrors(object, errors.map(error => relocatedError(error, error.path != null ? error.path.slice(1) : undefined))); | ||
setObjectSubschema(object, subschema); | ||
@@ -763,10 +781,9 @@ newParent = object; | ||
result[ERROR_SYMBOL] = parent[ERROR_SYMBOL].map((error) => { | ||
const path = error.relativePath.slice(); | ||
const pathSegment = path.pop(); | ||
const expandedPathSegment = pathSegment.split(delimeter); | ||
return { | ||
relativePath: path.concat(expandedPathSegment), | ||
// setting path to null will cause issues for errors that bubble up from non nullable fields | ||
graphQLError: relocatedError(error.graphQLError, null), | ||
}; | ||
if (error.path != null) { | ||
const path = error.path.slice(); | ||
const pathSegment = path.shift(); | ||
const expandedPathSegment = pathSegment.split(delimeter); | ||
return relocatedError(error, expandedPathSegment.concat(path)); | ||
} | ||
return error; | ||
}); | ||
@@ -777,4 +794,4 @@ result[OBJECT_SUBSCHEMA_SYMBOL] = parent[OBJECT_SUBSCHEMA_SYMBOL]; | ||
function mergeProxiedResults(target, ...sources) { | ||
const errors = target[ERROR_SYMBOL].concat(...sources.map((source) => source[ERROR_SYMBOL])); | ||
const fieldSubschemaMap = sources.reduce((acc, source) => { | ||
const results = sources.filter(source => !(source instanceof Error)); | ||
const fieldSubschemaMap = results.reduce((acc, source) => { | ||
const subschema = source[OBJECT_SUBSCHEMA_SYMBOL]; | ||
@@ -786,11 +803,12 @@ Object.keys(source).forEach(key => { | ||
}, {}); | ||
const result = sources.reduce(mergeDeep, target); | ||
result[ERROR_SYMBOL] = errors; | ||
const result = results.reduce(mergeDeep, target); | ||
result[FIELD_SUBSCHEMA_MAP_SYMBOL] = target[FIELD_SUBSCHEMA_MAP_SYMBOL] | ||
? mergeDeep(target[FIELD_SUBSCHEMA_MAP_SYMBOL], fieldSubschemaMap) | ||
: fieldSubschemaMap; | ||
const errors = sources.map((source) => (source instanceof Error ? source : source[ERROR_SYMBOL])); | ||
result[ERROR_SYMBOL] = target[ERROR_SYMBOL].concat(...errors); | ||
return result; | ||
} | ||
function buildDelegationPlan(mergedTypeInfo, originalSelections, sourceSubschemas, targetSubschemas) { | ||
function buildDelegationPlan(mergedTypeInfo, fieldNodes, sourceSubschemas, targetSubschemas) { | ||
// 1. calculate if possible to delegate to given subschema | ||
@@ -812,43 +830,44 @@ // TODO: change logic so that required selection set can be spread across multiple subschemas? | ||
const { uniqueFields, nonUniqueFields } = mergedTypeInfo; | ||
const unproxiableSelections = []; | ||
const unproxiableFieldNodes = []; | ||
// 2. for each selection: | ||
const delegationMap = new Map(); | ||
originalSelections.forEach(selection => { | ||
if (selection.name.value === '__typename') { | ||
fieldNodes.forEach(fieldNode => { | ||
if (fieldNode.name.value === '__typename') { | ||
return; | ||
} | ||
// 2a. use uniqueFields map to assign fields to subschema if one of possible subschemas | ||
const uniqueSubschema = uniqueFields[selection.name.value]; | ||
const uniqueSubschema = uniqueFields[fieldNode.name.value]; | ||
if (uniqueSubschema != null) { | ||
if (proxiableSubschemas.includes(uniqueSubschema)) { | ||
const existingSubschema = delegationMap.get(uniqueSubschema); | ||
if (existingSubschema != null) { | ||
existingSubschema.push(selection); | ||
} | ||
else { | ||
delegationMap.set(uniqueSubschema, [selection]); | ||
} | ||
if (!proxiableSubschemas.includes(uniqueSubschema)) { | ||
unproxiableFieldNodes.push(fieldNode); | ||
return; | ||
} | ||
const existingSubschema = delegationMap.get(uniqueSubschema); | ||
if (existingSubschema != null) { | ||
existingSubschema.push(fieldNode); | ||
} | ||
else { | ||
unproxiableSelections.push(selection); | ||
delegationMap.set(uniqueSubschema, [fieldNode]); | ||
} | ||
return; | ||
} | ||
// 2b. use nonUniqueFields to assign to a possible subschema, | ||
// preferring one of the subschemas already targets of delegation | ||
let nonUniqueSubschemas = nonUniqueFields[fieldNode.name.value]; | ||
if (nonUniqueSubschemas == null) { | ||
unproxiableFieldNodes.push(fieldNode); | ||
return; | ||
} | ||
nonUniqueSubschemas = nonUniqueSubschemas.filter(s => proxiableSubschemas.includes(s)); | ||
if (nonUniqueSubschemas == null) { | ||
unproxiableFieldNodes.push(fieldNode); | ||
return; | ||
} | ||
const subschemas = Array.from(delegationMap.keys()); | ||
const existingSubschema = nonUniqueSubschemas.find(s => subschemas.includes(s)); | ||
if (existingSubschema != null) { | ||
delegationMap.get(existingSubschema).push(fieldNode); | ||
} | ||
else { | ||
// 2b. use nonUniqueFields to assign to a possible subschema, | ||
// preferring one of the subschemas already targets of delegation | ||
let nonUniqueSubschemas = nonUniqueFields[selection.name.value]; | ||
nonUniqueSubschemas = nonUniqueSubschemas.filter(s => proxiableSubschemas.includes(s)); | ||
if (nonUniqueSubschemas != null) { | ||
const subschemas = Array.from(delegationMap.keys()); | ||
const existingSubschema = nonUniqueSubschemas.find(s => subschemas.includes(s)); | ||
if (existingSubschema != null) { | ||
delegationMap.get(existingSubschema).push(selection); | ||
} | ||
else { | ||
delegationMap.set(nonUniqueSubschemas[0], [selection]); | ||
} | ||
} | ||
else { | ||
unproxiableSelections.push(selection); | ||
} | ||
delegationMap.set(nonUniqueSubschemas[0], [fieldNode]); | ||
} | ||
@@ -858,3 +877,3 @@ }); | ||
delegationMap, | ||
unproxiableSelections, | ||
unproxiableFieldNodes, | ||
proxiableSubschemas, | ||
@@ -864,7 +883,7 @@ nonProxiableSubschemas, | ||
} | ||
function mergeFields(mergedTypeInfo, typeName, object, originalSelections, sourceSubschemas, targetSubschemas, context, info) { | ||
if (!originalSelections.length) { | ||
function mergeFields(mergedTypeInfo, typeName, object, fieldNodes, sourceSubschemas, targetSubschemas, context, info) { | ||
if (!fieldNodes.length) { | ||
return object; | ||
} | ||
const { delegationMap, unproxiableSelections, proxiableSubschemas, nonProxiableSubschemas } = buildDelegationPlan(mergedTypeInfo, originalSelections, sourceSubschemas, targetSubschemas); | ||
const { delegationMap, unproxiableFieldNodes, proxiableSubschemas, nonProxiableSubschemas } = buildDelegationPlan(mergedTypeInfo, fieldNodes, sourceSubschemas, targetSubschemas); | ||
if (!delegationMap.size) { | ||
@@ -889,4 +908,4 @@ return object; | ||
return containsPromises | ||
? Promise.all(maybePromises).then(results => mergeFields(mergedTypeInfo, typeName, mergeProxiedResults(object, ...results), unproxiableSelections, sourceSubschemas.concat(proxiableSubschemas), nonProxiableSubschemas, context, info)) | ||
: mergeFields(mergedTypeInfo, typeName, mergeProxiedResults(object, ...maybePromises), unproxiableSelections, sourceSubschemas.concat(proxiableSubschemas), nonProxiableSubschemas, context, info); | ||
? Promise.all(maybePromises).then(results => mergeFields(mergedTypeInfo, typeName, mergeProxiedResults(object, ...results), unproxiableFieldNodes, sourceSubschemas.concat(proxiableSubschemas), nonProxiableSubschemas, context, info)) | ||
: mergeFields(mergedTypeInfo, typeName, mergeProxiedResults(object, ...maybePromises), unproxiableFieldNodes, sourceSubschemas.concat(proxiableSubschemas), nonProxiableSubschemas, context, info); | ||
} | ||
@@ -897,3 +916,3 @@ | ||
const stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a.stitchingInfo; | ||
setErrors(object, errors); | ||
setErrors(object, errors.map(error => slicedError(error))); | ||
setObjectSubschema(object, subschema); | ||
@@ -916,5 +935,4 @@ if (skipTypeMerging || !stitchingInfo) { | ||
} | ||
const subFields = collectSubFields(info, object.__typename); | ||
const selections = getFieldsNotInSubschema(subFields, subschema, mergedTypeInfo, object.__typename); | ||
return mergeFields(mergedTypeInfo, typeName, object, selections, [subschema], targetSubschemas, context, info); | ||
const fieldNodes = getFieldsNotInSubschema(info, subschema, mergedTypeInfo, object.__typename); | ||
return mergeFields(mergedTypeInfo, typeName, object, fieldNodes, [subschema], targetSubschemas, context, info); | ||
} | ||
@@ -924,21 +942,30 @@ function collectSubFields(info, typeName) { | ||
const visitedFragmentNames = Object.create(null); | ||
const type = info.schema.getType(typeName); | ||
const partialExecutionContext = { | ||
schema: info.schema, | ||
variableValues: info.variableValues, | ||
fragments: info.fragments, | ||
}; | ||
info.fieldNodes.forEach(fieldNode => { | ||
subFieldNodes = collectFields({ | ||
schema: info.schema, | ||
variableValues: info.variableValues, | ||
fragments: info.fragments, | ||
}, info.schema.getType(typeName), fieldNode.selectionSet, subFieldNodes, visitedFragmentNames); | ||
subFieldNodes = collectFields(partialExecutionContext, type, fieldNode.selectionSet, subFieldNodes, visitedFragmentNames); | ||
}); | ||
const selectionSetsByField = info.schema.extensions.stitchingInfo.selectionSetsByField; | ||
Object.keys(subFieldNodes).forEach(responseName => { | ||
const fieldName = subFieldNodes[responseName][0].name.value; | ||
if (selectionSetsByField[typeName] && selectionSetsByField[typeName][fieldName]) { | ||
subFieldNodes = collectFields(partialExecutionContext, type, selectionSetsByField[typeName][fieldName], subFieldNodes, visitedFragmentNames); | ||
} | ||
}); | ||
return subFieldNodes; | ||
} | ||
function getFieldsNotInSubschema(subFieldNodes, subschema, mergedTypeInfo, typeName) { | ||
function getFieldsNotInSubschema(info, subschema, mergedTypeInfo, typeName) { | ||
const typeMap = isSubschemaConfig(subschema) ? mergedTypeInfo.typeMaps.get(subschema) : subschema.getTypeMap(); | ||
const fields = typeMap[typeName].getFields(); | ||
const fieldsNotInSchema = []; | ||
const subFieldNodes = collectSubFields(info, typeName); | ||
let fieldsNotInSchema = []; | ||
Object.keys(subFieldNodes).forEach(responseName => { | ||
subFieldNodes[responseName].forEach(subFieldNode => { | ||
if (!(subFieldNode.name.value in fields)) { | ||
fieldsNotInSchema.push(subFieldNode); | ||
} | ||
}); | ||
const fieldName = subFieldNodes[responseName][0].name.value; | ||
if (!(fieldName in fields)) { | ||
fieldsNotInSchema = fieldsNotInSchema.concat(subFieldNodes[responseName]); | ||
} | ||
}); | ||
@@ -997,3 +1024,3 @@ return fieldsNotInSchema; | ||
function checkResultAndHandleErrors(result, context, info, responseKey = getResponseKeyFromInfo(info), subschema, returnType = info.returnType, skipTypeMerging) { | ||
const errors = result.errors != null ? toRelativeErrors(result.errors, info) : []; | ||
const errors = result.errors != null ? result.errors : []; | ||
const data = result.data != null ? result.data[responseKey] : undefined; | ||
@@ -1000,0 +1027,0 @@ return handleResult(data, errors, subschema, context, info, returnType, skipTypeMerging); |
import { FieldNode, GraphQLResolveInfo } from 'graphql'; | ||
import { MergedTypeInfo, SubschemaConfig } from './types'; | ||
export declare function mergeFields(mergedTypeInfo: MergedTypeInfo, typeName: string, object: any, originalSelections: Array<FieldNode>, sourceSubschemas: Array<SubschemaConfig>, targetSubschemas: Array<SubschemaConfig>, context: Record<string, any>, info: GraphQLResolveInfo): any; | ||
export declare function mergeFields(mergedTypeInfo: MergedTypeInfo, typeName: string, object: any, fieldNodes: Array<FieldNode>, sourceSubschemas: Array<SubschemaConfig>, targetSubschemas: Array<SubschemaConfig>, context: Record<string, any>, info: GraphQLResolveInfo): any; |
{ | ||
"name": "@graphql-tools/delegate", | ||
"version": "6.0.11-alpha-029b3b1.0", | ||
"version": "6.0.11-alpha-0a4ce2b.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -10,4 +10,4 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@graphql-tools/schema": "6.0.11-alpha-029b3b1.0", | ||
"@graphql-tools/utils": "6.0.11-alpha-029b3b1.0", | ||
"@graphql-tools/schema": "6.0.11-alpha-0a4ce2b.0", | ||
"@graphql-tools/utils": "6.0.11-alpha-0a4ce2b.0", | ||
"aggregate-error": "3.0.1", | ||
@@ -14,0 +14,0 @@ "tslib": "~2.0.0" |
export declare function isProxiedResult(result: any): any; | ||
export declare function unwrapResult(parent: any, path: Array<string>): any; | ||
export declare function dehoistResult(parent: any, delimeter?: string): any; | ||
export declare function mergeProxiedResults(target: any, ...sources: any): any; | ||
export declare function mergeProxiedResults(target: any, ...sources: Array<any>): any; |
@@ -1,4 +0,3 @@ | ||
import { GraphQLList, GraphQLSchema, GraphQLResolveInfo } from 'graphql'; | ||
import { RelativeGraphQLError } from '@graphql-tools/utils'; | ||
import { GraphQLList, GraphQLSchema, GraphQLError, GraphQLResolveInfo } from 'graphql'; | ||
import { SubschemaConfig } from '../types'; | ||
export declare function handleList(type: GraphQLList<any>, list: Array<any>, errors: Array<RelativeGraphQLError>, subschema: GraphQLSchema | SubschemaConfig, context: Record<string, any>, info: GraphQLResolveInfo, skipTypeMerging?: boolean): any[]; | ||
export declare function handleList(type: GraphQLList<any>, list: Array<any>, errors: ReadonlyArray<GraphQLError>, subschema: GraphQLSchema | SubschemaConfig, context: Record<string, any>, info: GraphQLResolveInfo, skipTypeMerging?: boolean): any[]; |
@@ -1,3 +0,2 @@ | ||
import AggregateError from 'aggregate-error'; | ||
import { RelativeGraphQLError } from '@graphql-tools/utils'; | ||
export declare function handleNull(errors: Array<RelativeGraphQLError>): import("graphql").GraphQLError | AggregateError; | ||
import { GraphQLError } from 'graphql'; | ||
export declare function handleNull(errors: ReadonlyArray<GraphQLError>): {}; |
@@ -1,4 +0,3 @@ | ||
import { GraphQLCompositeType, GraphQLSchema, GraphQLResolveInfo } from 'graphql'; | ||
import { RelativeGraphQLError } from '@graphql-tools/utils'; | ||
import { GraphQLCompositeType, GraphQLError, GraphQLSchema, GraphQLResolveInfo } from 'graphql'; | ||
import { SubschemaConfig } from '../types'; | ||
export declare function handleObject(type: GraphQLCompositeType, object: any, errors: Array<RelativeGraphQLError>, subschema: GraphQLSchema | SubschemaConfig, context: Record<string, any>, info: GraphQLResolveInfo, skipTypeMerging?: boolean): any; | ||
export declare function handleObject(type: GraphQLCompositeType, object: any, errors: ReadonlyArray<GraphQLError>, subschema: GraphQLSchema | SubschemaConfig, context: Record<string, any>, info: GraphQLResolveInfo, skipTypeMerging?: boolean): any; |
@@ -1,4 +0,3 @@ | ||
import { GraphQLResolveInfo, GraphQLSchema } from 'graphql'; | ||
import { RelativeGraphQLError } from '@graphql-tools/utils'; | ||
import { GraphQLResolveInfo, GraphQLError, GraphQLSchema } from 'graphql'; | ||
import { SubschemaConfig } from '../types'; | ||
export declare function handleResult(result: any, errors: Array<RelativeGraphQLError>, subschema: GraphQLSchema | SubschemaConfig, context: Record<string, any>, info: GraphQLResolveInfo, returnType?: import("graphql").GraphQLOutputType, skipTypeMerging?: boolean): any; | ||
export declare function handleResult(result: any, errors: ReadonlyArray<GraphQLError>, subschema: GraphQLSchema | SubschemaConfig, context: Record<string, any>, info: GraphQLResolveInfo, returnType?: import("graphql").GraphQLOutputType, skipTypeMerging?: boolean): any; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
388273
3128
+ Added@graphql-tools/schema@6.0.11-alpha-0a4ce2b.0(transitive)
+ Added@graphql-tools/utils@6.0.11-alpha-0a4ce2b.0(transitive)
- Removed@graphql-tools/schema@6.0.11-alpha-029b3b1.0(transitive)
- Removed@graphql-tools/utils@6.0.11-alpha-029b3b1.0(transitive)