Formalized GraphQLEnumType, add enumType funcion

This commit is contained in:
Tobe O 2018-08-03 18:10:29 -04:00
parent 0ddecbc44e
commit 3c4a03a25d
2 changed files with 23 additions and 3 deletions

View file

@ -1,8 +1,15 @@
part of graphql_schema.src.schema; part of graphql_schema.src.schema;
GraphQLEnumType enumType(String name, List<String> values,
{String description}) {
return new GraphQLEnumType(
name, values.map((s) => new GraphQLEnumValue(s)).toList(),
description: description);
}
class GraphQLEnumType extends _GraphQLStringType { class GraphQLEnumType extends _GraphQLStringType {
final String name; final String name;
final List<String> values; final List<GraphQLEnumValue> values;
final String description; final String description;
GraphQLEnumType(this.name, this.values, {this.description}) : super._(); GraphQLEnumType(this.name, this.values, {this.description}) : super._();
@ -11,7 +18,8 @@ class GraphQLEnumType extends _GraphQLStringType {
ValidationResult<String> validate(String key, String input) { ValidationResult<String> validate(String key, String input) {
var result = super.validate(key, 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() return result._asFailure()
..errors.add( ..errors.add(
'"${result.value}" is not a valid value for the enum "$name".'); '"${result.value}" is not a valid value for the enum "$name".');
@ -20,3 +28,15 @@ class GraphQLEnumType extends _GraphQLStringType {
return result; 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;
}

View file

@ -2,7 +2,7 @@ import 'package:graphql_schema/graphql_schema.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
void main() { void main() {
var typeType = new GraphQLEnumType('Type', [ var typeType = enumType('Type', [
'FIRE', 'FIRE',
'WATER', 'WATER',
'GRASS', 'GRASS',