findConflictsWithinSelectionSet function

List<Conflict> findConflictsWithinSelectionSet(
  1. ValidationCtx context,
  2. Map<SelectionSetNode, FieldsAndFragmentNames> cachedFieldsAndFragmentNames,
  3. PairSet comparedFragmentPairs,
  4. GraphQLNamedType? parentType,
  5. SelectionSetNode selectionSet,
)

Find all conflicts found "within" a selection set, including those found via spreading in fragments. Called when visiting each SelectionSet in the GraphQL Document.

Implementation

List<Conflict> findConflictsWithinSelectionSet(
  ValidationCtx context,
  Map<SelectionSetNode, FieldsAndFragmentNames> cachedFieldsAndFragmentNames,
  PairSet comparedFragmentPairs,
  GraphQLNamedType? parentType,
  SelectionSetNode selectionSet,
) {
  final conflicts = <Conflict>[];

  final _v = getFieldsAndFragmentNames(
    context,
    cachedFieldsAndFragmentNames,
    parentType,
    selectionSet,
  );
  final fieldMap = _v.fieldMap;
  final fragmentNames = _v.fragmentNames;

  // (A) Find find all conflicts "within" the fields of this selection set.
  // Note: this is the *only place* `collectConflictsWithin` is called.
  collectConflictsWithin(
    context,
    conflicts,
    cachedFieldsAndFragmentNames,
    comparedFragmentPairs,
    fieldMap,
  );

  if (fragmentNames.isNotEmpty) {
    // (B) Then collect conflicts between these fields and those represented by
    // each spread fragment name found.
    for (int i = 0; i < fragmentNames.length; i++) {
      collectConflictsBetweenFieldsAndFragment(
        context,
        conflicts,
        cachedFieldsAndFragmentNames,
        comparedFragmentPairs,
        false,
        fieldMap,
        fragmentNames[i],
      );
      // (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
      // item in that same list (except for itself).
      for (int j = i + 1; j < fragmentNames.length; j++) {
        collectConflictsBetweenFragments(
          context,
          conflicts,
          cachedFieldsAndFragmentNames,
          comparedFragmentPairs,
          false,
          fragmentNames[i],
          fragmentNames[j],
        );
      }
    }
  }
  return conflicts;
}