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 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].
final GraphQLScalarType<DateTime, String> graphQLDate =
@ -91,10 +91,9 @@ class _GraphQLNumType<T extends num> extends GraphQLScalarType<T, T> {
}
class _GraphQLStringType extends GraphQLScalarType<String, String> {
_GraphQLStringType._();
final String name;
@override
String get name => 'String';
_GraphQLStringType._([this.name = 'String']);
@override
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`.
class GraphQLDocumentation {
final String description;
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(
Symbol name, MethodMirror mirror, Exclude exclude, ClassMirror clazz) {
var type = convertDartType(mirror.returnType.reflectedType,
mirror.returnType.typeArguments.map((t) => t.reflectedType).toList());
var type = _getProvidedType(mirror.metadata);
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 defaultValue = _getDefaultValue(mirror);
if (nameString == 'id' && _autoNames(clazz)) {
if (!wasProvided && (nameString == 'id' && _autoNames(clazz))) {
type = graphQLId;
}
@ -262,3 +267,13 @@ String _getDescription(List<InstanceMirror> metadata) {
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;
}