Added enum type
This commit is contained in:
parent
398598e6fd
commit
ceced6726d
3 changed files with 30 additions and 0 deletions
22
graphql_schema/lib/src/enum.dart
Normal file
22
graphql_schema/lib/src/enum.dart
Normal file
|
@ -0,0 +1,22 @@
|
|||
part of graphql_schema.src.schema;
|
||||
|
||||
class GraphQLEnumType extends _GraphQLStringType {
|
||||
final String name;
|
||||
final List<String> values;
|
||||
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);
|
||||
|
||||
if (result.successful && !values.contains(result.value)) {
|
||||
return result._asFailure()
|
||||
..errors.add(
|
||||
'"${result.value}" is not a valid value for the enum "$name".');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -7,6 +7,8 @@ import 'package:source_span/source_span.dart';
|
|||
|
||||
part 'argument.dart';
|
||||
|
||||
part 'enum.dart';
|
||||
|
||||
part 'field.dart';
|
||||
|
||||
part 'gen.dart';
|
||||
|
|
|
@ -5,6 +5,8 @@ class ValidationResult<T> {
|
|||
final T value;
|
||||
final List<String> errors;
|
||||
|
||||
ValidationResult._(this.successful, this.value, this.errors);
|
||||
|
||||
ValidationResult._ok(this.value)
|
||||
: errors = [],
|
||||
successful = true;
|
||||
|
@ -12,4 +14,8 @@ class ValidationResult<T> {
|
|||
ValidationResult._failure(this.errors)
|
||||
: value = null,
|
||||
successful = false;
|
||||
|
||||
ValidationResult<T> _asFailure() {
|
||||
return new ValidationResult<T>._(false, value, errors);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue