Skip to content

Commit b2a71fc

Browse files
sachindshindeyaacovCR
authored andcommitted
Modify comparedFragmentPairs to allow a selection set to replace a fragment, and modify collectConflictsBetweenFieldsAndFragment() to memoize the selection set and fragment
1 parent a359a93 commit b2a71fc

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/validation/rules/OverlappingFieldsCanBeMergedRule.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ function collectConflictsBetweenFieldsAndFragment(
235235
fieldMap: NodeAndDefCollection,
236236
fragmentName: string,
237237
): void {
238+
// Memoize so the fields and fragments are not compared for conflicts more
239+
// than once.
240+
if (comparedFragmentPairs.has(fieldMap, fragmentName, areMutuallyExclusive)) {
241+
return;
242+
}
243+
comparedFragmentPairs.add(fieldMap, fragmentName, areMutuallyExclusive);
244+
238245
const fragment = context.getFragment(fragmentName);
239246
if (!fragment) {
240247
return;
@@ -800,14 +807,19 @@ function subfieldConflicts(
800807
* A way to keep track of pairs of things when the ordering of the pair does not matter.
801808
*/
802809
class PairSet {
803-
_data: Map<string, Map<string, boolean>>;
810+
_data: Map<string | NodeAndDefCollection, Map<string, boolean>>;
804811

805812
constructor() {
806813
this._data = new Map();
807814
}
808815

809-
has(a: string, b: string, areMutuallyExclusive: boolean): boolean {
810-
const [key1, key2] = a < b ? [a, b] : [b, a];
816+
has(
817+
a: string | NodeAndDefCollection,
818+
b: string,
819+
areMutuallyExclusive: boolean,
820+
): boolean {
821+
const [key1, key2] =
822+
typeof a !== 'string' ? [a, b] : a < b ? [a, b] : [b, a];
811823

812824
const result = this._data.get(key1)?.get(key2);
813825
if (result === undefined) {
@@ -820,8 +832,13 @@ class PairSet {
820832
return areMutuallyExclusive ? true : areMutuallyExclusive === result;
821833
}
822834

823-
add(a: string, b: string, areMutuallyExclusive: boolean): void {
824-
const [key1, key2] = a < b ? [a, b] : [b, a];
835+
add(
836+
a: string | NodeAndDefCollection,
837+
b: string,
838+
areMutuallyExclusive: boolean,
839+
): void {
840+
const [key1, key2] =
841+
typeof a !== 'string' ? [a, b] : a < b ? [a, b] : [b, a];
825842

826843
const map = this._data.get(key1);
827844
if (map === undefined) {

0 commit comments

Comments
 (0)