completeValue method

Future completeValue(
  1. DocumentContext document,
  2. String? fieldName,
  3. GraphQLType fieldType,
  4. List<SelectionContext> fields,
  5. dynamic result,
  6. Map<String, dynamic> variableValues,
  7. Map<String, dynamic> globalVariables, {
  8. List<List> lazy = const [],
})

Implementation

Future completeValue(
    DocumentContext document,
    String? fieldName,
    GraphQLType fieldType,
    List<SelectionContext> fields,
    dynamic result,
    Map<String, dynamic> variableValues,
    Map<String, dynamic> globalVariables,
    {List<List> lazy = const []}) async {
  if (fieldType is GraphQLNonNullableType) {
    var innerType = fieldType.ofType;
    var completedResult = await completeValue(document, fieldName, innerType,
        fields, result, variableValues, globalVariables);

    if (completedResult == null) {
      throw GraphQLException.fromMessage(
          'Null value provided for non-nullable field "$fieldName".');
    } else {
      return completedResult;
    }
  }

  if (result == null) {
    return null;
  }

  if (fieldType is GraphQLListType) {
    if (result is! Iterable) {
      throw GraphQLException.fromMessage(
          'Value of field "$fieldName" must be a list or iterable, got $result instead.');
    }

    var innerType = fieldType.ofType;
    var futureOut = [];

    for (var resultItem in result) {
      futureOut.add(completeValue(document, '(item in "$fieldName")',
          innerType, fields, resultItem, variableValues, globalVariables));
    }

    var out = [];
    for (var f in futureOut) {
      out.add(await f);
    }

    return out;
  }

  if (fieldType is GraphQLScalarType) {
    try {
      final ret = fieldType.serialize(result);

      return ret;
    } on TypeError {
      throw GraphQLException.fromMessage(
          'Value of field "$fieldName" must be ${fieldType.valueType}, got $result (${result.runtimeType}) instead.');
    }
  }

  if (fieldType is GraphQLObjectType || fieldType is GraphQLUnionType) {
    GraphQLObjectType objectType;

    if (fieldType is GraphQLObjectType && !fieldType.isInterface) {
      objectType = fieldType;
    } else {
      objectType = resolveAbstractType(fieldName, fieldType, result);
    }

    //objectType = fieldType as GraphQLObjectType;
    var subSelectionSet = mergeSelectionSets(fields);
    return await executeSelectionSet(document, subSelectionSet, objectType,
        result, variableValues, globalVariables,
        lazy: lazy, parentType: fieldType as GraphQLObjectType);
  }

  throw UnsupportedError('Unsupported type: $fieldType');
}