From b7dea3f2e00ff122884d3f41d0624fd83b4bffca Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 3 Aug 2018 18:54:12 -0400 Subject: [PATCH] Finished EnumValue type --- angel_graphql/example/main.dart | 11 +++++-- graphql_schema/lib/src/enum.dart | 3 +- graphql_server/lib/introspection.dart | 44 ++++++++++++++++++++++++--- graphql_server/lib/mirrors.dart | 1 + 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/angel_graphql/example/main.dart b/angel_graphql/example/main.dart index 99f57686..76bc50ae 100644 --- a/angel_graphql/example/main.dart +++ b/angel_graphql/example/main.dart @@ -53,12 +53,17 @@ main() async { } @serializable -@GraphQLDocumentation(description: 'A task that might not be completed yet. **Yay! Markdown!**') +@GraphQLDocumentation( + description: 'A task that might not be completed yet. **Yay! Markdown!**') class Todo extends Model { String text; - @GraphQLDocumentation(deprecationReason: 'Booleans are just *sooo* 2015!') + @GraphQLDocumentation(deprecationReason: 'Use `completion_status` instead.') bool completed; - Todo({this.text, this.completed}); + CompletionStatus completionStatus; + + Todo({this.text, this.completed, this.completionStatus}); } + +enum CompletionStatus { complete, incomplete } diff --git a/graphql_schema/lib/src/enum.dart b/graphql_schema/lib/src/enum.dart index 294ef73f..59c62647 100644 --- a/graphql_schema/lib/src/enum.dart +++ b/graphql_schema/lib/src/enum.dart @@ -47,9 +47,10 @@ class GraphQLEnumType extends GraphQLScalarType class GraphQLEnumValue { final String name; final Value value; + final String description; final String deprecationReason; - GraphQLEnumValue(this.name, this.value, {this.deprecationReason}); + GraphQLEnumValue(this.name, this.value, {this.description, this.deprecationReason}); bool get isDeprecated => deprecationReason != null; diff --git a/graphql_server/lib/introspection.dart b/graphql_server/lib/introspection.dart index c337a178..d0cb48ae 100644 --- a/graphql_server/lib/introspection.dart +++ b/graphql_server/lib/introspection.dart @@ -309,8 +309,16 @@ GraphQLObjectType _reflectDirectiveType() { ), field( 'locations', - type: listType(graphQLString.nonNullable()).nonNullable(), - // TODO: Enum directiveLocation + type: listType(enumTypeFromStrings('DirectiveLocation', [ + 'QUERY', + 'MUTATION', + 'FIELD', + 'FRAGMENT_DEFINITION', + 'FRAGMENT_SPREAD', + 'INLINE_FRAGMENT' + ]).nonNullable()) + .nonNullable(), + // TODO: Fetch directiveLocation resolve: (obj, _) => [], ), field( @@ -324,8 +332,32 @@ GraphQLObjectType _reflectDirectiveType() { GraphQLObjectType _enumValueType; GraphQLObjectType _reflectEnumValueType() { - // TODO: Enum values - return _enumValueType ?? objectType('__EnumValue', fields: []); + return _enumValueType ?? + objectType( + '__EnumValue', + fields: [ + field( + 'name', + type: graphQLString.nonNullable(), + resolve: (obj, _) => (obj as GraphQLEnumValue).name, + ), + field( + 'description', + type: graphQLString, + resolve: (obj, _) => (obj as GraphQLEnumValue).description, + ), + field( + 'isDeprecated', + type: graphQLBoolean.nonNullable(), + resolve: (obj, _) => (obj as GraphQLEnumValue).isDeprecated, + ), + field( + 'deprecationReason', + type: graphQLString, + resolve: (obj, _) => (obj as GraphQLEnumValue).deprecationReason, + ), + ], + ); } List fetchAllTypes(GraphQLSchema schema) { @@ -369,8 +401,10 @@ Iterable _fetchAllTypesFromType(GraphQLType type) { types.addAll(_fetchAllTypesFromType(type.innerType)); } else if (type is GraphQLObjectType) { types.addAll(_fetchAllTypesFromObject(type)); + } else if (type is GraphQLEnumType) { + types.add(type); } - // TODO: Enum, interface, union + // TODO: Interface, union return types; } diff --git a/graphql_server/lib/mirrors.dart b/graphql_server/lib/mirrors.dart index 36e8ef34..8f5bda33 100644 --- a/graphql_server/lib/mirrors.dart +++ b/graphql_server/lib/mirrors.dart @@ -84,6 +84,7 @@ GraphQLEnumType enumTypeFromClassMirror(ClassMirror mirror) { new GraphQLEnumValue( MirrorSystem.getName(name), mirror.getField(name).reflectee, + description: _getDescription(methodMirror.metadata), deprecationReason: _getDeprecationReason(methodMirror.metadata), ), );