collectFields function

Map<String, List<FieldNode>> collectFields(
  1. GraphQLSchema schema,
  2. Map<String, FragmentDefinitionNode> fragments,
  3. GraphQLObjectType objectType,
  4. SelectionSetNode selectionSet,
  5. Map<String, dynamic> variableValues, {
  6. Set<String>? visitedFragments,
  7. bool aliased = true,
})

Implementation

Map<String, List<FieldNode>> collectFields(
  GraphQLSchema schema,
  Map<String, FragmentDefinitionNode> fragments,
  GraphQLObjectType objectType,
  SelectionSetNode selectionSet,
  Map<String, dynamic> variableValues, {
  Set<String>? visitedFragments,
  bool aliased = true,
}) {
  final groupedFields = <String, List<FieldNode>>{};
  visitedFragments ??= {};

  for (final selection in selectionSet.selections) {
    final directives = selection.directives;
    if (getDirectiveValue('skip', 'if', directives, variableValues) == true) {
      continue;
    }
    if (getDirectiveValue('include', 'if', directives, variableValues) ==
        false) {
      continue;
    }

    if (selection is FieldNode) {
      final responseKey = aliased
          ? (selection.alias?.value ?? selection.name.value)
          : selection.name.value;
      final groupForResponseKey =
          groupedFields.putIfAbsent(responseKey, () => []);
      groupForResponseKey.add(selection);
    } else if (selection is FragmentSpreadNode) {
      final fragmentSpreadName = selection.name.value;
      if (visitedFragments.contains(fragmentSpreadName)) continue;
      visitedFragments.add(fragmentSpreadName);
      final fragment = fragments[fragmentSpreadName];

      if (fragment == null) continue;
      final fragmentType = fragment.typeCondition;
      if (!doesFragmentTypeApply(objectType, fragmentType, schema)) continue;
      final fragmentSelectionSet = fragment.selectionSet;
      final fragmentGroupFieldSet = collectFields(
          schema, fragments, objectType, fragmentSelectionSet, variableValues,
          visitedFragments: visitedFragments, aliased: aliased);

      for (final groupEntry in fragmentGroupFieldSet.entries) {
        final responseKey = groupEntry.key;
        final fragmentGroup = groupEntry.value;
        final groupForResponseKey =
            groupedFields.putIfAbsent(responseKey, () => []);
        groupForResponseKey.addAll(fragmentGroup);
      }
    } else if (selection is InlineFragmentNode) {
      final fragmentType = selection.typeCondition;
      if (fragmentType != null &&
          !doesFragmentTypeApply(objectType, fragmentType, schema)) continue;
      final fragmentSelectionSet = selection.selectionSet;
      final fragmentGroupFieldSet = collectFields(
          schema, fragments, objectType, fragmentSelectionSet, variableValues,
          visitedFragments: visitedFragments, aliased: aliased);

      for (final groupEntry in fragmentGroupFieldSet.entries) {
        final responseKey = groupEntry.key;
        final fragmentGroup = groupEntry.value;
        final groupForResponseKey =
            groupedFields.putIfAbsent(responseKey, () => []);
        groupForResponseKey.addAll(fragmentGroup);
      }
    }
  }

  return groupedFields;
}