Skip to content

Commit

Permalink
abstract into function
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jun 19, 2024
1 parent 54e1e06 commit d4c5449
Showing 1 changed file with 60 additions and 69 deletions.
129 changes: 60 additions & 69 deletions src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,26 +207,15 @@ function findConflictsWithinSelectionSet(
discoveredFragments,
);

// (E) Then collect any conflicts between the provided collection of fields
// and any fragment names found in the given fragment.
while (discoveredFragments.length !== 0) {
const item = discoveredFragments.pop();
if (!item || comparedFragmentPairs.has(item[1], item[0], false)) {
continue;
}
const [fragmentName, referencedFragmentName] = item;
comparedFragmentPairs.add(referencedFragmentName, fragmentName, false);
collectConflictsBetweenFieldsAndFragment(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
false,
fieldMap,
referencedFragmentName,
discoveredFragments,
);
}
processDiscoveredFragments(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
false,
fieldMap,
discoveredFragments,
);
// (C) Then compare this fragment with all other fragments found in this
// selection set to collect conflicts between fragments spread together.
// This compares each item in the list of fragment names to every other
Expand Down Expand Up @@ -439,33 +428,15 @@ function findConflictsBetweenSubSelectionSets(
);
}

// (E) Then collect any conflicts between the provided collection of fields
// and any fragment names found in the given fragment.
while (discoveredFragments.length !== 0) {
const item = discoveredFragments.pop();
if (
!item ||
comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)
) {
continue;
}
const [fragmentName, referencedFragmentName] = item;
comparedFragmentPairs.add(
referencedFragmentName,
fragmentName,
areMutuallyExclusive,
);
collectConflictsBetweenFieldsAndFragment(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap1,
referencedFragmentName,
discoveredFragments,
);
}
processDiscoveredFragments(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap1,
discoveredFragments,
);

// (I) Then collect conflicts between the second collection of fields and
// those referenced by each fragment name associated with the first.
Expand All @@ -482,8 +453,46 @@ function findConflictsBetweenSubSelectionSets(
);
}

// (E) Then collect any conflicts between the provided collection of fields
// and any fragment names found in the given fragment.
processDiscoveredFragments(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap2,
discoveredFragments,
);

// (J) Also collect conflicts between any fragment names by the first and
// fragment names by the second. This compares each item in the first set of
// names to each item in the second set of names.
for (const fragmentName1 of fragmentNames1) {
for (const fragmentName2 of fragmentNames2) {
collectConflictsBetweenFragments(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fragmentName1,
fragmentName2,
);
}
}
return conflicts;
}

// (E) Then collect any conflicts between the provided collection of fields
// and any fragment names found in the given fragment.
const processDiscoveredFragments = (
context: ValidationContext,
conflicts: Array<Conflict>,
cachedFieldsAndFragmentNames: Map<SelectionSetNode, FieldsAndFragmentNames>,
comparedFragmentPairs: PairSet,
areMutuallyExclusive: boolean,
fieldMap: NodeAndDefCollection,
discoveredFragments: Array<Array<string>>,
) => {
while (discoveredFragments.length !== 0) {
const item = discoveredFragments.pop();
if (
Expand All @@ -504,30 +513,12 @@ function findConflictsBetweenSubSelectionSets(
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap2,
fieldMap,
referencedFragmentName,
discoveredFragments,
);
}

// (J) Also collect conflicts between any fragment names by the first and
// fragment names by the second. This compares each item in the first set of
// names to each item in the second set of names.
for (const fragmentName1 of fragmentNames1) {
for (const fragmentName2 of fragmentNames2) {
collectConflictsBetweenFragments(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fragmentName1,
fragmentName2,
);
}
}
return conflicts;
}
};

// Collect all Conflicts "within" one collection of fields.
function collectConflictsWithin(
Expand Down

0 comments on commit d4c5449

Please sign in to comment.