@graphql-tools/delegate
Advanced tools
Comparing version 10.0.19 to 10.0.20-alpha-20240826080044-86bd8494b97dcaaa850bc8500e6c03b3ab9dbc7c
@@ -44,2 +44,3 @@ "use strict"; | ||
missingDeferredFields.set(responseKey, deferred); | ||
handleResult(parent, responseKey, context, info); | ||
return deferred.promise; | ||
@@ -77,14 +78,29 @@ } | ||
const parentTypeName = info.parentType.name; | ||
const selectionSet = stitchingInfo.mergedTypes[parentTypeName].selectionSets.get(possibleSubschema); | ||
const selectionSets = new Set(); | ||
const mainSelectionSet = stitchingInfo.mergedTypes[parentTypeName].selectionSets.get(possibleSubschema); | ||
if (mainSelectionSet) { | ||
selectionSets.add(mainSelectionSet); | ||
} | ||
for (const fieldNode of leftOver.unproxiableFieldNodes) { | ||
const fieldName = fieldNode.name.value; | ||
const fieldSelectionSet = stitchingInfo.mergedTypes[parentTypeName].fieldSelectionSets.get(possibleSubschema)?.[fieldName]; | ||
if (fieldSelectionSet) { | ||
selectionSets.add(fieldSelectionSet); | ||
} | ||
} | ||
// Wait until the parent is flattened, then check if non proxiable subschemas are satisfied now, | ||
// then the deferred fields can be resolved | ||
if (selectionSet) { | ||
if (selectionSets.size) { | ||
const selectionSet = { | ||
kind: graphql_1.Kind.SELECTION_SET, | ||
selections: Array.from(selectionSets).flatMap(selectionSet => selectionSet.selections), | ||
}; | ||
const flattenedParent$ = flattenPromise(parent); | ||
if ((0, utils_1.isPromise)(flattenedParent$)) { | ||
flattenedParent$.then(flattenedParent => { | ||
handleFlattenedParent(flattenedParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
handleFlattenedParent(flattenedParent, parent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
}); | ||
} | ||
else { | ||
handleFlattenedParent(flattenedParent$, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
handleFlattenedParent(flattenedParent$, parent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
} | ||
@@ -95,6 +111,7 @@ } | ||
} | ||
function handleFlattenedParent(flattenedParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info) { | ||
function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info) { | ||
// If this subschema is satisfied now, try to resolve the deferred fields | ||
if (parentSatisfiedSelectionSet(flattenedParent, selectionSet)) { | ||
for (const [leftOverParent, missingFieldNodes] of leftOver.missingFieldsParentMap) { | ||
const missingFieldNodes = leftOver.missingFieldsParentMap.get(leftOverParent); | ||
if (missingFieldNodes) { | ||
const resolver = stitchingInfo.mergedTypes[parentTypeName].resolvers.get(possibleSubschema); | ||
@@ -124,2 +141,44 @@ if (resolver) { | ||
} | ||
else { | ||
// try to resolve the missing fields | ||
for (const selectionNode of selectionSet.selections) { | ||
if (selectionNode.kind === graphql_1.Kind.FIELD && selectionNode.selectionSet?.selections?.length) { | ||
const responseKey = selectionNode.alias?.value ?? selectionNode.name.value; | ||
const nestedParent = flattenedParent[responseKey]; | ||
const nestedSelectionSet = selectionNode.selectionSet; | ||
if (nestedParent != null) { | ||
if (!parentSatisfiedSelectionSet(nestedParent, nestedSelectionSet)) { | ||
async function handleNestedParentItem(nestedParentItem, fieldNode) { | ||
const nestedTypeName = nestedParentItem['__typename']; | ||
const sourceSubschema = (0, mergeFields_js_1.getSubschema)(flattenedParent, responseKey); | ||
if (sourceSubschema && nestedTypeName) { | ||
const delegationPlan = stitchingInfo.mergedTypes[nestedTypeName].delegationPlanBuilder(info.schema, sourceSubschema, info.variableValues, info.fragments, [fieldNode]); | ||
// Later optimize | ||
for (const delegationMap of delegationPlan) { | ||
for (const [subschema, selectionSet] of delegationMap) { | ||
const resolver = stitchingInfo.mergedTypes[nestedTypeName].resolvers.get(subschema); | ||
if (resolver) { | ||
const res = await resolver(nestedParentItem, context, info, subschema, selectionSet, info.parentType, info.parentType); | ||
if (res) { | ||
(0, mergeFields_js_1.handleResolverResult)(res, subschema, selectionSet, nestedParentItem, (nestedParentItem[symbols_js_1.FIELD_SUBSCHEMA_MAP_SYMBOL] ||= new Map()), info, (0, graphql_1.responsePathAsArray)(info.path), (nestedParentItem[symbols_js_1.UNPATHED_ERRORS_SYMBOL] ||= [])); | ||
} | ||
} | ||
} | ||
} | ||
if (parentSatisfiedSelectionSet(nestedParent, nestedSelectionSet)) { | ||
handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
} | ||
} | ||
} | ||
if (Array.isArray(nestedParent)) { | ||
nestedParent.forEach(nestedParentItem => handleNestedParentItem(nestedParentItem, selectionNode)); | ||
} | ||
else { | ||
handleNestedParentItem(nestedParent, selectionNode); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -227,2 +286,11 @@ function handleDeferredResolverResult(resolverResult, possibleSubschema, selectionSet, leftOverParent, leftOver, context, info) { | ||
} | ||
if (data[symbols_js_1.OBJECT_SUBSCHEMA_SYMBOL]) { | ||
newData[symbols_js_1.OBJECT_SUBSCHEMA_SYMBOL] = data[symbols_js_1.OBJECT_SUBSCHEMA_SYMBOL]; | ||
} | ||
if (data[symbols_js_1.FIELD_SUBSCHEMA_MAP_SYMBOL]) { | ||
newData[symbols_js_1.FIELD_SUBSCHEMA_MAP_SYMBOL] = data[symbols_js_1.FIELD_SUBSCHEMA_MAP_SYMBOL]; | ||
} | ||
if (data[symbols_js_1.UNPATHED_ERRORS_SYMBOL]) { | ||
newData[symbols_js_1.UNPATHED_ERRORS_SYMBOL] = data[symbols_js_1.UNPATHED_ERRORS_SYMBOL]; | ||
} | ||
if (jobs.length) { | ||
@@ -229,0 +297,0 @@ return Promise.all(jobs).then(() => newData); |
@@ -14,5 +14,5 @@ "use strict"; | ||
Object.defineProperties(object, { | ||
[symbols_js_1.OBJECT_SUBSCHEMA_SYMBOL]: { value: subschema }, | ||
[symbols_js_1.FIELD_SUBSCHEMA_MAP_SYMBOL]: { value: subschemaMap }, | ||
[symbols_js_1.UNPATHED_ERRORS_SYMBOL]: { value: errors }, | ||
[symbols_js_1.OBJECT_SUBSCHEMA_SYMBOL]: { value: subschema, writable: true }, | ||
[symbols_js_1.FIELD_SUBSCHEMA_MAP_SYMBOL]: { value: subschemaMap, writable: true }, | ||
[symbols_js_1.UNPATHED_ERRORS_SYMBOL]: { value: errors, writable: true }, | ||
}); | ||
@@ -19,0 +19,0 @@ return object; |
@@ -98,3 +98,3 @@ "use strict"; | ||
if (unreportedErrors.length === 1) { | ||
return unreportedErrors[0]; | ||
return (0, graphql_1.locatedError)(unreportedErrors[0], undefined, unreportedErrors[0].path); | ||
} | ||
@@ -101,0 +101,0 @@ return new AggregateError(unreportedErrors.map(e => |
@@ -6,3 +6,3 @@ import { defaultFieldResolver, Kind, responsePathAsArray, } from 'graphql'; | ||
import { resolveExternalValue } from './resolveExternalValue.js'; | ||
import { FIELD_SUBSCHEMA_MAP_SYMBOL, UNPATHED_ERRORS_SYMBOL } from './symbols.js'; | ||
import { FIELD_SUBSCHEMA_MAP_SYMBOL, OBJECT_SUBSCHEMA_SYMBOL, UNPATHED_ERRORS_SYMBOL, } from './symbols.js'; | ||
/** | ||
@@ -42,2 +42,3 @@ * Resolver that knows how to: | ||
missingDeferredFields.set(responseKey, deferred); | ||
handleResult(parent, responseKey, context, info); | ||
return deferred.promise; | ||
@@ -74,14 +75,29 @@ } | ||
const parentTypeName = info.parentType.name; | ||
const selectionSet = stitchingInfo.mergedTypes[parentTypeName].selectionSets.get(possibleSubschema); | ||
const selectionSets = new Set(); | ||
const mainSelectionSet = stitchingInfo.mergedTypes[parentTypeName].selectionSets.get(possibleSubschema); | ||
if (mainSelectionSet) { | ||
selectionSets.add(mainSelectionSet); | ||
} | ||
for (const fieldNode of leftOver.unproxiableFieldNodes) { | ||
const fieldName = fieldNode.name.value; | ||
const fieldSelectionSet = stitchingInfo.mergedTypes[parentTypeName].fieldSelectionSets.get(possibleSubschema)?.[fieldName]; | ||
if (fieldSelectionSet) { | ||
selectionSets.add(fieldSelectionSet); | ||
} | ||
} | ||
// Wait until the parent is flattened, then check if non proxiable subschemas are satisfied now, | ||
// then the deferred fields can be resolved | ||
if (selectionSet) { | ||
if (selectionSets.size) { | ||
const selectionSet = { | ||
kind: Kind.SELECTION_SET, | ||
selections: Array.from(selectionSets).flatMap(selectionSet => selectionSet.selections), | ||
}; | ||
const flattenedParent$ = flattenPromise(parent); | ||
if (isPromise(flattenedParent$)) { | ||
flattenedParent$.then(flattenedParent => { | ||
handleFlattenedParent(flattenedParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
handleFlattenedParent(flattenedParent, parent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
}); | ||
} | ||
else { | ||
handleFlattenedParent(flattenedParent$, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
handleFlattenedParent(flattenedParent$, parent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
} | ||
@@ -92,6 +108,7 @@ } | ||
} | ||
function handleFlattenedParent(flattenedParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info) { | ||
function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info) { | ||
// If this subschema is satisfied now, try to resolve the deferred fields | ||
if (parentSatisfiedSelectionSet(flattenedParent, selectionSet)) { | ||
for (const [leftOverParent, missingFieldNodes] of leftOver.missingFieldsParentMap) { | ||
const missingFieldNodes = leftOver.missingFieldsParentMap.get(leftOverParent); | ||
if (missingFieldNodes) { | ||
const resolver = stitchingInfo.mergedTypes[parentTypeName].resolvers.get(possibleSubschema); | ||
@@ -121,2 +138,44 @@ if (resolver) { | ||
} | ||
else { | ||
// try to resolve the missing fields | ||
for (const selectionNode of selectionSet.selections) { | ||
if (selectionNode.kind === Kind.FIELD && selectionNode.selectionSet?.selections?.length) { | ||
const responseKey = selectionNode.alias?.value ?? selectionNode.name.value; | ||
const nestedParent = flattenedParent[responseKey]; | ||
const nestedSelectionSet = selectionNode.selectionSet; | ||
if (nestedParent != null) { | ||
if (!parentSatisfiedSelectionSet(nestedParent, nestedSelectionSet)) { | ||
async function handleNestedParentItem(nestedParentItem, fieldNode) { | ||
const nestedTypeName = nestedParentItem['__typename']; | ||
const sourceSubschema = getSubschema(flattenedParent, responseKey); | ||
if (sourceSubschema && nestedTypeName) { | ||
const delegationPlan = stitchingInfo.mergedTypes[nestedTypeName].delegationPlanBuilder(info.schema, sourceSubschema, info.variableValues, info.fragments, [fieldNode]); | ||
// Later optimize | ||
for (const delegationMap of delegationPlan) { | ||
for (const [subschema, selectionSet] of delegationMap) { | ||
const resolver = stitchingInfo.mergedTypes[nestedTypeName].resolvers.get(subschema); | ||
if (resolver) { | ||
const res = await resolver(nestedParentItem, context, info, subschema, selectionSet, info.parentType, info.parentType); | ||
if (res) { | ||
handleResolverResult(res, subschema, selectionSet, nestedParentItem, (nestedParentItem[FIELD_SUBSCHEMA_MAP_SYMBOL] ||= new Map()), info, responsePathAsArray(info.path), (nestedParentItem[UNPATHED_ERRORS_SYMBOL] ||= [])); | ||
} | ||
} | ||
} | ||
} | ||
if (parentSatisfiedSelectionSet(nestedParent, nestedSelectionSet)) { | ||
handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info); | ||
} | ||
} | ||
} | ||
if (Array.isArray(nestedParent)) { | ||
nestedParent.forEach(nestedParentItem => handleNestedParentItem(nestedParentItem, selectionNode)); | ||
} | ||
else { | ||
handleNestedParentItem(nestedParent, selectionNode); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -224,2 +283,11 @@ function handleDeferredResolverResult(resolverResult, possibleSubschema, selectionSet, leftOverParent, leftOver, context, info) { | ||
} | ||
if (data[OBJECT_SUBSCHEMA_SYMBOL]) { | ||
newData[OBJECT_SUBSCHEMA_SYMBOL] = data[OBJECT_SUBSCHEMA_SYMBOL]; | ||
} | ||
if (data[FIELD_SUBSCHEMA_MAP_SYMBOL]) { | ||
newData[FIELD_SUBSCHEMA_MAP_SYMBOL] = data[FIELD_SUBSCHEMA_MAP_SYMBOL]; | ||
} | ||
if (data[UNPATHED_ERRORS_SYMBOL]) { | ||
newData[UNPATHED_ERRORS_SYMBOL] = data[UNPATHED_ERRORS_SYMBOL]; | ||
} | ||
if (jobs.length) { | ||
@@ -226,0 +294,0 @@ return Promise.all(jobs).then(() => newData); |
@@ -10,5 +10,5 @@ import { GraphQLError, isAbstractType, locatedError, responsePathAsArray, } from 'graphql'; | ||
Object.defineProperties(object, { | ||
[OBJECT_SUBSCHEMA_SYMBOL]: { value: subschema }, | ||
[FIELD_SUBSCHEMA_MAP_SYMBOL]: { value: subschemaMap }, | ||
[UNPATHED_ERRORS_SYMBOL]: { value: errors }, | ||
[OBJECT_SUBSCHEMA_SYMBOL]: { value: subschema, writable: true }, | ||
[FIELD_SUBSCHEMA_MAP_SYMBOL]: { value: subschemaMap, writable: true }, | ||
[UNPATHED_ERRORS_SYMBOL]: { value: errors, writable: true }, | ||
}); | ||
@@ -15,0 +15,0 @@ return object; |
@@ -94,3 +94,3 @@ import { getNullableType, isAbstractType, isCompositeType, isLeafType, isListType, locatedError, } from 'graphql'; | ||
if (unreportedErrors.length === 1) { | ||
return unreportedErrors[0]; | ||
return locatedError(unreportedErrors[0], undefined, unreportedErrors[0].path); | ||
} | ||
@@ -97,0 +97,0 @@ return new AggregateError(unreportedErrors.map(e => |
{ | ||
"name": "@graphql-tools/delegate", | ||
"version": "10.0.19", | ||
"version": "10.0.20-alpha-20240826080044-86bd8494b97dcaaa850bc8500e6c03b3ab9dbc7c", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
241917
4746
2