Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(typesContainSelectionSet): memoize #1921

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
);
});