diff --git a/graphql_schema/lib/src/enum.dart b/graphql_schema/lib/src/enum.dart new file mode 100644 index 00000000..3169f737 --- /dev/null +++ b/graphql_schema/lib/src/enum.dart @@ -0,0 +1,22 @@ +part of graphql_schema.src.schema; + +class GraphQLEnumType extends _GraphQLStringType { + final String name; + final List values; + final String description; + + GraphQLEnumType(this.name, this.values, {this.description}) : super._(); + + @override + ValidationResult 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; + } +} diff --git a/graphql_schema/lib/src/schema.dart b/graphql_schema/lib/src/schema.dart index 6eeb5e3f..3fcc7a52 100644 --- a/graphql_schema/lib/src/schema.dart +++ b/graphql_schema/lib/src/schema.dart @@ -7,6 +7,8 @@ import 'package:source_span/source_span.dart'; part 'argument.dart'; +part 'enum.dart'; + part 'field.dart'; part 'gen.dart'; diff --git a/graphql_schema/lib/src/validation_result.dart b/graphql_schema/lib/src/validation_result.dart index 54aadb83..92b7c5fb 100644 --- a/graphql_schema/lib/src/validation_result.dart +++ b/graphql_schema/lib/src/validation_result.dart @@ -5,6 +5,8 @@ class ValidationResult { final T value; final List errors; + ValidationResult._(this.successful, this.value, this.errors); + ValidationResult._ok(this.value) : errors = [], successful = true; @@ -12,4 +14,8 @@ class ValidationResult { ValidationResult._failure(this.errors) : value = null, successful = false; + + ValidationResult _asFailure() { + return new ValidationResult._(false, value, errors); + } } \ No newline at end of file