Enable specifying a custom type in @GraphQLDocumentation

This commit is contained in:
Tobe O 2018-08-03 20:12:23 -04:00
parent 10dda84f95
commit a5a4dfe0b5
3 changed files with 26 additions and 8 deletions

View file

@ -10,7 +10,7 @@ final GraphQLScalarType<String, String> graphQLString =
/// The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. /// The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache.
/// ///
/// The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be humanreadable. /// The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be humanreadable.
final GraphQLScalarType<String, String> graphQLId = new _GraphQLStringType._(); final GraphQLScalarType<String, String> graphQLId = new _GraphQLStringType._('ID');
/// A [DateTime]. /// A [DateTime].
final GraphQLScalarType<DateTime, String> graphQLDate = final GraphQLScalarType<DateTime, String> graphQLDate =
@ -91,10 +91,9 @@ class _GraphQLNumType<T extends num> extends GraphQLScalarType<T, T> {
} }
class _GraphQLStringType extends GraphQLScalarType<String, String> { class _GraphQLStringType extends GraphQLScalarType<String, String> {
_GraphQLStringType._(); final String name;
@override _GraphQLStringType._([this.name = 'String']);
String get name => 'String';
@override @override
String get description => 'A character sequence.'; String get description => 'A character sequence.';

View file

@ -100,10 +100,14 @@ class GraphExceptionErrorLocation {
} }
} }
typedef GraphQLType _GraphDocumentationTypeProvider();
/// A metadata annotation used to provide documentation to `package:graphql_server`. /// A metadata annotation used to provide documentation to `package:graphql_server`.
class GraphQLDocumentation { class GraphQLDocumentation {
final String description; final String description;
final String deprecationReason; final String deprecationReason;
final _GraphDocumentationTypeProvider type;
const GraphQLDocumentation({this.description, this.deprecationReason}); const GraphQLDocumentation(
{this.description, this.deprecationReason, GraphQLType this.type()});
} }

View file

@ -151,13 +151,18 @@ GraphQLEnumType enumTypeFromClassMirror(ClassMirror mirror) {
GraphQLField fieldFromGetter( GraphQLField fieldFromGetter(
Symbol name, MethodMirror mirror, Exclude exclude, ClassMirror clazz) { Symbol name, MethodMirror mirror, Exclude exclude, ClassMirror clazz) {
var type = convertDartType(mirror.returnType.reflectedType, var type = _getProvidedType(mirror.metadata);
mirror.returnType.typeArguments.map((t) => t.reflectedType).toList()); var wasProvided = type != null;
if (!wasProvided) {
type = convertDartType(mirror.returnType.reflectedType,
mirror.returnType.typeArguments.map((t) => t.reflectedType).toList());
}
var nameString = _getSerializedName(name, mirror, clazz); var nameString = _getSerializedName(name, mirror, clazz);
var defaultValue = _getDefaultValue(mirror); var defaultValue = _getDefaultValue(mirror);
if (nameString == 'id' && _autoNames(clazz)) { if (!wasProvided && (nameString == 'id' && _autoNames(clazz))) {
type = graphQLId; type = graphQLId;
} }
@ -262,3 +267,13 @@ String _getDescription(List<InstanceMirror> metadata) {
return null; return null;
} }
GraphQLType _getProvidedType(List<InstanceMirror> metadata) {
for (var obj in metadata) {
if (obj.reflectee is GraphQLDocumentation) {
return (obj.reflectee as GraphQLDocumentation).type();
}
}
return null;
}