Finish port to Angel 2

This commit is contained in:
Tobe O 2019-03-28 21:33:21 -04:00
parent 715bc1371e
commit 9c876a643c
7 changed files with 40 additions and 32 deletions

View file

@ -10,13 +10,13 @@ import 'package:star_wars/star_wars.dart' as star_wars;
main() async { main() async {
Future<Angel> createServer() async { Future<Angel> createServer() async {
var app = new Angel(); var app = new Angel();
app.logger = new Logger('star_wars')..onRecord.listen(star_wars.prettyLog); hierarchicalLoggingEnabled = true;
app.logger = new Logger.detached('star_wars')
..onRecord.listen(star_wars.prettyLog);
await app.configure(star_wars.configureServer); await app.configure(star_wars.configureServer);
return app; return app;
} }
hierarchicalLoggingEnabled = true;
var hot = new HotReloader(createServer, [new Directory('lib')]); var hot = new HotReloader(createServer, [new Directory('lib')]);
var server = await hot.startServer('127.0.0.1', 3000); var server = await hot.startServer('127.0.0.1', 3000);

View file

@ -1,7 +1,7 @@
import 'package:angel_model/angel_model.dart'; import 'package:angel_model/angel_model.dart';
import 'episode.dart'; import 'episode.dart';
abstract class Character extends Model { abstract class Character {
String get id; String get id;
String get name; String get name;

View file

@ -87,9 +87,7 @@ abstract class DroidSerializer {
? (map['appears_in'] as Iterable).cast<dynamic>().toList() ? (map['appears_in'] as Iterable).cast<dynamic>().toList()
: null, : null,
friends: map['friends'] is Iterable friends: map['friends'] is Iterable
? new List.unmodifiable(((map['friends'] as Iterable) ? (map['friends'] as Iterable).cast<Character>().toList()
.where((x) => x is Map) as Iterable<Map>)
.map(CharacterSerializer.fromMap))
: null, : null,
createdAt: map['created_at'] != null createdAt: map['created_at'] != null
? (map['created_at'] is DateTime ? (map['created_at'] is DateTime
@ -111,8 +109,7 @@ abstract class DroidSerializer {
'id': model.id, 'id': model.id,
'name': model.name, 'name': model.name,
'appears_in': model.appearsIn, 'appears_in': model.appearsIn,
'friends': 'friends': model.friends,
model.friends?.map((m) => CharacterSerializer.toMap(m))?.toList(),
'created_at': model.createdAt?.toIso8601String(), 'created_at': model.createdAt?.toIso8601String(),
'updated_at': model.updatedAt?.toIso8601String() 'updated_at': model.updatedAt?.toIso8601String()
}; };
@ -120,7 +117,7 @@ abstract class DroidSerializer {
} }
abstract class DroidFields { abstract class DroidFields {
static const List<String> allFields = const <String>[ static const List<String> allFields = <String>[
id, id,
name, name,
appearsIn, appearsIn,

View file

@ -14,7 +14,7 @@ class Human extends _Human {
List<dynamic> appearsIn, List<dynamic> appearsIn,
List<Character> friends, List<Character> friends,
this.totalCredits, this.totalCredits,
List<Starship> starships, List<dynamic> starships,
this.createdAt, this.createdAt,
this.updatedAt}) this.updatedAt})
: this.appearsIn = new List.unmodifiable(appearsIn ?? []), : this.appearsIn = new List.unmodifiable(appearsIn ?? []),
@ -37,7 +37,7 @@ class Human extends _Human {
final int totalCredits; final int totalCredits;
@override @override
final List<Starship> starships; final List<dynamic> starships;
@override @override
final DateTime createdAt; final DateTime createdAt;
@ -51,7 +51,7 @@ class Human extends _Human {
List<dynamic> appearsIn, List<dynamic> appearsIn,
List<Character> friends, List<Character> friends,
int totalCredits, int totalCredits,
List<Starship> starships, List<dynamic> starships,
DateTime createdAt, DateTime createdAt,
DateTime updatedAt}) { DateTime updatedAt}) {
return new Human( return new Human(
@ -74,7 +74,7 @@ class Human extends _Human {
const ListEquality<Character>(const DefaultEquality<Character>()) const ListEquality<Character>(const DefaultEquality<Character>())
.equals(other.friends, friends) && .equals(other.friends, friends) &&
other.totalCredits == totalCredits && other.totalCredits == totalCredits &&
const ListEquality<Starship>(const DefaultEquality<Starship>()) const ListEquality<dynamic>(const DefaultEquality())
.equals(other.starships, starships) && .equals(other.starships, starships) &&
other.createdAt == createdAt && other.createdAt == createdAt &&
other.updatedAt == updatedAt; other.updatedAt == updatedAt;
@ -116,9 +116,7 @@ abstract class HumanSerializer {
: null, : null,
totalCredits: map['total_credits'] as int, totalCredits: map['total_credits'] as int,
starships: map['starships'] is Iterable starships: map['starships'] is Iterable
? new List.unmodifiable(((map['starships'] as Iterable) ? (map['starships'] as Iterable).cast<dynamic>().toList()
.where((x) => x is Map) as Iterable<Map>)
.map(StarshipSerializer.fromMap))
: null, : null,
createdAt: map['created_at'] != null createdAt: map['created_at'] != null
? (map['created_at'] is DateTime ? (map['created_at'] is DateTime
@ -142,8 +140,7 @@ abstract class HumanSerializer {
'appears_in': model.appearsIn, 'appears_in': model.appearsIn,
'friends': model.friends, 'friends': model.friends,
'total_credits': model.totalCredits, 'total_credits': model.totalCredits,
'starships': 'starships': model.starships,
model.starships?.map((m) => StarshipSerializer.toMap(m))?.toList(),
'created_at': model.createdAt?.toIso8601String(), 'created_at': model.createdAt?.toIso8601String(),
'updated_at': model.updatedAt?.toIso8601String() 'updated_at': model.updatedAt?.toIso8601String()
}; };
@ -151,7 +148,7 @@ abstract class HumanSerializer {
} }
abstract class HumanFields { abstract class HumanFields {
static const List<String> allFields = const <String>[ static const List<String> allFields = <String>[
id, id,
name, name,
appearsIn, appearsIn,

View file

@ -95,7 +95,7 @@ abstract class StarshipSerializer {
} }
abstract class StarshipFields { abstract class StarshipFields {
static const List<String> allFields = const <String>[ static const List<String> allFields = <String>[
id, id,
name, name,
length, length,

View file

@ -1,8 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'dart:math'; import 'dart:math';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_graphql/angel_graphql.dart'; import 'package:angel_graphql/angel_graphql.dart';
import 'package:angel_typed_service/angel_typed_service.dart';
import 'package:graphql_schema/graphql_schema.dart'; import 'package:graphql_schema/graphql_schema.dart';
import 'package:graphql_server/graphql_server.dart'; import 'package:graphql_server/graphql_server.dart';
import 'package:graphql_server/mirrors.dart'; import 'package:graphql_server/mirrors.dart';
@ -15,7 +15,7 @@ Future configureServer(Angel app) async {
var droidService = mountService<Droid>(app, '/api/droids'); var droidService = mountService<Droid>(app, '/api/droids');
var humansService = mountService<Human>(app, '/api/humans'); var humansService = mountService<Human>(app, '/api/humans');
var starshipService = mountService<Starship>(app, '/api/starships'); var starshipService = mountService<Starship>(app, '/api/starships');
var rnd = new Random(); var rnd = Random();
// Create the GraphQL schema. // Create the GraphQL schema.
// This code uses dart:mirrors to easily create GraphQL types from Dart PODO's. // This code uses dart:mirrors to easily create GraphQL types from Dart PODO's.
@ -25,7 +25,7 @@ Future configureServer(Angel app) async {
var starshipType = convertDartType(Starship); var starshipType = convertDartType(Starship);
// A Hero can be either a Droid or Human; create a union type that represents this. // A Hero can be either a Droid or Human; create a union type that represents this.
var heroType = new GraphQLUnionType('Hero', [droidType, humanType]); var heroType = GraphQLUnionType('Hero', [droidType, humanType]);
// Create the query type. // Create the query type.
// //
@ -59,7 +59,7 @@ Future configureServer(Angel app) async {
description: description:
'Finds a random hero within the known galaxy, whether a Droid or Human.', 'Finds a random hero within the known galaxy, whether a Droid or Human.',
inputs: [ inputs: [
new GraphQLFieldInput('ep', episodeType), GraphQLFieldInput('ep', episodeType),
], ],
resolve: randomHeroResolver(droidService, humansService, rnd), resolve: randomHeroResolver(droidService, humansService, rnd),
), ),
@ -80,8 +80,8 @@ Future configureServer(Angel app) async {
humanType.nonNullable(), humanType.nonNullable(),
description: 'Modifies a human in the database.', description: 'Modifies a human in the database.',
inputs: [ inputs: [
new GraphQLFieldInput('id', graphQLId.nonNullable()), GraphQLFieldInput('id', graphQLId.nonNullable()),
new GraphQLFieldInput('data', humanChangesType.nonNullable()), GraphQLFieldInput('data', humanChangesType.nonNullable()),
], ],
resolve: resolveViaServiceModify(humansService), resolve: resolveViaServiceModify(humansService),
), ),
@ -96,7 +96,7 @@ Future configureServer(Angel app) async {
// Next, create a GraphQL object, which will be passed to `graphQLHttp`, and // Next, create a GraphQL object, which will be passed to `graphQLHttp`, and
// used to mount a spec-compliant GraphQL endpoint on the server. // used to mount a spec-compliant GraphQL endpoint on the server.
var graphQL = new GraphQL(schema); var graphQL = GraphQL(schema);
// Mount the GraphQL endpoint. // Mount the GraphQL endpoint.
app.all('/graphql', graphQLHttp(graphQL)); app.all('/graphql', graphQLHttp(graphQL));
@ -135,12 +135,16 @@ Future configureServer(Angel app) async {
}); });
} }
Service<String, dynamic> mountService<T extends Model>(
Angel app, String path) =>
app.use(path, TypedService<String, T>(MapService()));
GraphQLFieldResolver randomHeroResolver( GraphQLFieldResolver randomHeroResolver(
Service droidService, Service humansService, Random rnd) { Service droidService, Service humansService, Random rnd) {
return (_, args) async { return (_, args) async {
var allHeroes = []; var allHeroes = [];
var allDroids = await droidService.index() as Iterable; var allDroids = await droidService.index();
var allHumans = await humansService.index() as Iterable; var allHumans = await humansService.index();
allHeroes..addAll(allDroids)..addAll(allHumans); allHeroes..addAll(allDroids)..addAll(allHumans);
// Ignore the annoying cast here, hopefully Dart 2 fixes cases like this // Ignore the annoying cast here, hopefully Dart 2 fixes cases like this
@ -152,4 +156,4 @@ GraphQLFieldResolver randomHeroResolver(
return allHeroes.isEmpty ? null : allHeroes[rnd.nextInt(allHeroes.length)]; return allHeroes.isEmpty ? null : allHeroes[rnd.nextInt(allHeroes.length)];
}; };
} }

View file

@ -6,7 +6,17 @@ dependencies:
path: ../angel_graphql path: ../angel_graphql
angel_hot: ^2.0.0-alpha angel_hot: ^2.0.0-alpha
angel_serialize: ^2.0.0 angel_serialize: ^2.0.0
angel_typed_service: ^1.0.0
io: ^0.3.2 io: ^0.3.2
dev_dependencies: dev_dependencies:
angel_serialize_generator: ^2.0.0 angel_serialize_generator: ^2.0.0
build_runner: ^1.0.0 build_runner: ^1.0.0
graphql_generator:
path: ../graphql_generator
dependency_overrides:
graphql_parser:
path: ../graphql_parser
graphql_schema:
path: ../graphql_schema
graphql_server:
path: ../graphql_server