diff --git a/graphql_schema/lib/src/scalar.dart b/graphql_schema/lib/src/scalar.dart index 2d5941fb..5dd0d1b2 100644 --- a/graphql_schema/lib/src/scalar.dart +++ b/graphql_schema/lib/src/scalar.dart @@ -10,7 +10,7 @@ final GraphQLScalarType 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 human‐readable. -final GraphQLScalarType graphQLId = new _GraphQLStringType._(); +final GraphQLScalarType graphQLId = new _GraphQLStringType._('ID'); /// A [DateTime]. final GraphQLScalarType graphQLDate = @@ -91,10 +91,9 @@ class _GraphQLNumType extends GraphQLScalarType { } class _GraphQLStringType extends GraphQLScalarType { - _GraphQLStringType._(); + final String name; - @override - String get name => 'String'; + _GraphQLStringType._([this.name = 'String']); @override String get description => 'A character sequence.'; diff --git a/graphql_schema/lib/src/schema.dart b/graphql_schema/lib/src/schema.dart index 3fcc7a52..5c61a3b7 100644 --- a/graphql_schema/lib/src/schema.dart +++ b/graphql_schema/lib/src/schema.dart @@ -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()}); } diff --git a/graphql_server/lib/mirrors.dart b/graphql_server/lib/mirrors.dart index 21bfb2be..8f8771b3 100644 --- a/graphql_server/lib/mirrors.dart +++ b/graphql_server/lib/mirrors.dart @@ -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 metadata) { return null; } + +GraphQLType _getProvidedType(List metadata) { + for (var obj in metadata) { + if (obj.reflectee is GraphQLDocumentation) { + return (obj.reflectee as GraphQLDocumentation).type(); + } + } + + return null; +}