diff --git a/graphql_schema/lib/src/enum.dart b/graphql_schema/lib/src/enum.dart index 3169f737..d85a7eca 100644 --- a/graphql_schema/lib/src/enum.dart +++ b/graphql_schema/lib/src/enum.dart @@ -1,8 +1,15 @@ part of graphql_schema.src.schema; +GraphQLEnumType enumType(String name, List values, + {String description}) { + return new GraphQLEnumType( + name, values.map((s) => new GraphQLEnumValue(s)).toList(), + description: description); +} + class GraphQLEnumType extends _GraphQLStringType { final String name; - final List values; + final List values; final String description; GraphQLEnumType(this.name, this.values, {this.description}) : super._(); @@ -11,7 +18,8 @@ class GraphQLEnumType extends _GraphQLStringType { ValidationResult validate(String key, String input) { var result = super.validate(key, input); - if (result.successful && !values.contains(result.value)) { + if (result.successful && + !values.map((v) => v.name).contains(result.value)) { return result._asFailure() ..errors.add( '"${result.value}" is not a valid value for the enum "$name".'); @@ -20,3 +28,15 @@ class GraphQLEnumType extends _GraphQLStringType { return result; } } + +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; +} diff --git a/graphql_schema/test/validation_test.dart b/graphql_schema/test/validation_test.dart index a6c8edd3..bfe23bed 100644 --- a/graphql_schema/test/validation_test.dart +++ b/graphql_schema/test/validation_test.dart @@ -2,7 +2,7 @@ import 'package:graphql_schema/graphql_schema.dart'; import 'package:test/test.dart'; void main() { - var typeType = new GraphQLEnumType('Type', [ + var typeType = enumType('Type', [ 'FIRE', 'WATER', 'GRASS',