Skip to content

Commit

Permalink
Fixes for complex type merging (#1699)
Browse files Browse the repository at this point in the history
= Fix cases where keys differ across subschemas
= Fix multiple rounds of type merging

Closes #1697.

Thanks to @flux627 for reporting.

Includes:
* refactor tests
* Shallow merge the field subschema map
* Add required fields for keys
* Add tests
  • Loading branch information
yaacovCR committed Jun 28, 2020
1 parent 0f8be46 commit b8815ad
Show file tree
Hide file tree
Showing 4 changed files with 543 additions and 121 deletions.
2 changes: 1 addition & 1 deletion packages/delegate/src/proxiedResult.ts
Expand Up @@ -79,7 +79,7 @@ export function mergeProxiedResults(target: any, ...sources: Array<any>): any {

const result = results.reduce(mergeDeep, target);
result[FIELD_SUBSCHEMA_MAP_SYMBOL] = target[FIELD_SUBSCHEMA_MAP_SYMBOL]
? mergeDeep(target[FIELD_SUBSCHEMA_MAP_SYMBOL], fieldSubschemaMap)
? Object.assign({}, target[FIELD_SUBSCHEMA_MAP_SYMBOL], fieldSubschemaMap)
: fieldSubschemaMap;

const errors = sources.map((source: any) => (source instanceof Error ? source : source[ERROR_SYMBOL]));
Expand Down
19 changes: 16 additions & 3 deletions packages/delegate/src/results/handleObject.ts
Expand Up @@ -87,15 +87,28 @@ function collectSubFields(info: GraphQLResolveInfo, typeName: string): Record<st
);
});

const selectionSetsByField = (info.schema.extensions.stitchingInfo as StitchingInfo).selectionSetsByField;
const stitchingInfo = info.schema.extensions.stitchingInfo as StitchingInfo;
const selectionSetsByType = stitchingInfo.selectionSetsByType;
const selectionSetsByField = stitchingInfo.selectionSetsByField;

Object.keys(subFieldNodes).forEach(responseName => {
const fieldName = subFieldNodes[responseName][0].name.value;
if (selectionSetsByField[typeName] && selectionSetsByField[typeName][fieldName]) {
const typeSelectionSet = selectionSetsByType[typeName];
if (typeSelectionSet != null) {
subFieldNodes = collectFields(
partialExecutionContext,
type,
selectionSetsByField[typeName][fieldName],
typeSelectionSet,
subFieldNodes,
visitedFragmentNames
);
}
const fieldSelectionSet = selectionSetsByField?.[typeName]?.[fieldName];
if (fieldSelectionSet != null) {
subFieldNodes = collectFields(
partialExecutionContext,
type,
fieldSelectionSet,
subFieldNodes,
visitedFragmentNames
);
Expand Down

0 comments on commit b8815ad

Please sign in to comment.