@graphql-tools/delegate 12.3.1-rc-e3d6a992e62239c25c7e9abe4cf052be9a2213cf → 12.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @graphql-tools/delegate
2
2
 
3
- ## 12.3.1-rc-e3d6a992e62239c25c7e9abe4cf052be9a2213cf
3
+ ## 12.3.1
4
4
  ### Patch Changes
5
5
 
6
6
 
@@ -11,6 +11,18 @@
11
11
 
12
12
  Without `defer: true`, a deferred field owned by another inner subschema under `wrapSchema` over a stitched schema called `defaultMergedResolver` on the nested schema with the same parent and recursed. Re-fetch via the nested merged-type resolver for the field owner instead.
13
13
 
14
+
15
+ - [#2701](https://github.com/graphql-hive/gateway/pull/2701) [`a558eea`](https://github.com/graphql-hive/gateway/commit/a558eeae3c94deb60d40f98232b9d4d82ae9d9a1) Thanks [@ardatan](https://github.com/ardatan)! - Settle unsatisfiable stitched leftover fields instead of hanging
16
+
17
+ When a merged field could only be fetched with a merge key that never
18
+ arrived, leftover resolution returned a deferred promise that never
19
+ settled and the request hung idle. Track leftover work and, once the
20
+ queue is empty, reject unsettled deferreds with `SATISFIABILITY_ERROR`
21
+ (Apollo composition-style message, including selection snippet and
22
+ subschema names when set).
23
+
24
+ Closes [#2695](https://github.com/graphql-hive/gateway/issues/2695)
25
+
14
26
  ## 12.3.0
15
27
  ### Minor Changes
16
28
 
package/dist/index.cjs CHANGED
@@ -3330,6 +3330,13 @@ function defaultMergedResolver(parent, args, context, info) {
3330
3330
  missingDeferredFields.set(responseKey, deferred);
3331
3331
  const stitchingInfo2 = info.schema.extensions?.["stitchingInfo"];
3332
3332
  const parentTypeName2 = parent?.__typename || info.parentType.name;
3333
+ leftOverDeferredContext.set(deferred, {
3334
+ parentTypeName: parentTypeName2,
3335
+ fieldName: info.fieldName,
3336
+ responseKey,
3337
+ path: graphql.responsePathAsArray(info.path),
3338
+ sourceSubschema: parent[OBJECT_SUBSCHEMA_SYMBOL]
3339
+ });
3333
3340
  const fieldNodesByType = stitchingInfo2?.fieldNodesByField?.[parentTypeName2]?.[info.fieldName];
3334
3341
  if (fieldNodesByType?.every((fieldNode) => {
3335
3342
  const key = fieldNode.alias?.value ?? fieldNode.name.value;
@@ -3626,19 +3633,23 @@ function handleResult(parent, responseKey, context, info) {
3626
3633
  );
3627
3634
  const leftOver = getPlanLeftOverFromParent(parent);
3628
3635
  if (leftOver) {
3636
+ const endWork = beginLeftOverWork(leftOver);
3629
3637
  return promiseHelpers.handleMaybePromise(
3630
3638
  () => resolvedData$,
3631
3639
  (resolvedData) => {
3632
3640
  parent[responseKey] = resolvedData;
3633
3641
  handleLeftOver(parent, context, info, leftOver);
3634
3642
  return resolvedData;
3635
- }
3643
+ },
3644
+ void 0,
3645
+ endWork
3636
3646
  );
3637
3647
  }
3638
3648
  return resolvedData$;
3639
3649
  }
3640
3650
  function handleLeftOver(parent, context, info, leftOver) {
3641
3651
  const stitchingInfo = info.schema.extensions?.["stitchingInfo"];
3652
+ let scheduledWork = false;
3642
3653
  if (stitchingInfo) {
3643
3654
  for (const possibleSubschema of leftOver.nonProxiableSubschemas) {
3644
3655
  const parentTypeName = info.parentType.name;
@@ -3659,12 +3670,14 @@ function handleLeftOver(parent, context, info, leftOver) {
3659
3670
  }
3660
3671
  }
3661
3672
  if (selectionSets.size) {
3673
+ scheduledWork = true;
3662
3674
  const selectionSet = {
3663
3675
  kind: graphql.Kind.SELECTION_SET,
3664
3676
  selections: Array.from(selectionSets).flatMap(
3665
3677
  (selectionSet2) => selectionSet2.selections
3666
3678
  )
3667
3679
  };
3680
+ const endWork = beginLeftOverWork(leftOver);
3668
3681
  promiseHelpers.handleMaybePromise(
3669
3682
  () => flattenPromise(parent),
3670
3683
  (flattenedParent) => {
@@ -3679,11 +3692,16 @@ function handleLeftOver(parent, context, info, leftOver) {
3679
3692
  context,
3680
3693
  info
3681
3694
  );
3682
- }
3695
+ },
3696
+ void 0,
3697
+ endWork
3683
3698
  );
3684
3699
  }
3685
3700
  }
3686
3701
  }
3702
+ if (!scheduledWork) {
3703
+ scheduleLeftOverDrain(leftOver);
3704
+ }
3687
3705
  }
3688
3706
  function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info) {
3689
3707
  if (parentSatisfiedSelectionSet(flattenedParent, selectionSet)) {
@@ -3698,6 +3716,7 @@ function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschem
3698
3716
  kind: graphql.Kind.SELECTION_SET,
3699
3717
  selections: missingFieldNodes
3700
3718
  };
3719
+ const endWork = beginLeftOverWork(leftOver);
3701
3720
  promiseHelpers.handleMaybePromise(
3702
3721
  () => resolver(
3703
3722
  leftOverParent,
@@ -3719,7 +3738,8 @@ function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschem
3719
3738
  info
3720
3739
  );
3721
3740
  },
3722
- (error) => handleDeferredResolverFailure(leftOver, leftOverParent, error)
3741
+ (error) => handleDeferredResolverFailure(leftOver, leftOverParent, error),
3742
+ endWork
3723
3743
  );
3724
3744
  }
3725
3745
  }
@@ -3732,70 +3752,78 @@ function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschem
3732
3752
  if (nestedParent != null) {
3733
3753
  if (!parentSatisfiedSelectionSet(nestedParent, nestedSelectionSet)) {
3734
3754
  async function handleNestedParentItem(nestedParentItem, fieldNode) {
3735
- const nestedTypeName = nestedParentItem["__typename"];
3736
- const sourceSubschema = getSubschema(
3737
- flattenedParent,
3738
- responseKey
3739
- );
3740
- if (sourceSubschema && nestedTypeName) {
3741
- const variableValues = getCoercedVariableValues(
3742
- info.variableValues
3755
+ const endWork = beginLeftOverWork(leftOver);
3756
+ try {
3757
+ const nestedTypeName = nestedParentItem["__typename"];
3758
+ const sourceSubschema = getSubschema(
3759
+ flattenedParent,
3760
+ responseKey
3743
3761
  );
3744
- const delegationPlan = stitchingInfo.mergedTypes[nestedTypeName]?.delegationPlanBuilder(
3745
- info.schema,
3746
- sourceSubschema,
3747
- variableValues != null && Object.keys(variableValues).length > 0 ? variableValues : EMPTY_OBJECT,
3748
- info.fragments != null && Object.keys(info.fragments).length > 0 ? info.fragments : EMPTY_OBJECT,
3749
- getActualFieldNodes(fieldNode),
3750
- context,
3751
- info
3752
- );
3753
- if (delegationPlan?.length) {
3754
- for (const delegationMap of delegationPlan) {
3755
- for (const [subschema, selectionSet2] of delegationMap) {
3756
- const resolver = stitchingInfo.mergedTypes[nestedTypeName]?.resolvers.get(subschema);
3757
- if (resolver) {
3758
- const subschemaTypes = stitchingInfo.mergedTypes[nestedTypeName].typeMaps.get(subschema);
3759
- const returnType = subschemaTypes[nestedTypeName];
3760
- const res = await resolver(
3761
- nestedParentItem,
3762
- context,
3763
- info,
3764
- subschema,
3765
- selectionSet2,
3766
- returnType,
3767
- // returnType
3768
- info.parentType
3769
- );
3770
- if (res) {
3771
- handleResolverResult(
3772
- res,
3773
- subschema,
3774
- selectionSet2,
3762
+ if (sourceSubschema && nestedTypeName) {
3763
+ const variableValues = getCoercedVariableValues(
3764
+ info.variableValues
3765
+ );
3766
+ const delegationPlan = stitchingInfo.mergedTypes[nestedTypeName]?.delegationPlanBuilder(
3767
+ info.schema,
3768
+ sourceSubschema,
3769
+ variableValues != null && Object.keys(variableValues).length > 0 ? variableValues : EMPTY_OBJECT,
3770
+ info.fragments != null && Object.keys(info.fragments).length > 0 ? info.fragments : EMPTY_OBJECT,
3771
+ getActualFieldNodes(fieldNode),
3772
+ context,
3773
+ info
3774
+ );
3775
+ if (delegationPlan?.length) {
3776
+ for (const delegationMap of delegationPlan) {
3777
+ for (const [subschema, selectionSet2] of delegationMap) {
3778
+ const resolver = stitchingInfo.mergedTypes[nestedTypeName]?.resolvers.get(subschema);
3779
+ if (resolver) {
3780
+ const subschemaTypes = stitchingInfo.mergedTypes[nestedTypeName].typeMaps.get(subschema);
3781
+ const returnType = subschemaTypes[nestedTypeName];
3782
+ const res = await resolver(
3775
3783
  nestedParentItem,
3776
- nestedParentItem[FIELD_SUBSCHEMA_MAP_SYMBOL] ||= /* @__PURE__ */ new Map(),
3784
+ context,
3777
3785
  info,
3778
- graphql.responsePathAsArray(info.path),
3779
- nestedParentItem[UNPATHED_ERRORS_SYMBOL] ||= []
3786
+ subschema,
3787
+ selectionSet2,
3788
+ returnType,
3789
+ // returnType
3790
+ info.parentType
3780
3791
  );
3792
+ if (res) {
3793
+ handleResolverResult(
3794
+ res,
3795
+ subschema,
3796
+ selectionSet2,
3797
+ nestedParentItem,
3798
+ nestedParentItem[FIELD_SUBSCHEMA_MAP_SYMBOL] ||= /* @__PURE__ */ new Map(),
3799
+ info,
3800
+ graphql.responsePathAsArray(info.path),
3801
+ nestedParentItem[UNPATHED_ERRORS_SYMBOL] ||= []
3802
+ );
3803
+ }
3781
3804
  }
3782
3805
  }
3783
3806
  }
3784
3807
  }
3808
+ if (parentSatisfiedSelectionSet(
3809
+ nestedParent,
3810
+ nestedSelectionSet
3811
+ )) {
3812
+ handleFlattenedParent(
3813
+ flattenedParent,
3814
+ leftOverParent,
3815
+ possibleSubschema,
3816
+ selectionSet,
3817
+ leftOver,
3818
+ stitchingInfo,
3819
+ parentTypeName,
3820
+ context,
3821
+ info
3822
+ );
3823
+ }
3785
3824
  }
3786
- if (parentSatisfiedSelectionSet(nestedParent, nestedSelectionSet)) {
3787
- handleFlattenedParent(
3788
- flattenedParent,
3789
- leftOverParent,
3790
- possibleSubschema,
3791
- selectionSet,
3792
- leftOver,
3793
- stitchingInfo,
3794
- parentTypeName,
3795
- context,
3796
- info
3797
- );
3798
- }
3825
+ } finally {
3826
+ endWork();
3799
3827
  }
3800
3828
  }
3801
3829
  if (Array.isArray(nestedParent)) {
@@ -3843,8 +3871,13 @@ function handleDeferredResolverResult(resolverResult, possibleSubschema, selecti
3843
3871
  requiredKey
3844
3872
  );
3845
3873
  })) {
3874
+ const endWork = beginLeftOverWork(leftOver);
3846
3875
  setTimeout(() => {
3847
- handleLeftOver(leftOverParent, context, info, leftOver);
3876
+ try {
3877
+ handleLeftOver(leftOverParent, context, info, leftOver);
3878
+ } finally {
3879
+ endWork();
3880
+ }
3848
3881
  }, 0);
3849
3882
  }
3850
3883
  }
@@ -3968,6 +4001,126 @@ function flattenPromise(data) {
3968
4001
  }
3969
4002
  return data;
3970
4003
  }
4004
+ const leftOverWorkState = /* @__PURE__ */ new WeakMap();
4005
+ function getLeftOverWorkState(leftOver) {
4006
+ let state = leftOverWorkState.get(leftOver);
4007
+ if (!state) {
4008
+ state = { pending: 0, drainScheduled: false };
4009
+ leftOverWorkState.set(leftOver, state);
4010
+ }
4011
+ return state;
4012
+ }
4013
+ function beginLeftOverWork(leftOver) {
4014
+ const state = getLeftOverWorkState(leftOver);
4015
+ state.pending++;
4016
+ let ended = false;
4017
+ return () => {
4018
+ if (ended) {
4019
+ return;
4020
+ }
4021
+ ended = true;
4022
+ state.pending--;
4023
+ scheduleLeftOverDrain(leftOver);
4024
+ };
4025
+ }
4026
+ function scheduleLeftOverDrain(leftOver) {
4027
+ const state = getLeftOverWorkState(leftOver);
4028
+ if (state.drainScheduled) {
4029
+ return;
4030
+ }
4031
+ state.drainScheduled = true;
4032
+ queueMicrotask(() => {
4033
+ state.drainScheduled = false;
4034
+ if (state.pending > 0) {
4035
+ return;
4036
+ }
4037
+ rejectOrphanedLeftOverDeferreds(leftOver);
4038
+ });
4039
+ }
4040
+ function rejectOrphanedLeftOverDeferreds(leftOver) {
4041
+ for (const [
4042
+ parent,
4043
+ deferredFields
4044
+ ] of leftOver.missingFieldsParentDeferredMap) {
4045
+ if (!deferredFields.size) {
4046
+ leftOver.missingFieldsParentDeferredMap.delete(parent);
4047
+ continue;
4048
+ }
4049
+ for (const [responseKey, deferred] of deferredFields) {
4050
+ const ctx = leftOverDeferredContext.get(deferred);
4051
+ deferred.reject(
4052
+ new graphql.GraphQLError(
4053
+ buildSatisfiabilityErrorMessage(leftOver, responseKey, ctx, parent),
4054
+ {
4055
+ extensions: {
4056
+ code: "SATISFIABILITY_ERROR"
4057
+ }
4058
+ }
4059
+ )
4060
+ );
4061
+ leftOverDeferredContext.delete(deferred);
4062
+ }
4063
+ deferredFields.clear();
4064
+ leftOver.missingFieldsParentDeferredMap.delete(parent);
4065
+ }
4066
+ }
4067
+ const leftOverDeferredContext = /* @__PURE__ */ new WeakMap();
4068
+ function formatSubschemaLabel(subschema, fallback) {
4069
+ const name = subschema?.name;
4070
+ if (name) {
4071
+ return `subschema "${name}"`;
4072
+ }
4073
+ return fallback;
4074
+ }
4075
+ function printPathAsQuerySelection(path) {
4076
+ const fields = path.filter((segment) => {
4077
+ return typeof segment === "string";
4078
+ });
4079
+ if (fields.length === 0) {
4080
+ return "{\n}";
4081
+ }
4082
+ const lines = ["{"];
4083
+ for (let i = 0; i < fields.length; i++) {
4084
+ const pad = " ".repeat(i + 1);
4085
+ if (i === fields.length - 1) {
4086
+ lines.push(`${pad}${fields[i]}`);
4087
+ } else {
4088
+ lines.push(`${pad}${fields[i]} {`);
4089
+ }
4090
+ }
4091
+ for (let i = fields.length - 1; i >= 1; i--) {
4092
+ lines.push(`${" ".repeat(i)}}`);
4093
+ }
4094
+ lines.push("}");
4095
+ return lines.join("\n");
4096
+ }
4097
+ function buildSatisfiabilityErrorMessage(leftOver, responseKey, ctx, parent) {
4098
+ const parentTypeName = ctx?.parentTypeName || (typeof parent?.__typename === "string" ? parent.__typename : "Object");
4099
+ const fieldName = ctx?.fieldName || responseKey;
4100
+ const qualifiedField = `${parentTypeName}.${fieldName}`;
4101
+ const path = ctx?.path?.length ? ctx.path : [responseKey];
4102
+ const selection = printPathAsQuerySelection(path);
4103
+ const sourceSubschema = ctx?.sourceSubschema || parent[OBJECT_SUBSCHEMA_SYMBOL];
4104
+ const sourceLabel = formatSubschemaLabel(
4105
+ sourceSubschema,
4106
+ "the current subschema"
4107
+ );
4108
+ const reasons = [`- cannot find field "${qualifiedField}".`];
4109
+ for (const target of leftOver.nonProxiableSubschemas) {
4110
+ const targetLabel = formatSubschemaLabel(target, "another subschema");
4111
+ const mergeConfig = target.merge?.[parentTypeName];
4112
+ const keyFields = mergeConfig?.selectionSet?.trim();
4113
+ const keyPhrase = keyFields ? `using key fields ${keyFields} of "${parentTypeName}"` : `using key fields of "${parentTypeName}"`;
4114
+ reasons.push(
4115
+ `- cannot move to ${targetLabel} ${keyPhrase}; the key field(s) cannot be resolved from ${sourceLabel}.`
4116
+ );
4117
+ }
4118
+ return `The following field cannot be satisfied by the subschemas:
4119
+ ${selection}
4120
+ because:
4121
+ - from ${sourceLabel}:
4122
+ ` + reasons.map((reason) => ` ${reason}`).join("\n");
4123
+ }
3971
4124
 
3972
4125
  function isSubschemaConfig(value) {
3973
4126
  return Boolean(value?.schema);
package/dist/index.js CHANGED
@@ -3330,6 +3330,13 @@ function defaultMergedResolver(parent, args, context, info) {
3330
3330
  missingDeferredFields.set(responseKey, deferred);
3331
3331
  const stitchingInfo2 = info.schema.extensions?.["stitchingInfo"];
3332
3332
  const parentTypeName2 = parent?.__typename || info.parentType.name;
3333
+ leftOverDeferredContext.set(deferred, {
3334
+ parentTypeName: parentTypeName2,
3335
+ fieldName: info.fieldName,
3336
+ responseKey,
3337
+ path: responsePathAsArray(info.path),
3338
+ sourceSubschema: parent[OBJECT_SUBSCHEMA_SYMBOL]
3339
+ });
3333
3340
  const fieldNodesByType = stitchingInfo2?.fieldNodesByField?.[parentTypeName2]?.[info.fieldName];
3334
3341
  if (fieldNodesByType?.every((fieldNode) => {
3335
3342
  const key = fieldNode.alias?.value ?? fieldNode.name.value;
@@ -3626,19 +3633,23 @@ function handleResult(parent, responseKey, context, info) {
3626
3633
  );
3627
3634
  const leftOver = getPlanLeftOverFromParent(parent);
3628
3635
  if (leftOver) {
3636
+ const endWork = beginLeftOverWork(leftOver);
3629
3637
  return handleMaybePromise(
3630
3638
  () => resolvedData$,
3631
3639
  (resolvedData) => {
3632
3640
  parent[responseKey] = resolvedData;
3633
3641
  handleLeftOver(parent, context, info, leftOver);
3634
3642
  return resolvedData;
3635
- }
3643
+ },
3644
+ void 0,
3645
+ endWork
3636
3646
  );
3637
3647
  }
3638
3648
  return resolvedData$;
3639
3649
  }
3640
3650
  function handleLeftOver(parent, context, info, leftOver) {
3641
3651
  const stitchingInfo = info.schema.extensions?.["stitchingInfo"];
3652
+ let scheduledWork = false;
3642
3653
  if (stitchingInfo) {
3643
3654
  for (const possibleSubschema of leftOver.nonProxiableSubschemas) {
3644
3655
  const parentTypeName = info.parentType.name;
@@ -3659,12 +3670,14 @@ function handleLeftOver(parent, context, info, leftOver) {
3659
3670
  }
3660
3671
  }
3661
3672
  if (selectionSets.size) {
3673
+ scheduledWork = true;
3662
3674
  const selectionSet = {
3663
3675
  kind: Kind.SELECTION_SET,
3664
3676
  selections: Array.from(selectionSets).flatMap(
3665
3677
  (selectionSet2) => selectionSet2.selections
3666
3678
  )
3667
3679
  };
3680
+ const endWork = beginLeftOverWork(leftOver);
3668
3681
  handleMaybePromise(
3669
3682
  () => flattenPromise(parent),
3670
3683
  (flattenedParent) => {
@@ -3679,11 +3692,16 @@ function handleLeftOver(parent, context, info, leftOver) {
3679
3692
  context,
3680
3693
  info
3681
3694
  );
3682
- }
3695
+ },
3696
+ void 0,
3697
+ endWork
3683
3698
  );
3684
3699
  }
3685
3700
  }
3686
3701
  }
3702
+ if (!scheduledWork) {
3703
+ scheduleLeftOverDrain(leftOver);
3704
+ }
3687
3705
  }
3688
3706
  function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschema, selectionSet, leftOver, stitchingInfo, parentTypeName, context, info) {
3689
3707
  if (parentSatisfiedSelectionSet(flattenedParent, selectionSet)) {
@@ -3698,6 +3716,7 @@ function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschem
3698
3716
  kind: Kind.SELECTION_SET,
3699
3717
  selections: missingFieldNodes
3700
3718
  };
3719
+ const endWork = beginLeftOverWork(leftOver);
3701
3720
  handleMaybePromise(
3702
3721
  () => resolver(
3703
3722
  leftOverParent,
@@ -3719,7 +3738,8 @@ function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschem
3719
3738
  info
3720
3739
  );
3721
3740
  },
3722
- (error) => handleDeferredResolverFailure(leftOver, leftOverParent, error)
3741
+ (error) => handleDeferredResolverFailure(leftOver, leftOverParent, error),
3742
+ endWork
3723
3743
  );
3724
3744
  }
3725
3745
  }
@@ -3732,70 +3752,78 @@ function handleFlattenedParent(flattenedParent, leftOverParent, possibleSubschem
3732
3752
  if (nestedParent != null) {
3733
3753
  if (!parentSatisfiedSelectionSet(nestedParent, nestedSelectionSet)) {
3734
3754
  async function handleNestedParentItem(nestedParentItem, fieldNode) {
3735
- const nestedTypeName = nestedParentItem["__typename"];
3736
- const sourceSubschema = getSubschema(
3737
- flattenedParent,
3738
- responseKey
3739
- );
3740
- if (sourceSubschema && nestedTypeName) {
3741
- const variableValues = getCoercedVariableValues(
3742
- info.variableValues
3755
+ const endWork = beginLeftOverWork(leftOver);
3756
+ try {
3757
+ const nestedTypeName = nestedParentItem["__typename"];
3758
+ const sourceSubschema = getSubschema(
3759
+ flattenedParent,
3760
+ responseKey
3743
3761
  );
3744
- const delegationPlan = stitchingInfo.mergedTypes[nestedTypeName]?.delegationPlanBuilder(
3745
- info.schema,
3746
- sourceSubschema,
3747
- variableValues != null && Object.keys(variableValues).length > 0 ? variableValues : EMPTY_OBJECT,
3748
- info.fragments != null && Object.keys(info.fragments).length > 0 ? info.fragments : EMPTY_OBJECT,
3749
- getActualFieldNodes(fieldNode),
3750
- context,
3751
- info
3752
- );
3753
- if (delegationPlan?.length) {
3754
- for (const delegationMap of delegationPlan) {
3755
- for (const [subschema, selectionSet2] of delegationMap) {
3756
- const resolver = stitchingInfo.mergedTypes[nestedTypeName]?.resolvers.get(subschema);
3757
- if (resolver) {
3758
- const subschemaTypes = stitchingInfo.mergedTypes[nestedTypeName].typeMaps.get(subschema);
3759
- const returnType = subschemaTypes[nestedTypeName];
3760
- const res = await resolver(
3761
- nestedParentItem,
3762
- context,
3763
- info,
3764
- subschema,
3765
- selectionSet2,
3766
- returnType,
3767
- // returnType
3768
- info.parentType
3769
- );
3770
- if (res) {
3771
- handleResolverResult(
3772
- res,
3773
- subschema,
3774
- selectionSet2,
3762
+ if (sourceSubschema && nestedTypeName) {
3763
+ const variableValues = getCoercedVariableValues(
3764
+ info.variableValues
3765
+ );
3766
+ const delegationPlan = stitchingInfo.mergedTypes[nestedTypeName]?.delegationPlanBuilder(
3767
+ info.schema,
3768
+ sourceSubschema,
3769
+ variableValues != null && Object.keys(variableValues).length > 0 ? variableValues : EMPTY_OBJECT,
3770
+ info.fragments != null && Object.keys(info.fragments).length > 0 ? info.fragments : EMPTY_OBJECT,
3771
+ getActualFieldNodes(fieldNode),
3772
+ context,
3773
+ info
3774
+ );
3775
+ if (delegationPlan?.length) {
3776
+ for (const delegationMap of delegationPlan) {
3777
+ for (const [subschema, selectionSet2] of delegationMap) {
3778
+ const resolver = stitchingInfo.mergedTypes[nestedTypeName]?.resolvers.get(subschema);
3779
+ if (resolver) {
3780
+ const subschemaTypes = stitchingInfo.mergedTypes[nestedTypeName].typeMaps.get(subschema);
3781
+ const returnType = subschemaTypes[nestedTypeName];
3782
+ const res = await resolver(
3775
3783
  nestedParentItem,
3776
- nestedParentItem[FIELD_SUBSCHEMA_MAP_SYMBOL] ||= /* @__PURE__ */ new Map(),
3784
+ context,
3777
3785
  info,
3778
- responsePathAsArray(info.path),
3779
- nestedParentItem[UNPATHED_ERRORS_SYMBOL] ||= []
3786
+ subschema,
3787
+ selectionSet2,
3788
+ returnType,
3789
+ // returnType
3790
+ info.parentType
3780
3791
  );
3792
+ if (res) {
3793
+ handleResolverResult(
3794
+ res,
3795
+ subschema,
3796
+ selectionSet2,
3797
+ nestedParentItem,
3798
+ nestedParentItem[FIELD_SUBSCHEMA_MAP_SYMBOL] ||= /* @__PURE__ */ new Map(),
3799
+ info,
3800
+ responsePathAsArray(info.path),
3801
+ nestedParentItem[UNPATHED_ERRORS_SYMBOL] ||= []
3802
+ );
3803
+ }
3781
3804
  }
3782
3805
  }
3783
3806
  }
3784
3807
  }
3808
+ if (parentSatisfiedSelectionSet(
3809
+ nestedParent,
3810
+ nestedSelectionSet
3811
+ )) {
3812
+ handleFlattenedParent(
3813
+ flattenedParent,
3814
+ leftOverParent,
3815
+ possibleSubschema,
3816
+ selectionSet,
3817
+ leftOver,
3818
+ stitchingInfo,
3819
+ parentTypeName,
3820
+ context,
3821
+ info
3822
+ );
3823
+ }
3785
3824
  }
3786
- if (parentSatisfiedSelectionSet(nestedParent, nestedSelectionSet)) {
3787
- handleFlattenedParent(
3788
- flattenedParent,
3789
- leftOverParent,
3790
- possibleSubschema,
3791
- selectionSet,
3792
- leftOver,
3793
- stitchingInfo,
3794
- parentTypeName,
3795
- context,
3796
- info
3797
- );
3798
- }
3825
+ } finally {
3826
+ endWork();
3799
3827
  }
3800
3828
  }
3801
3829
  if (Array.isArray(nestedParent)) {
@@ -3843,8 +3871,13 @@ function handleDeferredResolverResult(resolverResult, possibleSubschema, selecti
3843
3871
  requiredKey
3844
3872
  );
3845
3873
  })) {
3874
+ const endWork = beginLeftOverWork(leftOver);
3846
3875
  setTimeout(() => {
3847
- handleLeftOver(leftOverParent, context, info, leftOver);
3876
+ try {
3877
+ handleLeftOver(leftOverParent, context, info, leftOver);
3878
+ } finally {
3879
+ endWork();
3880
+ }
3848
3881
  }, 0);
3849
3882
  }
3850
3883
  }
@@ -3968,6 +4001,126 @@ function flattenPromise(data) {
3968
4001
  }
3969
4002
  return data;
3970
4003
  }
4004
+ const leftOverWorkState = /* @__PURE__ */ new WeakMap();
4005
+ function getLeftOverWorkState(leftOver) {
4006
+ let state = leftOverWorkState.get(leftOver);
4007
+ if (!state) {
4008
+ state = { pending: 0, drainScheduled: false };
4009
+ leftOverWorkState.set(leftOver, state);
4010
+ }
4011
+ return state;
4012
+ }
4013
+ function beginLeftOverWork(leftOver) {
4014
+ const state = getLeftOverWorkState(leftOver);
4015
+ state.pending++;
4016
+ let ended = false;
4017
+ return () => {
4018
+ if (ended) {
4019
+ return;
4020
+ }
4021
+ ended = true;
4022
+ state.pending--;
4023
+ scheduleLeftOverDrain(leftOver);
4024
+ };
4025
+ }
4026
+ function scheduleLeftOverDrain(leftOver) {
4027
+ const state = getLeftOverWorkState(leftOver);
4028
+ if (state.drainScheduled) {
4029
+ return;
4030
+ }
4031
+ state.drainScheduled = true;
4032
+ queueMicrotask(() => {
4033
+ state.drainScheduled = false;
4034
+ if (state.pending > 0) {
4035
+ return;
4036
+ }
4037
+ rejectOrphanedLeftOverDeferreds(leftOver);
4038
+ });
4039
+ }
4040
+ function rejectOrphanedLeftOverDeferreds(leftOver) {
4041
+ for (const [
4042
+ parent,
4043
+ deferredFields
4044
+ ] of leftOver.missingFieldsParentDeferredMap) {
4045
+ if (!deferredFields.size) {
4046
+ leftOver.missingFieldsParentDeferredMap.delete(parent);
4047
+ continue;
4048
+ }
4049
+ for (const [responseKey, deferred] of deferredFields) {
4050
+ const ctx = leftOverDeferredContext.get(deferred);
4051
+ deferred.reject(
4052
+ new GraphQLError(
4053
+ buildSatisfiabilityErrorMessage(leftOver, responseKey, ctx, parent),
4054
+ {
4055
+ extensions: {
4056
+ code: "SATISFIABILITY_ERROR"
4057
+ }
4058
+ }
4059
+ )
4060
+ );
4061
+ leftOverDeferredContext.delete(deferred);
4062
+ }
4063
+ deferredFields.clear();
4064
+ leftOver.missingFieldsParentDeferredMap.delete(parent);
4065
+ }
4066
+ }
4067
+ const leftOverDeferredContext = /* @__PURE__ */ new WeakMap();
4068
+ function formatSubschemaLabel(subschema, fallback) {
4069
+ const name = subschema?.name;
4070
+ if (name) {
4071
+ return `subschema "${name}"`;
4072
+ }
4073
+ return fallback;
4074
+ }
4075
+ function printPathAsQuerySelection(path) {
4076
+ const fields = path.filter((segment) => {
4077
+ return typeof segment === "string";
4078
+ });
4079
+ if (fields.length === 0) {
4080
+ return "{\n}";
4081
+ }
4082
+ const lines = ["{"];
4083
+ for (let i = 0; i < fields.length; i++) {
4084
+ const pad = " ".repeat(i + 1);
4085
+ if (i === fields.length - 1) {
4086
+ lines.push(`${pad}${fields[i]}`);
4087
+ } else {
4088
+ lines.push(`${pad}${fields[i]} {`);
4089
+ }
4090
+ }
4091
+ for (let i = fields.length - 1; i >= 1; i--) {
4092
+ lines.push(`${" ".repeat(i)}}`);
4093
+ }
4094
+ lines.push("}");
4095
+ return lines.join("\n");
4096
+ }
4097
+ function buildSatisfiabilityErrorMessage(leftOver, responseKey, ctx, parent) {
4098
+ const parentTypeName = ctx?.parentTypeName || (typeof parent?.__typename === "string" ? parent.__typename : "Object");
4099
+ const fieldName = ctx?.fieldName || responseKey;
4100
+ const qualifiedField = `${parentTypeName}.${fieldName}`;
4101
+ const path = ctx?.path?.length ? ctx.path : [responseKey];
4102
+ const selection = printPathAsQuerySelection(path);
4103
+ const sourceSubschema = ctx?.sourceSubschema || parent[OBJECT_SUBSCHEMA_SYMBOL];
4104
+ const sourceLabel = formatSubschemaLabel(
4105
+ sourceSubschema,
4106
+ "the current subschema"
4107
+ );
4108
+ const reasons = [`- cannot find field "${qualifiedField}".`];
4109
+ for (const target of leftOver.nonProxiableSubschemas) {
4110
+ const targetLabel = formatSubschemaLabel(target, "another subschema");
4111
+ const mergeConfig = target.merge?.[parentTypeName];
4112
+ const keyFields = mergeConfig?.selectionSet?.trim();
4113
+ const keyPhrase = keyFields ? `using key fields ${keyFields} of "${parentTypeName}"` : `using key fields of "${parentTypeName}"`;
4114
+ reasons.push(
4115
+ `- cannot move to ${targetLabel} ${keyPhrase}; the key field(s) cannot be resolved from ${sourceLabel}.`
4116
+ );
4117
+ }
4118
+ return `The following field cannot be satisfied by the subschemas:
4119
+ ${selection}
4120
+ because:
4121
+ - from ${sourceLabel}:
4122
+ ` + reasons.map((reason) => ` ${reason}`).join("\n");
4123
+ }
3971
4124
 
3972
4125
  function isSubschemaConfig(value) {
3973
4126
  return Boolean(value?.schema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/delegate",
3
- "version": "12.3.1-rc-e3d6a992e62239c25c7e9abe4cf052be9a2213cf",
3
+ "version": "12.3.1",
4
4
  "type": "module",
5
5
  "description": "A set of utils for faster development of GraphQL tools",
6
6
  "repository": {