possibleSelectionsCallback function

PossibleSelections? Function() possibleSelectionsCallback(
  1. GraphQLSchema schema,
  2. GraphQLObjectField field,
  3. List<FieldNode> fields,
  4. DocumentNode document,
  5. Map<String, Object?> variableValues,
)

Implementation

PossibleSelections? Function() possibleSelectionsCallback(
  GraphQLSchema schema,
  GraphQLObjectField field,
  List<FieldNode> fields,
  DocumentNode document,
  Map<String, Object?> variableValues,
) {
  bool calculated = false;
  PossibleSelections? _value;
  return () {
    if (calculated) return _value;

    final possibleObjects = <GraphQLObjectType>[];

    void _mapperType(GraphQLWrapperType nn) {
      final ofType = nn.ofType;
      if (ofType is GraphQLObjectType) {
        possibleObjects.add(ofType);
      } else if (ofType is GraphQLUnionType) {
        possibleObjects.addAll(ofType.possibleTypes);
      } else if (ofType is GraphQLWrapperType) {
        _mapperType(ofType as GraphQLWrapperType);
      }
    }

    field.type.whenOrNull(
      object: (obj) {
        possibleObjects.add(obj);
      },
      union: (union) => possibleObjects.addAll(union.possibleTypes),
      nonNullable: _mapperType,
      list: _mapperType,
    );
    if (possibleObjects.isNotEmpty) {
      final selectionSet = mergeSelectionSets(fields);
      final fragments = fragmentsFromDocument(document);

      _value = PossibleSelections(
        Map.fromEntries(
          possibleObjects.map((obj) {
            final nonAliased = collectFields(
              schema,
              fragments,
              obj,
              selectionSet,
              variableValues,
              aliased: false,
            );
            final aliased = collectFields(
              schema,
              fragments,
              obj,
              selectionSet,
              variableValues,
              aliased: true,
            );

            Iterable<MapEntry<String, PossibleSelectionsField>> _mapEntries(
              Iterable<MapEntry<String, List<FieldNode>>> entries,
            ) {
              return entries.map(
                (e) {
                  final fieldName = e.value.first.name.value;
                  final field = obj.fieldByName(fieldName);
                  if (field == null) {
                    return null;
                  }
                  return MapEntry(
                    e.key,
                    PossibleSelectionsField(
                      e.value,
                      field,
                      possibleSelectionsCallback(
                        schema,
                        field,
                        e.value,
                        document,
                        variableValues,
                      ),
                    ),
                  );
                },
              ).whereType();
            }

            return MapEntry(
              obj.name,
              PossibleSelectionsObject(
                map: Map.fromEntries(
                  _mapEntries(nonAliased.entries),
                ),
                mapAliased: Map.fromEntries(
                  _mapEntries(aliased.entries),
                ),
                objectType: obj,
              ),
            );
          }),
        ),
        fields,
        field,
      );
    }
    calculated = true;
    return _value;
  };
}