coerceArgumentValues method

Map<String, dynamic> coerceArgumentValues(
  1. GraphQLObjectType objectType,
  2. SelectionContext field,
  3. Map<String?, dynamic> variableValues
)

Implementation

Map<String, dynamic> coerceArgumentValues(GraphQLObjectType objectType,
    SelectionContext field, Map<String?, dynamic> variableValues) {
  var coercedValues = <String, dynamic>{};
  var argumentValues = field.field?.arguments;
  var fieldName =
      field.field?.fieldName.alias?.name ?? field.field?.fieldName.name;
  var desiredField = objectType.fields.firstWhere((f) => f.name == fieldName,
      orElse: (() => throw FormatException(
          '${objectType.name} has no field named "$fieldName".')));
  var argumentDefinitions = desiredField.inputs;

  for (var argumentDefinition in argumentDefinitions) {
    var argumentName = argumentDefinition.name;
    var argumentType = argumentDefinition.type;
    var defaultValue = argumentDefinition.defaultValue;

    var argumentValue =
        argumentValues?.firstWhereOrNull((a) => a.name == argumentName);

    if (argumentValue == null) {
      if (defaultValue != null || argumentDefinition.defaultsToNull) {
        coercedValues[argumentName] = defaultValue;
      } else if (argumentType is GraphQLNonNullableType) {
        throw GraphQLException.fromMessage(
            'Missing value for argument "$argumentName" of field "$fieldName".');
      } else {
        continue;
      }
    } else {
      final inputValue = argumentValue.value
          .computeValue(variableValues as Map<String, dynamic>);

      try {
        final validation = argumentType.validate(argumentName, inputValue);

        if (!validation.successful) {
          var errors = <GraphQLExceptionError>[
            GraphQLExceptionError(
              'Type coercion error for value of argument "$argumentName" of field "$fieldName". ($inputValue)',
              locations: [
                GraphExceptionErrorLocation.fromSourceLocation(
                    argumentValue.value.span!.start)
              ],
            )
          ];

          for (var error in validation.errors) {
            var err = argumentValue.value.span?.start;
            var locations = <GraphExceptionErrorLocation>[];
            if (err != null) {
              locations
                  .add(GraphExceptionErrorLocation.fromSourceLocation(err));
            }
            errors.add(
              GraphQLExceptionError(
                error,
                locations: locations,
              ),
            );
          }

          throw GraphQLException(errors);
        } else {
          final coercedValue = argumentType.deserialize(inputValue);

          coercedValues[argumentName] = coercedValue;
        }
      } on TypeError catch (e) {
        var err = argumentValue.value.span?.start;
        var locations = <GraphExceptionErrorLocation>[];
        if (err != null) {
          locations.add(GraphExceptionErrorLocation.fromSourceLocation(err));
        }

        throw GraphQLException(<GraphQLExceptionError>[
          GraphQLExceptionError(
            'Type coercion error for value of argument "$argumentName" of field "$fieldName". [$inputValue]',
            locations: locations,
          ),
          GraphQLExceptionError(
            e.toString(),
            locations: locations,
          ),
        ]);
      }
    }
  }

  return coercedValues;
}