Finish port to Angel 2
This commit is contained in:
parent
715bc1371e
commit
9c876a643c
7 changed files with 40 additions and 32 deletions
|
@ -10,13 +10,13 @@ import 'package:star_wars/star_wars.dart' as star_wars;
|
|||
main() async {
|
||||
Future<Angel> createServer() async {
|
||||
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);
|
||||
return app;
|
||||
}
|
||||
|
||||
hierarchicalLoggingEnabled = true;
|
||||
|
||||
var hot = new HotReloader(createServer, [new Directory('lib')]);
|
||||
|
||||
var server = await hot.startServer('127.0.0.1', 3000);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'package:angel_model/angel_model.dart';
|
||||
import 'episode.dart';
|
||||
|
||||
abstract class Character extends Model {
|
||||
abstract class Character {
|
||||
String get id;
|
||||
|
||||
String get name;
|
||||
|
|
|
@ -87,9 +87,7 @@ abstract class DroidSerializer {
|
|||
? (map['appears_in'] as Iterable).cast<dynamic>().toList()
|
||||
: null,
|
||||
friends: map['friends'] is Iterable
|
||||
? new List.unmodifiable(((map['friends'] as Iterable)
|
||||
.where((x) => x is Map) as Iterable<Map>)
|
||||
.map(CharacterSerializer.fromMap))
|
||||
? (map['friends'] as Iterable).cast<Character>().toList()
|
||||
: null,
|
||||
createdAt: map['created_at'] != null
|
||||
? (map['created_at'] is DateTime
|
||||
|
@ -111,8 +109,7 @@ abstract class DroidSerializer {
|
|||
'id': model.id,
|
||||
'name': model.name,
|
||||
'appears_in': model.appearsIn,
|
||||
'friends':
|
||||
model.friends?.map((m) => CharacterSerializer.toMap(m))?.toList(),
|
||||
'friends': model.friends,
|
||||
'created_at': model.createdAt?.toIso8601String(),
|
||||
'updated_at': model.updatedAt?.toIso8601String()
|
||||
};
|
||||
|
@ -120,7 +117,7 @@ abstract class DroidSerializer {
|
|||
}
|
||||
|
||||
abstract class DroidFields {
|
||||
static const List<String> allFields = const <String>[
|
||||
static const List<String> allFields = <String>[
|
||||
id,
|
||||
name,
|
||||
appearsIn,
|
||||
|
|
|
@ -14,7 +14,7 @@ class Human extends _Human {
|
|||
List<dynamic> appearsIn,
|
||||
List<Character> friends,
|
||||
this.totalCredits,
|
||||
List<Starship> starships,
|
||||
List<dynamic> starships,
|
||||
this.createdAt,
|
||||
this.updatedAt})
|
||||
: this.appearsIn = new List.unmodifiable(appearsIn ?? []),
|
||||
|
@ -37,7 +37,7 @@ class Human extends _Human {
|
|||
final int totalCredits;
|
||||
|
||||
@override
|
||||
final List<Starship> starships;
|
||||
final List<dynamic> starships;
|
||||
|
||||
@override
|
||||
final DateTime createdAt;
|
||||
|
@ -51,7 +51,7 @@ class Human extends _Human {
|
|||
List<dynamic> appearsIn,
|
||||
List<Character> friends,
|
||||
int totalCredits,
|
||||
List<Starship> starships,
|
||||
List<dynamic> starships,
|
||||
DateTime createdAt,
|
||||
DateTime updatedAt}) {
|
||||
return new Human(
|
||||
|
@ -74,7 +74,7 @@ class Human extends _Human {
|
|||
const ListEquality<Character>(const DefaultEquality<Character>())
|
||||
.equals(other.friends, friends) &&
|
||||
other.totalCredits == totalCredits &&
|
||||
const ListEquality<Starship>(const DefaultEquality<Starship>())
|
||||
const ListEquality<dynamic>(const DefaultEquality())
|
||||
.equals(other.starships, starships) &&
|
||||
other.createdAt == createdAt &&
|
||||
other.updatedAt == updatedAt;
|
||||
|
@ -116,9 +116,7 @@ abstract class HumanSerializer {
|
|||
: null,
|
||||
totalCredits: map['total_credits'] as int,
|
||||
starships: map['starships'] is Iterable
|
||||
? new List.unmodifiable(((map['starships'] as Iterable)
|
||||
.where((x) => x is Map) as Iterable<Map>)
|
||||
.map(StarshipSerializer.fromMap))
|
||||
? (map['starships'] as Iterable).cast<dynamic>().toList()
|
||||
: null,
|
||||
createdAt: map['created_at'] != null
|
||||
? (map['created_at'] is DateTime
|
||||
|
@ -142,8 +140,7 @@ abstract class HumanSerializer {
|
|||
'appears_in': model.appearsIn,
|
||||
'friends': model.friends,
|
||||
'total_credits': model.totalCredits,
|
||||
'starships':
|
||||
model.starships?.map((m) => StarshipSerializer.toMap(m))?.toList(),
|
||||
'starships': model.starships,
|
||||
'created_at': model.createdAt?.toIso8601String(),
|
||||
'updated_at': model.updatedAt?.toIso8601String()
|
||||
};
|
||||
|
@ -151,7 +148,7 @@ abstract class HumanSerializer {
|
|||
}
|
||||
|
||||
abstract class HumanFields {
|
||||
static const List<String> allFields = const <String>[
|
||||
static const List<String> allFields = <String>[
|
||||
id,
|
||||
name,
|
||||
appearsIn,
|
||||
|
|
|
@ -95,7 +95,7 @@ abstract class StarshipSerializer {
|
|||
}
|
||||
|
||||
abstract class StarshipFields {
|
||||
static const List<String> allFields = const <String>[
|
||||
static const List<String> allFields = <String>[
|
||||
id,
|
||||
name,
|
||||
length,
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:angel_framework/angel_framework.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_server/graphql_server.dart';
|
||||
import 'package:graphql_server/mirrors.dart';
|
||||
|
@ -15,7 +15,7 @@ Future configureServer(Angel app) async {
|
|||
var droidService = mountService<Droid>(app, '/api/droids');
|
||||
var humansService = mountService<Human>(app, '/api/humans');
|
||||
var starshipService = mountService<Starship>(app, '/api/starships');
|
||||
var rnd = new Random();
|
||||
var rnd = Random();
|
||||
|
||||
// Create the GraphQL schema.
|
||||
// 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);
|
||||
|
||||
// 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.
|
||||
//
|
||||
|
@ -59,7 +59,7 @@ Future configureServer(Angel app) async {
|
|||
description:
|
||||
'Finds a random hero within the known galaxy, whether a Droid or Human.',
|
||||
inputs: [
|
||||
new GraphQLFieldInput('ep', episodeType),
|
||||
GraphQLFieldInput('ep', episodeType),
|
||||
],
|
||||
resolve: randomHeroResolver(droidService, humansService, rnd),
|
||||
),
|
||||
|
@ -80,8 +80,8 @@ Future configureServer(Angel app) async {
|
|||
humanType.nonNullable(),
|
||||
description: 'Modifies a human in the database.',
|
||||
inputs: [
|
||||
new GraphQLFieldInput('id', graphQLId.nonNullable()),
|
||||
new GraphQLFieldInput('data', humanChangesType.nonNullable()),
|
||||
GraphQLFieldInput('id', graphQLId.nonNullable()),
|
||||
GraphQLFieldInput('data', humanChangesType.nonNullable()),
|
||||
],
|
||||
resolve: resolveViaServiceModify(humansService),
|
||||
),
|
||||
|
@ -96,7 +96,7 @@ Future configureServer(Angel app) async {
|
|||
|
||||
// Next, create a GraphQL object, which will be passed to `graphQLHttp`, and
|
||||
// used to mount a spec-compliant GraphQL endpoint on the server.
|
||||
var graphQL = new GraphQL(schema);
|
||||
var graphQL = GraphQL(schema);
|
||||
|
||||
// Mount the GraphQL endpoint.
|
||||
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(
|
||||
Service droidService, Service humansService, Random rnd) {
|
||||
return (_, args) async {
|
||||
var allHeroes = [];
|
||||
var allDroids = await droidService.index() as Iterable;
|
||||
var allHumans = await humansService.index() as Iterable;
|
||||
var allDroids = await droidService.index();
|
||||
var allHumans = await humansService.index();
|
||||
allHeroes..addAll(allDroids)..addAll(allHumans);
|
||||
|
||||
// Ignore the annoying cast here, hopefully Dart 2 fixes cases like this
|
||||
|
|
|
@ -6,7 +6,17 @@ dependencies:
|
|||
path: ../angel_graphql
|
||||
angel_hot: ^2.0.0-alpha
|
||||
angel_serialize: ^2.0.0
|
||||
angel_typed_service: ^1.0.0
|
||||
io: ^0.3.2
|
||||
dev_dependencies:
|
||||
angel_serialize_generator: ^2.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
|
Loading…
Reference in a new issue