diff --git a/angel_graphql/README.md b/angel_graphql/README.md index c660a952..7fac35c2 100644 --- a/angel_graphql/README.md +++ b/angel_graphql/README.md @@ -127,6 +127,53 @@ UI, ready-to-go! Now you're ready to build a GraphQL API! +## Using Services +What would Angel be without services? For those unfamiliar - in Angel, +`Service` is a base class that implements CRUD functionality, and serves +as the database interface within an Angel application. They are well-suited +for NoSQL or other databases without a schema (they can be used with +SQL, but that's not their primary focus). + +`package:angel_graphql` has functionality to resolve fields by interacting with +services. + +Consider our previous example, and note the calls to +`resolveViaServiceIndex` and `resolveViaServiceRead`: + +```dart +var queryType = objectType( + 'Query', + description: 'A simple API that manages your to-do list.', + fields: [ + field( + 'todos', + listOf(convertDartType(Todo).nonNullable()), + resolve: resolveViaServiceIndex(todoService), + ), + field( + 'todo', + convertDartType(Todo), + resolve: resolveViaServiceRead(todoService), + inputs: [ + new GraphQLFieldInput('id', graphQLId.nonNullable()), + ], + ), + ], + ); +``` + +In all, there are: +* `resolveViaServiceIndex` +* `resolveViaServiceFindOne` +* `resolveViaServiceRead` +* `resolveViaServiceModify` +* `resolveViaServiceUpdate` +* `resolveViaServiceRemove` + +As one might imagine, using these convenience helpers makes +it much quicker to implement CRUD functionality in a GraphQL +API. + ## Documentation The `convertDartType` function can automatically read the documentation from a type like the following: diff --git a/angel_graphql/lib/src/resolvers.dart b/angel_graphql/lib/src/resolvers.dart index 5919f543..28120817 100644 --- a/angel_graphql/lib/src/resolvers.dart +++ b/angel_graphql/lib/src/resolvers.dart @@ -12,14 +12,14 @@ Map _fetchRequestInfo(Map arguments) { /// /// The arguments passed to the resolver will be forwarded to the service, and the /// service will receive [Providers.graphql]. -GraphQLFieldResolver - resolveViaServiceIndex(Service service) { +GraphQLFieldResolver, Serialized> + resolveViaServiceIndex(Service service) { return (_, arguments) async { var _requestInfo = _fetchRequestInfo(arguments); var params = {'query': arguments, 'provider': Providers.graphQL} ..addAll(_requestInfo); - return await service.index(params) as Value; + return await service.index(params); }; } @@ -28,12 +28,13 @@ GraphQLFieldResolver /// The arguments passed to the resolver will be forwarded to the service, and the /// service will receive [Providers.graphql]. GraphQLFieldResolver - resolveViaServiceFindOne(Service service) { + resolveViaServiceFindOne( + Service service) { return (_, arguments) async { var _requestInfo = _fetchRequestInfo(arguments); var params = {'query': arguments, 'provider': Providers.graphQL} ..addAll(_requestInfo); - return await service.findOne(params) as Value; + return await service.findOne(params); }; } @@ -45,14 +46,14 @@ GraphQLFieldResolver /// The arguments passed to the resolver will be forwarded to the service, and the /// service will receive [Providers.graphql]. GraphQLFieldResolver - resolveViaServiceRead(Service service, + resolveViaServiceRead(Service service, {String idField: 'id'}) { return (_, arguments) async { 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; + return await service.read(id, params); }; } @@ -65,14 +66,14 @@ GraphQLFieldResolver /// The arguments passed to the resolver will be forwarded to the service, and the /// service will receive [Providers.graphql]. GraphQLFieldResolver - resolveViaServiceModify(Service service, + resolveViaServiceModify(Service service, {String idField: 'id'}) { return (_, arguments) async { 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; + return await service.modify(id, arguments['data'] as Value, params); }; } @@ -88,14 +89,14 @@ GraphQLFieldResolver /// Keep in mind that `update` **overwrites** existing contents. /// To avoid this, use [resolveViaServiceModify] instead. GraphQLFieldResolver - resolveViaServiceUpdate(Service service, + resolveViaServiceUpdate(Service service, {String idField: 'id'}) { return (_, arguments) async { 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; + return await service.update(id, arguments['data'] as Value, params); }; } @@ -107,13 +108,13 @@ GraphQLFieldResolver /// The arguments passed to the resolver will be forwarded to the service, and the /// service will receive [Providers.graphql]. GraphQLFieldResolver - resolveViaServiceRemove(Service service, + resolveViaServiceRemove(Service service, {String idField: 'id'}) { return (_, arguments) async { 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; + return await service.remove(id, params); }; }