Finished coerceArgumentValues
This commit is contained in:
parent
cd4a2233de
commit
4f91c0b6a8
4 changed files with 57 additions and 16 deletions
|
@ -4,5 +4,10 @@ class GraphQLFieldArgument<Value, Serialized> {
|
|||
final String name;
|
||||
final GraphQLType<Value, Serialized> type;
|
||||
final Value defaultValue;
|
||||
GraphQLFieldArgument(this.name, this.type, {this.defaultValue});
|
||||
|
||||
/// If [defaultValue] is `null`, and `null` is a valid value for this argument, set this to `true`.
|
||||
final bool defaultsToNull;
|
||||
|
||||
GraphQLFieldArgument(this.name, this.type,
|
||||
{this.defaultValue, this.defaultsToNull: false});
|
||||
}
|
||||
|
|
|
@ -4,12 +4,17 @@ typedef FutureOr<Value> GraphQLFieldResolver<Value, Serialized>(
|
|||
Serialized serialized);
|
||||
|
||||
class GraphQLField<Value, Serialized> {
|
||||
final List<GraphQLFieldArgument> arguments = <GraphQLFieldArgument>[];
|
||||
final String name;
|
||||
final GraphQLFieldArgument argument;
|
||||
final GraphQLFieldResolver<Value, Serialized> resolve;
|
||||
final GraphQLType<Value, Serialized> type;
|
||||
|
||||
GraphQLField(this.name, {this.argument, this.resolve, this.type});
|
||||
GraphQLField(this.name,
|
||||
{Iterable<GraphQLFieldArgument> arguments: const <GraphQLFieldArgument>[],
|
||||
this.resolve,
|
||||
this.type}) {
|
||||
this.arguments.addAll(arguments ?? <GraphQLFieldArgument>[]);
|
||||
}
|
||||
|
||||
FutureOr<Serialized> serialize(Value value) {
|
||||
return type.serialize(value);
|
||||
|
|
|
@ -5,6 +5,8 @@ abstract class GraphQLType<Value, Serialized> {
|
|||
Value deserialize(Serialized serialized);
|
||||
ValidationResult<Serialized> validate(String key, Serialized input);
|
||||
GraphQLType<Value, Serialized> nonNullable();
|
||||
|
||||
bool get isNullable => false;
|
||||
}
|
||||
|
||||
/// Shorthand to create a [GraphQLListType].
|
||||
|
@ -64,6 +66,9 @@ class _GraphQLNonNullableType<Value, Serialized>
|
|||
final GraphQLType<Value, Serialized> type;
|
||||
_GraphQLNonNullableType._(this.type);
|
||||
|
||||
@override
|
||||
bool get isNullable => true;
|
||||
|
||||
@override
|
||||
GraphQLType<Value, Serialized> nonNullable() {
|
||||
throw new UnsupportedError(
|
||||
|
|
|
@ -33,8 +33,7 @@ class GraphQL {
|
|||
if (customTypes.containsKey(ctx.typeName.name))
|
||||
return customTypes[ctx.typeName.name];
|
||||
throw new ArgumentError(
|
||||
'Unknown GraphQL type: "${ctx.typeName.name}"\n${ctx.span
|
||||
.highlight()}');
|
||||
'Unknown GraphQL type: "${ctx.typeName.name}"\n${ctx.span.highlight()}');
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
|
@ -168,9 +167,36 @@ class GraphQL {
|
|||
var argumentValues = field.field.arguments;
|
||||
var fieldName = field.field.fieldName.name;
|
||||
var desiredField = objectType.fields.firstWhere((f) => f.name == fieldName);
|
||||
var argumentDefinitions = desiredField.arguments;
|
||||
|
||||
// TODO: Multiple arguments?
|
||||
var argumentDefinitions = desiredField.argument;
|
||||
for (var argumentDefinition in argumentDefinitions) {
|
||||
var argumentName = argumentDefinition.name;
|
||||
var argumentType = argumentDefinition.type;
|
||||
var defaultValue = argumentDefinition.defaultValue;
|
||||
var value = argumentValues.firstWhere((a) => a.name == argumentName,
|
||||
orElse: () => null);
|
||||
|
||||
if (value != null) {
|
||||
var variableName = value.name;
|
||||
var variableValue = variableValues[variableName];
|
||||
|
||||
if (variableValues.containsKey(variableName)) {
|
||||
coercedValues[argumentName] = variableValue;
|
||||
} else if (defaultValue != null || argumentDefinition.defaultsToNull) {
|
||||
coercedValues[argumentName] = defaultValue;
|
||||
} else if (!argumentType.isNullable) {
|
||||
throw new GraphQLException(
|
||||
'Missing value for argument "$argumentName".');
|
||||
}
|
||||
} else {
|
||||
if (defaultValue != null || argumentDefinition.defaultsToNull) {
|
||||
coercedValues[argumentName] = defaultValue;
|
||||
} else if (!argumentType.isNullable) {
|
||||
throw new GraphQLException(
|
||||
'Missing value for argument "$argumentName".');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return coercedValues;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue