This commit is contained in:
Tobe O 2018-08-02 13:19:30 -04:00
parent b3530e5a2a
commit 8f1e7eb7ce
4 changed files with 38 additions and 12 deletions

View file

@ -1,7 +1,9 @@
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_graphql/angel_graphql.dart';
import 'package:angel_serialize/angel_serialize.dart';
import 'package:graphql_schema/graphql_schema.dart';
import 'package:graphql_server/graphql_server.dart';
import 'package:graphql_server/mirrors.dart';
main() async {
var app = new Angel();
@ -9,18 +11,14 @@ main() async {
var todoService = app.use('api/todos', new MapService()) as Service;
var todo = objectType('todo', [
field(
'text',
type: graphQLString,
),
]);
var api = objectType('api', [
field(
'todos',
type: listType(todo),
'todo',
type: listType(objectTypeFromDartType(Todo)),
resolve: resolveFromService(todoService),
arguments: [
new GraphQLFieldArgument('id', graphQLId),
],
),
]);
@ -38,3 +36,12 @@ main() async {
print('Listening at $uri');
print('Access graphiql at $graphiqlUri');
}
@serializable
class Todo extends Model {
String text;
bool completed;
Todo({this.text, this.completed});
}

View file

@ -7,4 +7,6 @@ dependencies:
graphql_schema:
path: ../graphql_schema
graphql_server:
path: ../graphql_server
path: ../graphql_server
dev_dependencies:
angel_serialize: ^2.0.0

View file

@ -3,12 +3,22 @@ import 'dart:mirrors';
import 'package:angel_serialize/angel_serialize.dart';
import 'package:graphql_schema/graphql_schema.dart';
import 'package:recase/recase.dart';
import 'package:tuple/tuple.dart';
/// Reflects upon a given [type] and dynamically generates a [GraphQLType] that corresponds to it.
///
/// This function is aware of the annotations from `package:angel_serialize`, and works seamlessly
/// with them.
GraphQLType objectTypeFromDartType(Type type, [List<Type> typeArguments]) {
var tuple = new Tuple2(type, typeArguments);
return _cache.putIfAbsent(
tuple, () => _objectTypeFromDartType(type, typeArguments));
}
final Map<Tuple2<Type, List<Type>>, GraphQLType> _cache =
<Tuple2<Type, List<Type>>, GraphQLType>{};
GraphQLType _objectTypeFromDartType(Type type, [List<Type> typeArguments]) {
if (type == bool) {
return graphQLBoolean;
} else if (type == int) {
@ -17,9 +27,12 @@ GraphQLType objectTypeFromDartType(Type type, [List<Type> typeArguments]) {
return graphQLFloat;
} else if (type == String) {
return graphQLString;
} else if (type == DateTime) {
return graphQLDate;
}
var mirror = reflectType(type, typeArguments);
var mirror = reflectType(
type, typeArguments?.isNotEmpty == true ? typeArguments : null);
if (mirror is! ClassMirror) {
throw new StateError(
@ -35,7 +48,10 @@ GraphQLObjectType objectTypeFromClassMirror(ClassMirror mirror) {
for (var name in mirror.instanceMembers.keys) {
var methodMirror = mirror.instanceMembers[name];
var exclude = _getExclude(name, methodMirror);
var canAdd = exclude?.canSerialize != true;
var canAdd = name != #hashCode &&
name != #runtimeType &&
!methodMirror.isPrivate &&
exclude?.canSerialize != true;
if (methodMirror.isGetter && canAdd) {
fields.add(fieldFromGetter(name, methodMirror, exclude, mirror));
}

View file

@ -6,5 +6,6 @@ dependencies:
graphql_parser:
path: ../graphql_parser
recase: ^1.0.0
tuple: ^1.0.0
dev_dependencies:
test: ^0.12.0