collectConflictsBetweenFragments function

void collectConflictsBetweenFragments(
  1. ValidationCtx context,
  2. List<Conflict> conflicts,
  3. Map<SelectionSetNode, FieldsAndFragmentNames> cachedFieldsAndFragmentNames,
  4. PairSet comparedFragmentPairs,
  5. bool areMutuallyExclusive,
  6. String fragmentName1,
  7. String fragmentName2,
)

Collect all conflicts found between two fragments, including via spreading in any nested fragments.

Implementation

void collectConflictsBetweenFragments(
  ValidationCtx context,
  List<Conflict> conflicts,
  Map<SelectionSetNode, FieldsAndFragmentNames> cachedFieldsAndFragmentNames,
  PairSet comparedFragmentPairs,
  bool areMutuallyExclusive,
  String fragmentName1,
  String fragmentName2,
) {
  // No need to compare a fragment to itself.
  if (fragmentName1 == fragmentName2) {
    return;
  }

  // Memoize so two fragments are not compared for conflicts more than once.
  if (comparedFragmentPairs.has(
    fragmentName1,
    fragmentName2,
    areMutuallyExclusive,
  )) {
    return;
  }
  comparedFragmentPairs.add(fragmentName1, fragmentName2, areMutuallyExclusive);

  final fragment1 = context.fragmentsMap[fragmentName1];
  final fragment2 = context.fragmentsMap[fragmentName2];
  if (fragment1 == null || fragment2 == null) {
    return;
  }

  final fg1 = getReferencedFieldsAndFragmentNames(
    context,
    cachedFieldsAndFragmentNames,
    fragment1,
  );
  final fieldMap1 = fg1.fieldMap;
  final referencedFragmentNames1 = fg1.fragmentNames;
  final fg2 = getReferencedFieldsAndFragmentNames(
    context,
    cachedFieldsAndFragmentNames,
    fragment2,
  );
  final fieldMap2 = fg2.fieldMap;
  final referencedFragmentNames2 = fg2.fragmentNames;

  // (F) First, collect all conflicts between these two collections of fields
  // (not including any nested fragments).
  collectConflictsBetween(
    context,
    conflicts,
    cachedFieldsAndFragmentNames,
    comparedFragmentPairs,
    areMutuallyExclusive,
    fieldMap1,
    fieldMap2,
  );

  // (G) Then collect conflicts between the first fragment and any nested
  // fragments spread in the second fragment.
  for (final referencedFragmentName2 in referencedFragmentNames2) {
    collectConflictsBetweenFragments(
      context,
      conflicts,
      cachedFieldsAndFragmentNames,
      comparedFragmentPairs,
      areMutuallyExclusive,
      fragmentName1,
      referencedFragmentName2,
    );
  }

  // (G) Then collect conflicts between the second fragment and any nested
  // fragments spread in the first fragment.
  for (final referencedFragmentName1 in referencedFragmentNames1) {
    collectConflictsBetweenFragments(
      context,
      conflicts,
      cachedFieldsAndFragmentNames,
      comparedFragmentPairs,
      areMutuallyExclusive,
      referencedFragmentName1,
      fragmentName2,
    );
  }
}