resolveAbstractType method

GraphQLObjectType resolveAbstractType(
  1. String? fieldName,
  2. GraphQLType type,
  3. dynamic result
)

Implementation

GraphQLObjectType resolveAbstractType(
    String? fieldName, GraphQLType type, dynamic result) {
  List<GraphQLObjectType> possibleTypes;

  if (type is GraphQLObjectType) {
    if (type.isInterface) {
      possibleTypes = type.possibleTypes;
    } else {
      return type;
    }
  } else if (type is GraphQLUnionType) {
    possibleTypes = type.possibleTypes;
  } else {
    throw ArgumentError();
  }

  final errors = <GraphQLExceptionError>[];
  final types = [];

  for (var t in possibleTypes) {
    try {
      var validation =
          t.validate(fieldName!, foldToStringDynamic(result as Map?));

      if (validation.successful) {
        types.add(t);
      } else {
        errors.addAll(validation.errors.map((m) => GraphQLExceptionError(m)));
      }
    } on GraphQLException catch (e) {
      errors.addAll(e.errors);
    }
  }

  if (types.isNotEmpty) {
    if (types.length == 1) {
      return types.first;
    } else if (type is GraphQLObjectType) {
      return type;
    }
  }

  errors.insert(0,
      GraphQLExceptionError('Cannot convert value $result to type $type.'));

  throw GraphQLException(errors);
}