platform/graphql_schema/example/example.dart

42 lines
850 B
Dart
Raw Normal View History

2018-08-02 13:31:54 +00:00
import 'package:graphql_schema/graphql_schema.dart';
final GraphQLSchema todoSchema = new GraphQLSchema(
2018-08-04 19:18:53 +00:00
queryType: objectType('Todo', fields: [
field(
'text',
2018-08-04 19:18:53 +00:00
graphQLString.nonNullable(),
2018-08-02 15:17:14 +00:00
resolve: resolveToNull,
),
field(
'created_at',
2018-08-04 19:18:53 +00:00
graphQLDate,
2018-08-02 15:17:14 +00:00
resolve: resolveToNull,
),
]),
);
2018-08-02 13:31:54 +00:00
main() {
// Validation
2018-08-04 19:18:53 +00:00
var validation = todoSchema.queryType.validate(
'@root',
2018-08-02 15:17:14 +00:00
{
'foo': 'bar',
'text': null,
'created_at': 24,
},
);
2018-08-02 13:31:54 +00:00
if (validation.successful) {
print('This is valid data!!!');
} else {
print('Invalid data.');
validation.errors.forEach((s) => print(' * $s'));
}
// Serialization
2018-08-04 19:18:53 +00:00
print(todoSchema.queryType.serialize({
2018-08-02 13:31:54 +00:00
'text': 'Clean your room!',
'created_at': new DateTime.now().subtract(new Duration(days: 10))
}));
}