platform/packages/graphql/angel_graphql/example/main.dart

106 lines
3 KiB
Dart
Raw Permalink Normal View History

2019-04-24 17:56:11 +00:00
// ignore_for_file: deprecated_member_use
2018-08-02 17:02:00 +00:00
import 'package:angel_framework/angel_framework.dart';
2018-11-01 21:27:36 +00:00
import 'package:angel_framework/http.dart';
2018-08-02 17:02:00 +00:00
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 {
2019-04-11 16:49:21 +00:00
var logger = Logger('angel_graphql');
var app = Angel(
logger: logger
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
}));
var http = AngelHttp(app);
2018-08-02 17:02:00 +00:00
2019-04-11 16:49:21 +00:00
var todoService = app.use('api/todos', MapService());
2018-08-02 17:02:00 +00:00
var queryType = objectType(
2018-08-03 18:59:31 +00:00
'Query',
description: 'A simple API that manages your to-do list.',
fields: [
field(
2018-08-03 23:30:19 +00:00
'todos',
2018-08-05 01:49:13 +00:00
listOf(convertDartType(Todo).nonNullable()),
2018-08-03 23:30:19 +00:00
resolve: resolveViaServiceIndex(todoService),
),
field(
'todo',
2018-08-04 19:18:53 +00:00
convertDartType(Todo),
2018-08-03 23:30:19 +00:00
resolve: resolveViaServiceRead(todoService),
2018-08-04 19:18:53 +00:00
inputs: [
2019-04-11 16:49:21 +00:00
GraphQLFieldInput('id', graphQLId.nonNullable()),
2018-08-03 18:59:31 +00:00
],
),
],
);
2018-08-02 17:02:00 +00:00
var mutationType = objectType(
'Mutation',
description: 'Modify the to-do list.',
fields: [
field(
2019-04-24 19:12:18 +00:00
'createTodo',
convertDartType(Todo),
inputs: [
GraphQLFieldInput(
'data', convertDartType(Todo).coerceToInputObject()),
],
resolve: resolveViaServiceCreate(todoService),
),
],
);
var schema = graphQLSchema(
2018-08-04 19:18:53 +00:00
queryType: queryType,
mutationType: mutationType,
);
2018-08-02 17:02:00 +00:00
2019-04-11 16:49:21 +00:00
app.all('/graphql', graphQLHttp(GraphQL(schema)));
app.get('/graphiql', graphiQL());
2018-08-02 17:02:00 +00:00
2018-08-03 23:30:19 +00:00
await todoService
.create({'text': 'Clean your room!', 'completion_status': 'COMPLETE'});
2018-08-03 18:59:31 +00:00
await todoService.create(
2018-08-03 23:30:19 +00:00
{'text': 'Take out the trash', 'completion_status': 'INCOMPLETE'});
await todoService.create({
'text': 'Become a billionaire at the age of 5',
'completion_status': 'INCOMPLETE'
});
2018-08-02 17:02:00 +00:00
var server = await http.startServer('127.0.0.1', 3000);
var uri =
2019-04-11 16:49:21 +00:00
Uri(scheme: 'http', host: server.address.address, port: server.port);
2018-08-02 17:02:00 +00:00
var graphiqlUri = uri.replace(path: 'graphiql');
print('Listening at $uri');
print('Access graphiql at $graphiqlUri');
}
2018-08-02 17:19:30 +00:00
@GraphQLDocumentation(description: 'Any object with a .text (String) property.')
abstract class HasText {
String get text;
}
2018-08-02 17:19:30 +00:00
@serializable
2018-08-03 22:54:12 +00:00
@GraphQLDocumentation(
description: 'A task that might not be completed yet. **Yay! Markdown!**')
class Todo extends Model implements HasText {
2018-08-02 17:19:30 +00:00
String text;
2018-08-03 22:54:12 +00:00
@GraphQLDocumentation(deprecationReason: 'Use `completion_status` instead.')
2018-08-02 17:19:30 +00:00
bool completed;
2018-08-03 22:54:12 +00:00
CompletionStatus completionStatus;
Todo({this.text, this.completed, this.completionStatus});
2018-08-02 17:19:30 +00:00
}
2018-08-03 22:54:12 +00:00
2018-08-03 23:09:16 +00:00
@GraphQLDocumentation(description: 'The completion status of a to-do item.')
2018-08-03 23:30:19 +00:00
enum CompletionStatus { COMPLETE, INCOMPLETE }