From c0ca79b928fe4f30cdcafc7f1119a393299962b3 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 4 Aug 2018 15:56:51 -0400 Subject: [PATCH] Inject request + response into services via GraphQL --- angel_graphql/lib/src/resolvers.dart | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/angel_graphql/lib/src/resolvers.dart b/angel_graphql/lib/src/resolvers.dart index 57096a66..6556bfe7 100644 --- a/angel_graphql/lib/src/resolvers.dart +++ b/angel_graphql/lib/src/resolvers.dart @@ -1,6 +1,13 @@ import 'package:angel_framework/angel_framework.dart'; import 'package:graphql_schema/graphql_schema.dart'; +Map _fetchRequestInfo(Map arguments) { + return { + '__requestctx': arguments.remove('__requestctx'), + '__responsectx': arguments.remove('__responsectx'), + }; +} + /// A GraphQL resolver that `index`es an Angel service. /// /// The arguments passed to the resolver will be forwarded to the service, and the @@ -8,7 +15,8 @@ import 'package:graphql_schema/graphql_schema.dart'; GraphQLFieldResolver resolveViaServiceIndex(Service service) { return (_, arguments) async { - var params = {'query': arguments, 'provider': Providers.graphql}; + var _requestInfo = _fetchRequestInfo(arguments); + var params = {'query': arguments, 'provider': Providers.graphql}..addAll(_requestInfo); return await service.index(params) as Value; }; @@ -21,7 +29,8 @@ GraphQLFieldResolver GraphQLFieldResolver resolveViaServiceFindOne(Service service) { return (_, arguments) async { - var params = {'query': arguments, 'provider': Providers.graphql}; + var _requestInfo = _fetchRequestInfo(arguments); + var params = {'query': arguments, 'provider': Providers.graphql}..addAll(_requestInfo); return await service.findOne(params) as Value; }; } @@ -37,7 +46,8 @@ GraphQLFieldResolver resolveViaServiceRead(Service service, {String idField: 'id'}) { return (_, arguments) async { - var params = {'query': arguments, 'provider': Providers.graphql}; + var _requestInfo = _fetchRequestInfo(arguments); + var params = {'query': arguments, 'provider': Providers.graphql}..addAll(_requestInfo); var id = arguments.remove(idField); return await service.read(id, params) as Value; }; @@ -55,7 +65,8 @@ GraphQLFieldResolver resolveViaServiceModify(Service service, {String idField: 'id'}) { return (_, arguments) async { - var params = {'provider': Providers.graphql}; + var _requestInfo = _fetchRequestInfo(arguments); + var params = {'query': arguments, 'provider': Providers.graphql}..addAll(_requestInfo); var id = arguments.remove(idField); return await service.modify(id, arguments['data'], params) as Value; }; @@ -76,7 +87,8 @@ GraphQLFieldResolver resolveViaServiceUpdate(Service service, {String idField: 'id'}) { return (_, arguments) async { - var params = {'provider': Providers.graphql}; + var _requestInfo = _fetchRequestInfo(arguments); + var params = {'query': arguments, 'provider': Providers.graphql}..addAll(_requestInfo); var id = arguments.remove(idField); return await service.update(id, arguments['data'], params) as Value; }; @@ -93,7 +105,8 @@ GraphQLFieldResolver resolveViaServiceRemove(Service service, {String idField: 'id'}) { return (_, arguments) async { - var params = {'query': arguments, 'provider': Providers.graphql}; + var _requestInfo = _fetchRequestInfo(arguments); + var params = {'query': arguments, 'provider': Providers.graphql}..addAll(_requestInfo); var id = arguments.remove(idField); return await service.remove(id, params) as Value; };