Exclude values from mirror enums

This commit is contained in:
Tobe O 2018-08-03 19:09:16 -04:00
parent b7dea3f2e0
commit b32893b8c0
3 changed files with 61 additions and 24 deletions

View file

@ -66,4 +66,6 @@ class Todo extends Model {
Todo({this.text, this.completed, this.completionStatus}); Todo({this.text, this.completed, this.completionStatus});
} }
enum CompletionStatus { complete, incomplete } @GraphQLDocumentation(description: 'The completion status of a to-do item.')
enum CompletionStatus {
COMPLETE, INCOMPLETE }

View file

@ -3,7 +3,7 @@ import 'package:graphql_schema/graphql_schema.dart';
// TODO: How to handle custom types??? // TODO: How to handle custom types???
GraphQLSchema reflectSchema(GraphQLSchema schema, List<GraphQLType> allTypes) { GraphQLSchema reflectSchema(GraphQLSchema schema, List<GraphQLType> allTypes) {
var objectTypes = fetchAllTypes(schema); var objectTypes = fetchAllTypes(schema, allTypes);
var typeType = _reflectSchemaTypes(); var typeType = _reflectSchemaTypes();
var directiveType = _reflectDirectiveType(); var directiveType = _reflectDirectiveType();
allTypes.addAll(objectTypes); allTypes.addAll(objectTypes);
@ -46,6 +46,8 @@ GraphQLSchema reflectSchema(GraphQLSchema schema, List<GraphQLType> allTypes) {
directiveType, directiveType,
typeType, typeType,
schemaType, schemaType,
_typeKindType,
_directiveLocationType,
_reflectFields(), _reflectFields(),
_reflectDirectiveType(), _reflectDirectiveType(),
_reflectInputValueType(), _reflectInputValueType(),
@ -66,7 +68,7 @@ GraphQLSchema reflectSchema(GraphQLSchema schema, List<GraphQLType> allTypes) {
], ],
resolve: (_, args) { resolve: (_, args) {
var name = args['name'] as String; var name = args['name'] as String;
return objectTypes.firstWhere((t) => t.name == name, return allTypes.firstWhere((t) => t.name == name,
orElse: () => throw new GraphQLException.fromMessage( orElse: () => throw new GraphQLException.fromMessage(
'No type named "$name" exists.')); 'No type named "$name" exists.'));
}, },
@ -156,6 +158,18 @@ GraphQLObjectType _reflectSchemaTypes() {
return _typeType; return _typeType;
} }
final GraphQLEnumType<String> _typeKindType =
enumTypeFromStrings('__TypeKind', [
'SCALAR',
'OBJECT',
'INTERFACE',
'UNION',
'ENUM',
'INPUT_OBJECT',
'LIST',
'NON_NULL'
]);
GraphQLObjectType _createTypeType() { GraphQLObjectType _createTypeType() {
var enumValueType = _reflectEnumValueType(); var enumValueType = _reflectEnumValueType();
var fieldType = _reflectFields(); var fieldType = _reflectFields();
@ -174,7 +188,7 @@ GraphQLObjectType _createTypeType() {
), ),
field( field(
'kind', 'kind',
type: graphQLString, type: _typeKindType,
resolve: (type, _) { resolve: (type, _) {
var t = type as GraphQLType; var t = type as GraphQLType;
@ -186,6 +200,8 @@ GraphQLObjectType _createTypeType() {
return 'LIST'; return 'LIST';
else if (t is GraphQLNonNullableType) else if (t is GraphQLNonNullableType)
return 'NON_NULL'; return 'NON_NULL';
else if (t is GraphQLEnumType)
return 'ENUM';
else else
throw new UnsupportedError( throw new UnsupportedError(
'Cannot get the kind of $t.'); // TODO: Interface + union 'Cannot get the kind of $t.'); // TODO: Interface + union
@ -218,6 +234,16 @@ GraphQLObjectType _createTypeType() {
defaultValue: false, defaultValue: false,
), ),
], ],
resolve: (obj, args) {
if (obj is GraphQLEnumType) {
return obj.values
.where(
(f) => !f.isDeprecated || args['includeDeprecated'] == true)
.toList();
} else {
return null;
}
},
), ),
field( field(
'inputFields', 'inputFields',
@ -292,6 +318,16 @@ GraphQLObjectType _reflectInputValueType() {
GraphQLObjectType _directiveType; GraphQLObjectType _directiveType;
final GraphQLEnumType<String> _directiveLocationType =
enumTypeFromStrings('__DirectiveLocation', [
'QUERY',
'MUTATION',
'FIELD',
'FRAGMENT_DEFINITION',
'FRAGMENT_SPREAD',
'INLINE_FRAGMENT'
]);
GraphQLObjectType _reflectDirectiveType() { GraphQLObjectType _reflectDirectiveType() {
var inputValueType = _reflectInputValueType(); var inputValueType = _reflectInputValueType();
@ -309,15 +345,7 @@ GraphQLObjectType _reflectDirectiveType() {
), ),
field( field(
'locations', 'locations',
type: listType(enumTypeFromStrings('DirectiveLocation', [ type: listType(_directiveLocationType.nonNullable()).nonNullable(),
'QUERY',
'MUTATION',
'FIELD',
'FRAGMENT_DEFINITION',
'FRAGMENT_SPREAD',
'INLINE_FRAGMENT'
]).nonNullable())
.nonNullable(),
// TODO: Fetch directiveLocation // TODO: Fetch directiveLocation
resolve: (obj, _) => <String>[], resolve: (obj, _) => <String>[],
), ),
@ -360,7 +388,8 @@ GraphQLObjectType _reflectEnumValueType() {
); );
} }
List<GraphQLObjectType> fetchAllTypes(GraphQLSchema schema) { List<GraphQLObjectType> fetchAllTypes(
GraphQLSchema schema, List<GraphQLType> allTypes) {
var typess = <GraphQLType>[]; var typess = <GraphQLType>[];
typess.addAll(_fetchAllTypesFromObject(schema.query)); typess.addAll(_fetchAllTypesFromObject(schema.query));
@ -372,7 +401,9 @@ List<GraphQLObjectType> fetchAllTypes(GraphQLSchema schema) {
var types = <GraphQLObjectType>[]; var types = <GraphQLObjectType>[];
for (var type in typess) { for (var type in typess) {
if (type is GraphQLObjectType) types.add(type); if (type is GraphQLObjectType)
types.add(type);
else if (!allTypes.contains(type)) allTypes.add(type);
} }
return types.toSet().toList(); return types.toSet().toList();

View file

@ -79,15 +79,19 @@ GraphQLEnumType enumTypeFromClassMirror(ClassMirror mirror) {
var values = <GraphQLEnumValue>[]; var values = <GraphQLEnumValue>[];
for (var name in mirror.staticMembers.keys) { for (var name in mirror.staticMembers.keys) {
var methodMirror = mirror.staticMembers[name]; if (name != #values) {
values.add( var methodMirror = mirror.staticMembers[name];
new GraphQLEnumValue( values.add(
MirrorSystem.getName(name), new GraphQLEnumValue(
mirror.getField(name).reflectee, MirrorSystem.getName(name),
description: _getDescription(methodMirror.metadata), mirror
deprecationReason: _getDeprecationReason(methodMirror.metadata), .getField(name)
), .reflectee,
); description: _getDescription(methodMirror.metadata),
deprecationReason: _getDeprecationReason(methodMirror.metadata),
),
);
}
} }
return new GraphQLEnumType( return new GraphQLEnumType(