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 String name;
|
||||||
final GraphQLType<Value, Serialized> type;
|
final GraphQLType<Value, Serialized> type;
|
||||||
final Value defaultValue;
|
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);
|
Serialized serialized);
|
||||||
|
|
||||||
class GraphQLField<Value, Serialized> {
|
class GraphQLField<Value, Serialized> {
|
||||||
|
final List<GraphQLFieldArgument> arguments = <GraphQLFieldArgument>[];
|
||||||
final String name;
|
final String name;
|
||||||
final GraphQLFieldArgument argument;
|
|
||||||
final GraphQLFieldResolver<Value, Serialized> resolve;
|
final GraphQLFieldResolver<Value, Serialized> resolve;
|
||||||
final GraphQLType<Value, Serialized> type;
|
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) {
|
FutureOr<Serialized> serialize(Value value) {
|
||||||
return type.serialize(value);
|
return type.serialize(value);
|
||||||
|
|
|
@ -5,6 +5,8 @@ abstract class GraphQLType<Value, Serialized> {
|
||||||
Value deserialize(Serialized serialized);
|
Value deserialize(Serialized serialized);
|
||||||
ValidationResult<Serialized> validate(String key, Serialized input);
|
ValidationResult<Serialized> validate(String key, Serialized input);
|
||||||
GraphQLType<Value, Serialized> nonNullable();
|
GraphQLType<Value, Serialized> nonNullable();
|
||||||
|
|
||||||
|
bool get isNullable => false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shorthand to create a [GraphQLListType].
|
/// Shorthand to create a [GraphQLListType].
|
||||||
|
@ -64,6 +66,9 @@ class _GraphQLNonNullableType<Value, Serialized>
|
||||||
final GraphQLType<Value, Serialized> type;
|
final GraphQLType<Value, Serialized> type;
|
||||||
_GraphQLNonNullableType._(this.type);
|
_GraphQLNonNullableType._(this.type);
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get isNullable => true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
GraphQLType<Value, Serialized> nonNullable() {
|
GraphQLType<Value, Serialized> nonNullable() {
|
||||||
throw new UnsupportedError(
|
throw new UnsupportedError(
|
||||||
|
|
|
@ -33,8 +33,7 @@ class GraphQL {
|
||||||
if (customTypes.containsKey(ctx.typeName.name))
|
if (customTypes.containsKey(ctx.typeName.name))
|
||||||
return customTypes[ctx.typeName.name];
|
return customTypes[ctx.typeName.name];
|
||||||
throw new ArgumentError(
|
throw new ArgumentError(
|
||||||
'Unknown GraphQL type: "${ctx.typeName.name}"\n${ctx.span
|
'Unknown GraphQL type: "${ctx.typeName.name}"\n${ctx.span.highlight()}');
|
||||||
.highlight()}');
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -168,9 +167,36 @@ class GraphQL {
|
||||||
var argumentValues = field.field.arguments;
|
var argumentValues = field.field.arguments;
|
||||||
var fieldName = field.field.fieldName.name;
|
var fieldName = field.field.fieldName.name;
|
||||||
var desiredField = objectType.fields.firstWhere((f) => f.name == fieldName);
|
var desiredField = objectType.fields.firstWhere((f) => f.name == fieldName);
|
||||||
|
var argumentDefinitions = desiredField.arguments;
|
||||||
|
|
||||||
// TODO: Multiple arguments?
|
for (var argumentDefinition in argumentDefinitions) {
|
||||||
var argumentDefinitions = desiredField.argument;
|
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;
|
return coercedValues;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue