platform/angel_graphql/example/main.dart

65 lines
2 KiB
Dart
Raw Normal View History

2018-08-02 17:02:00 +00:00
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_graphql/angel_graphql.dart';
2018-08-02 17:19:30 +00:00
import 'package:angel_serialize/angel_serialize.dart';
2018-08-02 17:02:00 +00:00
import 'package:graphql_schema/graphql_schema.dart';
import 'package:graphql_server/graphql_server.dart';
2018-08-02 17:19:30 +00:00
import 'package:graphql_server/mirrors.dart';
2018-08-02 19:22:16 +00:00
import 'package:logging/logging.dart';
2018-08-02 17:02:00 +00:00
main() async {
var app = new Angel();
var http = new AngelHttp(app);
2018-08-02 18:33:50 +00:00
hierarchicalLoggingEnabled = true;
app.logger = new Logger('angel_graphql')
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
2018-08-02 17:02:00 +00:00
var todoService = app.use('api/todos', new MapService()) as Service;
2018-08-03 18:59:31 +00:00
var api = objectType(
'Query',
description: 'A simple API that manages your to-do list.',
fields: [
field(
'todo',
type: listType(objectTypeFromDartType(Todo).nonNullable()),
resolve: resolveFromService(todoService),
arguments: [
new GraphQLFieldArgument('id', graphQLId),
],
),
],
);
2018-08-02 17:02:00 +00:00
var schema = graphQLSchema(query: api);
app.all('/graphql', graphQLHttp(new GraphQL(schema)));
app.get('/graphiql', graphiql());
await todoService.create({'text': 'Clean your room!', 'completed': true});
2018-08-03 17:26:11 +00:00
await todoService.create({'text': 'Take out the trash', 'completed': false});
2018-08-03 18:59:31 +00:00
await todoService.create(
{'text': 'Become a billionaire at the age of 5', 'completed': false});
2018-08-02 17:02:00 +00:00
var server = await http.startServer('127.0.0.1', 3000);
var uri =
new Uri(scheme: 'http', host: server.address.address, port: server.port);
var graphiqlUri = uri.replace(path: 'graphiql');
print('Listening at $uri');
print('Access graphiql at $graphiqlUri');
}
2018-08-02 17:19:30 +00:00
@serializable
2018-08-03 18:59:31 +00:00
@GraphQLDocumentation(description: 'A task that might not be completed yet. **Yay! Markdown!**')
2018-08-02 17:19:30 +00:00
class Todo extends Model {
String text;
@GraphQLDocumentation(deprecationReason: 'Booleans are just *sooo* 2015!')
2018-08-02 17:19:30 +00:00
bool completed;
Todo({this.text, this.completed});
}