platform/packages/graphql/angel_graphql/lib/src/resolvers.dart

144 lines
5.8 KiB
Dart
Raw Normal View History

2018-08-02 17:02:00 +00:00
import 'package:angel_framework/angel_framework.dart';
import 'package:graphql_schema/graphql_schema.dart';
Map<String, dynamic> _fetchRequestInfo(Map<String, dynamic> arguments) {
2018-11-01 21:27:36 +00:00
return <String, dynamic>{
'__requestctx': arguments.remove('__requestctx'),
'__responsectx': arguments.remove('__responsectx'),
};
}
2019-04-11 17:46:04 +00:00
Map<String, dynamic> _getQuery(Map<String, dynamic> arguments) {
var f = Map<String, dynamic>.from(arguments)..remove('id')..remove('data');
2019-09-26 04:45:47 +00:00
return f.isEmpty ? null : f;
2019-04-11 17:46:04 +00:00
}
2018-08-03 23:30:19 +00:00
/// A GraphQL resolver that `index`es an Angel service.
2018-08-02 17:02:00 +00:00
///
2018-08-04 19:18:53 +00:00
/// The arguments passed to the resolver will be forwarded to the service, and the
2018-08-02 17:02:00 +00:00
/// service will receive [Providers.graphql].
2018-11-02 00:29:47 +00:00
GraphQLFieldResolver<List<Value>, Serialized>
resolveViaServiceIndex<Value, Serialized>(Service<dynamic, Value> service) {
2018-08-02 17:02:00 +00:00
return (_, arguments) async {
var _requestInfo = _fetchRequestInfo(arguments);
2019-04-11 17:46:04 +00:00
var params = {'query': _getQuery(arguments), 'provider': Providers.graphQL}
2018-11-01 21:27:36 +00:00
..addAll(_requestInfo);
2018-08-02 17:02:00 +00:00
2018-11-02 00:29:47 +00:00
return await service.index(params);
2018-08-02 17:02:00 +00:00
};
}
2018-08-03 23:30:19 +00:00
2018-08-04 19:18:53 +00:00
/// A GraphQL resolver that calls `findOne` on an Angel service.
///
/// The arguments passed to the resolver will be forwarded to the service, and the
/// service will receive [Providers.graphql].
GraphQLFieldResolver<Value, Serialized>
2018-11-02 00:29:47 +00:00
resolveViaServiceFindOne<Value, Serialized>(
Service<dynamic, Value> service) {
2018-08-04 19:18:53 +00:00
return (_, arguments) async {
var _requestInfo = _fetchRequestInfo(arguments);
2019-04-11 17:46:04 +00:00
var params = {'query': _getQuery(arguments), 'provider': Providers.graphQL}
2018-11-01 21:27:36 +00:00
..addAll(_requestInfo);
2018-11-02 00:29:47 +00:00
return await service.findOne(params);
2018-08-04 19:18:53 +00:00
};
}
2018-08-03 23:30:19 +00:00
/// A GraphQL resolver that `read`s a single value from an Angel service.
///
2018-08-04 19:18:53 +00:00
/// This resolver should be used on a field with at least the following inputs:
/// * `id`: a [graphQLId] or [graphQLString]
///
/// The arguments passed to the resolver will be forwarded to the service, and the
2018-08-03 23:30:19 +00:00
/// service will receive [Providers.graphql].
GraphQLFieldResolver<Value, Serialized>
2018-11-02 00:29:47 +00:00
resolveViaServiceRead<Value, Serialized>(Service<dynamic, Value> service,
2019-04-24 17:56:11 +00:00
{String idField = 'id'}) {
2018-08-03 23:30:19 +00:00
return (_, arguments) async {
var _requestInfo = _fetchRequestInfo(arguments);
2019-04-11 17:46:04 +00:00
var params = {'query': _getQuery(arguments), 'provider': Providers.graphQL}
2018-11-01 21:27:36 +00:00
..addAll(_requestInfo);
2018-08-03 23:30:19 +00:00
var id = arguments.remove(idField);
2018-11-02 00:29:47 +00:00
return await service.read(id, params);
2018-08-03 23:30:19 +00:00
};
}
2018-08-04 19:18:53 +00:00
2019-04-18 00:52:26 +00:00
/// A GraphQL resolver that `creates` a single value in an Angel service.
///
/// This resolver should be used on a field with at least the following input:
/// * `data`: a [GraphQLObjectType] corresponding to the format of `data` to be passed to `create`
///
/// The arguments passed to the resolver will be forwarded to the service, and the
/// service will receive [Providers.graphql].
GraphQLFieldResolver<Value, Serialized>
resolveViaServiceCreate<Value, Serialized>(
Service<dynamic, Value> service) {
return (_, arguments) async {
var _requestInfo = _fetchRequestInfo(arguments);
var params = {'query': _getQuery(arguments), 'provider': Providers.graphQL}
..addAll(_requestInfo);
return await service.create(arguments['data'] as Value, params);
};
}
2018-08-04 19:18:53 +00:00
/// A GraphQL resolver that `modifies` a single value from an Angel service.
///
/// This resolver should be used on a field with at least the following inputs:
/// * `id`: a [graphQLId] or [graphQLString]
/// * `data`: a [GraphQLObjectType] corresponding to the format of `data` to be passed to `modify`
///
/// The arguments passed to the resolver will be forwarded to the service, and the
/// service will receive [Providers.graphql].
GraphQLFieldResolver<Value, Serialized>
2018-11-02 00:29:47 +00:00
resolveViaServiceModify<Value, Serialized>(Service<dynamic, Value> service,
2019-04-24 17:56:11 +00:00
{String idField = 'id'}) {
2018-08-04 19:18:53 +00:00
return (_, arguments) async {
var _requestInfo = _fetchRequestInfo(arguments);
2019-04-11 17:46:04 +00:00
var params = {'query': _getQuery(arguments), 'provider': Providers.graphQL}
2018-11-01 21:27:36 +00:00
..addAll(_requestInfo);
2018-08-04 19:18:53 +00:00
var id = arguments.remove(idField);
2018-11-02 00:29:47 +00:00
return await service.modify(id, arguments['data'] as Value, params);
2018-08-04 19:18:53 +00:00
};
}
/// A GraphQL resolver that `update`s a single value from an Angel service.
///
/// This resolver should be used on a field with at least the following inputs:
/// * `id`: a [graphQLId] or [graphQLString]
/// * `data`: a [GraphQLObjectType] corresponding to the format of `data` to be passed to `update`
///
/// The arguments passed to the resolver will be forwarded to the service, and the
/// service will receive [Providers.graphql].
///
/// Keep in mind that `update` **overwrites** existing contents.
/// To avoid this, use [resolveViaServiceModify] instead.
GraphQLFieldResolver<Value, Serialized>
2018-11-02 00:29:47 +00:00
resolveViaServiceUpdate<Value, Serialized>(Service<dynamic, Value> service,
2019-04-24 17:56:11 +00:00
{String idField = 'id'}) {
2018-08-04 19:18:53 +00:00
return (_, arguments) async {
var _requestInfo = _fetchRequestInfo(arguments);
2019-04-11 17:46:04 +00:00
var params = {'query': _getQuery(arguments), 'provider': Providers.graphQL}
2018-11-01 21:27:36 +00:00
..addAll(_requestInfo);
2018-08-04 19:18:53 +00:00
var id = arguments.remove(idField);
2018-11-02 00:29:47 +00:00
return await service.update(id, arguments['data'] as Value, params);
2018-08-04 19:18:53 +00:00
};
}
/// A GraphQL resolver that `remove`s a single value from an Angel service.
///
/// This resolver should be used on a field with at least the following inputs:
/// * `id`: a [graphQLId] or [graphQLString]
///
/// The arguments passed to the resolver will be forwarded to the service, and the
/// service will receive [Providers.graphql].
GraphQLFieldResolver<Value, Serialized>
2018-11-02 00:29:47 +00:00
resolveViaServiceRemove<Value, Serialized>(Service<dynamic, Value> service,
2019-04-24 17:56:11 +00:00
{String idField = 'id'}) {
2018-08-04 19:18:53 +00:00
return (_, arguments) async {
var _requestInfo = _fetchRequestInfo(arguments);
2019-04-11 17:46:04 +00:00
var params = {'query': _getQuery(arguments), 'provider': Providers.graphQL}
2018-11-01 21:27:36 +00:00
..addAll(_requestInfo);
2018-08-04 19:18:53 +00:00
var id = arguments.remove(idField);
2018-11-02 00:29:47 +00:00
return await service.remove(id, params);
2018-08-04 19:18:53 +00:00
};
}