From ceced6726dd658fe9b5d269c8d25dd5e420c2bd6 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 3 Aug 2018 18:01:36 -0400 Subject: [PATCH] Added enum type --- graphql_schema/lib/src/enum.dart | 22 +++++++++++++++++++ graphql_schema/lib/src/schema.dart | 2 ++ graphql_schema/lib/src/validation_result.dart | 6 +++++ 3 files changed, 30 insertions(+) create mode 100644 graphql_schema/lib/src/enum.dart 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