Add types to resolvers
This commit is contained in:
parent
6a7c36a2d5
commit
22eed666e2
2 changed files with 61 additions and 13 deletions
|
@ -127,6 +127,53 @@ UI, ready-to-go!
|
||||||
|
|
||||||
Now you're ready to build a GraphQL API!
|
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
|
## Documentation
|
||||||
The `convertDartType` function can automatically read the documentation
|
The `convertDartType` function can automatically read the documentation
|
||||||
from a type like the following:
|
from a type like the following:
|
||||||
|
|
|
@ -12,14 +12,14 @@ Map<String, dynamic> _fetchRequestInfo(Map<String, dynamic> arguments) {
|
||||||
///
|
///
|
||||||
/// The arguments passed to the resolver will be forwarded to the service, and the
|
/// The arguments passed to the resolver will be forwarded to the service, and the
|
||||||
/// service will receive [Providers.graphql].
|
/// service will receive [Providers.graphql].
|
||||||
GraphQLFieldResolver<Value, Serialized>
|
GraphQLFieldResolver<List<Value>, Serialized>
|
||||||
resolveViaServiceIndex<Value, Serialized>(Service service) {
|
resolveViaServiceIndex<Value, Serialized>(Service<dynamic, Value> service) {
|
||||||
return (_, arguments) async {
|
return (_, arguments) async {
|
||||||
var _requestInfo = _fetchRequestInfo(arguments);
|
var _requestInfo = _fetchRequestInfo(arguments);
|
||||||
var params = {'query': arguments, 'provider': Providers.graphQL}
|
var params = {'query': arguments, 'provider': Providers.graphQL}
|
||||||
..addAll(_requestInfo);
|
..addAll(_requestInfo);
|
||||||
|
|
||||||
return await service.index(params) as Value;
|
return await service.index(params);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,12 +28,13 @@ GraphQLFieldResolver<Value, Serialized>
|
||||||
/// The arguments passed to the resolver will be forwarded to the service, and the
|
/// The arguments passed to the resolver will be forwarded to the service, and the
|
||||||
/// service will receive [Providers.graphql].
|
/// service will receive [Providers.graphql].
|
||||||
GraphQLFieldResolver<Value, Serialized>
|
GraphQLFieldResolver<Value, Serialized>
|
||||||
resolveViaServiceFindOne<Value, Serialized>(Service service) {
|
resolveViaServiceFindOne<Value, Serialized>(
|
||||||
|
Service<dynamic, Value> service) {
|
||||||
return (_, arguments) async {
|
return (_, arguments) async {
|
||||||
var _requestInfo = _fetchRequestInfo(arguments);
|
var _requestInfo = _fetchRequestInfo(arguments);
|
||||||
var params = {'query': arguments, 'provider': Providers.graphQL}
|
var params = {'query': arguments, 'provider': Providers.graphQL}
|
||||||
..addAll(_requestInfo);
|
..addAll(_requestInfo);
|
||||||
return await service.findOne(params) as Value;
|
return await service.findOne(params);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,14 +46,14 @@ GraphQLFieldResolver<Value, Serialized>
|
||||||
/// The arguments passed to the resolver will be forwarded to the service, and the
|
/// The arguments passed to the resolver will be forwarded to the service, and the
|
||||||
/// service will receive [Providers.graphql].
|
/// service will receive [Providers.graphql].
|
||||||
GraphQLFieldResolver<Value, Serialized>
|
GraphQLFieldResolver<Value, Serialized>
|
||||||
resolveViaServiceRead<Value, Serialized>(Service service,
|
resolveViaServiceRead<Value, Serialized>(Service<dynamic, Value> service,
|
||||||
{String idField: 'id'}) {
|
{String idField: 'id'}) {
|
||||||
return (_, arguments) async {
|
return (_, arguments) async {
|
||||||
var _requestInfo = _fetchRequestInfo(arguments);
|
var _requestInfo = _fetchRequestInfo(arguments);
|
||||||
var params = {'query': arguments, 'provider': Providers.graphQL}
|
var params = {'query': arguments, 'provider': Providers.graphQL}
|
||||||
..addAll(_requestInfo);
|
..addAll(_requestInfo);
|
||||||
var id = arguments.remove(idField);
|
var id = arguments.remove(idField);
|
||||||
return await service.read(id, params) as Value;
|
return await service.read(id, params);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,14 +66,14 @@ GraphQLFieldResolver<Value, Serialized>
|
||||||
/// The arguments passed to the resolver will be forwarded to the service, and the
|
/// The arguments passed to the resolver will be forwarded to the service, and the
|
||||||
/// service will receive [Providers.graphql].
|
/// service will receive [Providers.graphql].
|
||||||
GraphQLFieldResolver<Value, Serialized>
|
GraphQLFieldResolver<Value, Serialized>
|
||||||
resolveViaServiceModify<Value, Serialized>(Service service,
|
resolveViaServiceModify<Value, Serialized>(Service<dynamic, Value> service,
|
||||||
{String idField: 'id'}) {
|
{String idField: 'id'}) {
|
||||||
return (_, arguments) async {
|
return (_, arguments) async {
|
||||||
var _requestInfo = _fetchRequestInfo(arguments);
|
var _requestInfo = _fetchRequestInfo(arguments);
|
||||||
var params = {'query': arguments, 'provider': Providers.graphQL}
|
var params = {'query': arguments, 'provider': Providers.graphQL}
|
||||||
..addAll(_requestInfo);
|
..addAll(_requestInfo);
|
||||||
var id = arguments.remove(idField);
|
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<Value, Serialized>
|
||||||
/// Keep in mind that `update` **overwrites** existing contents.
|
/// Keep in mind that `update` **overwrites** existing contents.
|
||||||
/// To avoid this, use [resolveViaServiceModify] instead.
|
/// To avoid this, use [resolveViaServiceModify] instead.
|
||||||
GraphQLFieldResolver<Value, Serialized>
|
GraphQLFieldResolver<Value, Serialized>
|
||||||
resolveViaServiceUpdate<Value, Serialized>(Service service,
|
resolveViaServiceUpdate<Value, Serialized>(Service<dynamic, Value> service,
|
||||||
{String idField: 'id'}) {
|
{String idField: 'id'}) {
|
||||||
return (_, arguments) async {
|
return (_, arguments) async {
|
||||||
var _requestInfo = _fetchRequestInfo(arguments);
|
var _requestInfo = _fetchRequestInfo(arguments);
|
||||||
var params = {'query': arguments, 'provider': Providers.graphQL}
|
var params = {'query': arguments, 'provider': Providers.graphQL}
|
||||||
..addAll(_requestInfo);
|
..addAll(_requestInfo);
|
||||||
var id = arguments.remove(idField);
|
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<Value, Serialized>
|
||||||
/// The arguments passed to the resolver will be forwarded to the service, and the
|
/// The arguments passed to the resolver will be forwarded to the service, and the
|
||||||
/// service will receive [Providers.graphql].
|
/// service will receive [Providers.graphql].
|
||||||
GraphQLFieldResolver<Value, Serialized>
|
GraphQLFieldResolver<Value, Serialized>
|
||||||
resolveViaServiceRemove<Value, Serialized>(Service service,
|
resolveViaServiceRemove<Value, Serialized>(Service<dynamic, Value> service,
|
||||||
{String idField: 'id'}) {
|
{String idField: 'id'}) {
|
||||||
return (_, arguments) async {
|
return (_, arguments) async {
|
||||||
var _requestInfo = _fetchRequestInfo(arguments);
|
var _requestInfo = _fetchRequestInfo(arguments);
|
||||||
var params = {'query': arguments, 'provider': Providers.graphQL}
|
var params = {'query': arguments, 'provider': Providers.graphQL}
|
||||||
..addAll(_requestInfo);
|
..addAll(_requestInfo);
|
||||||
var id = arguments.remove(idField);
|
var id = arguments.remove(idField);
|
||||||
return await service.remove(id, params) as Value;
|
return await service.remove(id, params);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue