Skip to content

Commit

Permalink
refactor(typesContainSelectionSet): memoize (#1921)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Aug 19, 2020
1 parent efedf1a commit 957c754
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions packages/delegate/src/results/mergeFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,21 @@ const sortSubschemasByProxiability = memoize4(function (
const proxiableSubschemas: Array<SubschemaConfig> = [];
const nonProxiableSubschemas: Array<SubschemaConfig> = [];

const sourceSubschemas = Array.isArray(sourceSubschemaOrSourceSubschemas)
? sourceSubschemaOrSourceSubschemas
: [sourceSubschemaOrSourceSubschemas];

const typeName = mergedTypeInfo.typeName;
const types = sourceSubschemas.map(sourceSubschema => sourceSubschema.schema.getType(typeName) as GraphQLObjectType);

targetSubschemas.forEach(t => {
const selectionSet = mergedTypeInfo.selectionSets.get(t);
const fieldSelectionSets = mergedTypeInfo.fieldSelectionSets.get(t);
if (!typesContainSelectionSet(types, selectionSet)) {
if (!subschemaTypesContainSelectionSet(mergedTypeInfo, sourceSubschemaOrSourceSubschemas, selectionSet)) {
nonProxiableSubschemas.push(t);
} else if (fieldSelectionSets == null) {
proxiableSubschemas.push(t);
} else if (
fieldNodes.every(fieldNode => {
const fieldName = fieldNode.name.value;
const fieldSelectionSet = fieldSelectionSets[fieldName];
return fieldSelectionSet == null || typesContainSelectionSet(types, fieldSelectionSet);
return (
fieldSelectionSet == null ||
subschemaTypesContainSelectionSet(mergedTypeInfo, sourceSubschemaOrSourceSubschemas, fieldSelectionSet)
);
})
) {
proxiableSubschemas.push(t);
Expand Down Expand Up @@ -203,3 +199,23 @@ export function mergeFields(
info
);
}

const subschemaTypesContainSelectionSet = memoize3(function (
mergedTypeInfo: MergedTypeInfo,
sourceSubschemaOrSourceSubschemas: SubschemaConfig | Array<SubschemaConfig>,
selectionSet: SelectionSetNode
) {
if (Array.isArray(sourceSubschemaOrSourceSubschemas)) {
return typesContainSelectionSet(
sourceSubschemaOrSourceSubschemas.map(
sourceSubschema => sourceSubschema.schema.getType(mergedTypeInfo.typeName) as GraphQLObjectType
),
selectionSet
);
}

return typesContainSelectionSet(
[sourceSubschemaOrSourceSubschemas.schema.getType(mergedTypeInfo.typeName) as GraphQLObjectType],
selectionSet
);
});

0 comments on commit 957c754

Please sign in to comment.