2018-08-03 22:01:36 +00:00
|
|
|
part of graphql_schema.src.schema;
|
|
|
|
|
2018-08-03 22:10:29 +00:00
|
|
|
GraphQLEnumType enumType(String name, List<String> values,
|
|
|
|
{String description}) {
|
|
|
|
return new GraphQLEnumType(
|
|
|
|
name, values.map((s) => new GraphQLEnumValue(s)).toList(),
|
|
|
|
description: description);
|
|
|
|
}
|
|
|
|
|
2018-08-03 22:01:36 +00:00
|
|
|
class GraphQLEnumType extends _GraphQLStringType {
|
|
|
|
final String name;
|
2018-08-03 22:10:29 +00:00
|
|
|
final List<GraphQLEnumValue> values;
|
2018-08-03 22:01:36 +00:00
|
|
|
final String description;
|
|
|
|
|
|
|
|
GraphQLEnumType(this.name, this.values, {this.description}) : super._();
|
|
|
|
|
|
|
|
@override
|
|
|
|
ValidationResult<String> validate(String key, String input) {
|
|
|
|
var result = super.validate(key, input);
|
|
|
|
|
2018-08-03 22:10:29 +00:00
|
|
|
if (result.successful &&
|
|
|
|
!values.map((v) => v.name).contains(result.value)) {
|
2018-08-03 22:01:36 +00:00
|
|
|
return result._asFailure()
|
|
|
|
..errors.add(
|
|
|
|
'"${result.value}" is not a valid value for the enum "$name".');
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 22:10:29 +00:00
|
|
|
|
|
|
|
class GraphQLEnumValue {
|
|
|
|
final String name;
|
|
|
|
final String deprecationReason;
|
|
|
|
|
|
|
|
GraphQLEnumValue(this.name, {this.deprecationReason});
|
|
|
|
|
|
|
|
bool get isDeprecated => deprecationReason != null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(other) => other is GraphQLEnumValue && other.name == name;
|
|
|
|
}
|