Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Aug 10, 2020
1 parent 238ffc2 commit d1b26de
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
7 changes: 0 additions & 7 deletions packages/batch-delegate/tests/typeMerging.example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,6 @@ describe('merging using type merging', () => {
}
}],
mergeTypes: true,
resolvers: {
Product: {
shippingEstimate: {
selectionSet: '{ weight price }'
}
}
}
});

test('can stitch from products to inventory schema', async () => {
Expand Down
69 changes: 47 additions & 22 deletions packages/stitch/src/stitchingInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,45 @@ export function createStitchingInfo(
mergeTypes?: boolean | Array<string> | MergeTypeFilter
): StitchingInfo {
const mergedTypes = createMergedTypes(typeCandidates, mergeTypes);
const selectionSetsByField: Record<string, Record<string, SelectionSetNode>> = Object.entries(mergedTypes).reduce(
(acc, [typeName, mergedTypeInfo]) => {
if (mergedTypeInfo.selectionSets == null) {
return;
}
const selectionSetsByField: Record<string, Record<string, SelectionSetNode>> = Object.create(null);

acc[typeName] = Object.create(null);
Object.entries(mergedTypes).forEach(([typeName, mergedTypeInfo]) => {
if (mergedTypeInfo.selectionSets == null && mergedTypeInfo.fieldSelectionSets == null) {
return;
}

mergedTypeInfo.selectionSets.forEach((selectionSet, subschemaConfig) => {
const schema = subschemaConfig.schema;
const fields = (schema.getType(typeName) as GraphQLObjectType | GraphQLInterfaceType).getFields();
Object.keys(fields).forEach(fieldName => {
if (acc[typeName][fieldName] == null) {
acc[typeName][fieldName] = {
kind: Kind.SELECTION_SET,
selections: [parseSelectionSet('{ __typename }').selections[0]],
};
}
acc[typeName][fieldName].selections = acc[typeName][fieldName].selections.concat(selectionSet.selections);
});
selectionSetsByField[typeName] = Object.create(null);

mergedTypeInfo.selectionSets.forEach((selectionSet, subschemaConfig) => {
const schema = subschemaConfig.schema;
const fields = (schema.getType(typeName) as GraphQLObjectType | GraphQLInterfaceType).getFields();
Object.keys(fields).forEach(fieldName => {
if (selectionSetsByField[typeName][fieldName] == null) {
selectionSetsByField[typeName][fieldName] = {
kind: Kind.SELECTION_SET,
selections: [parseSelectionSet('{ __typename }').selections[0]],
};
}
selectionSetsByField[typeName][fieldName].selections = selectionSetsByField[typeName][
fieldName
].selections.concat(selectionSet.selections);
});
return acc;
},
Object.create(null)
);
});

mergedTypeInfo.fieldSelectionSets.forEach(selectionSetFieldMap => {
Object.keys(selectionSetFieldMap).forEach(fieldName => {
if (selectionSetsByField[typeName][fieldName] == null) {
selectionSetsByField[typeName][fieldName] = {
kind: Kind.SELECTION_SET,
selections: [parseSelectionSet('{ __typename }').selections[0]],
};
}
selectionSetsByField[typeName][fieldName].selections = selectionSetsByField[typeName][
fieldName
].selections.concat(selectionSetFieldMap[fieldName].selections);
});
});
});

return {
transformedSchemas,
Expand Down Expand Up @@ -122,6 +136,17 @@ function createMergedTypes(
selectionSets.set(subschema, selectionSet);
}

if (mergedTypeConfig.fields) {
const parsedFieldSelectionSets = Object.create(null);
Object.keys(mergedTypeConfig.fields).forEach(fieldName => {
if (mergedTypeConfig.fields[fieldName].selectionSet) {
const rawFieldSelectionSet = mergedTypeConfig.fields[fieldName].selectionSet;
parsedFieldSelectionSets[fieldName] = parseSelectionSet(rawFieldSelectionSet);
}
});
fieldSelectionSets.set(subschema, parsedFieldSelectionSets);
}

if (mergedTypeConfig.resolve != null) {
targetSubschemas.push(subschema);
} else if (mergedTypeConfig.key != null) {
Expand Down

0 comments on commit d1b26de

Please sign in to comment.