Fetch all types from enum as well

This commit is contained in:
Tobe O 2018-08-03 19:30:19 -04:00
parent b32893b8c0
commit c4c3ddff78
5 changed files with 41 additions and 25 deletions

View file

@ -24,11 +24,16 @@ main() async {
description: 'A simple API that manages your to-do list.',
fields: [
field(
'todo',
'todos',
type: listType(convertDartType(Todo).nonNullable()),
resolve: resolveFromService(todoService),
resolve: resolveViaServiceIndex(todoService),
),
field(
'todo',
type: convertDartType(Todo),
resolve: resolveViaServiceRead(todoService),
arguments: [
new GraphQLFieldArgument('id', graphQLId),
new GraphQLFieldArgument('id', graphQLId.nonNullable()),
],
),
],
@ -39,10 +44,14 @@ main() async {
app.all('/graphql', graphQLHttp(new GraphQL(schema)));
app.get('/graphiql', graphiql());
await todoService.create({'text': 'Clean your room!', 'completed': true});
await todoService.create({'text': 'Take out the trash', 'completed': false});
await todoService
.create({'text': 'Clean your room!', 'completion_status': 'COMPLETE'});
await todoService.create(
{'text': 'Become a billionaire at the age of 5', 'completed': false});
{'text': 'Take out the trash', 'completion_status': 'INCOMPLETE'});
await todoService.create({
'text': 'Become a billionaire at the age of 5',
'completion_status': 'INCOMPLETE'
});
var server = await http.startServer('127.0.0.1', 3000);
var uri =
@ -67,5 +76,4 @@ class Todo extends Model {
}
@GraphQLDocumentation(description: 'The completion status of a to-do item.')
enum CompletionStatus {
COMPLETE, INCOMPLETE }
enum CompletionStatus { COMPLETE, INCOMPLETE }

View file

@ -55,7 +55,7 @@ RequestHandler graphQLHttp(GraphQL graphQl) {
errors
.addAll(e.errors.map((ee) => new GraphQLExceptionError(ee)).toList());
throw new GraphQLException(errors);
return new GraphQLException(errors).toJson();
} on SyntaxError catch (e) {
return new GraphQLException.fromSourceSpan(e.message, e.span);
} on GraphQLException catch (e) {

View file

@ -1,25 +1,30 @@
import 'package:angel_framework/angel_framework.dart';
import 'package:graphql_schema/graphql_schema.dart';
/// A GraphQL resolver that indexes an Angel service.
///
/// If [enableRead] is `true`, and the [idField] is present in the input,
/// a `read` will be performed, rather than an `index`.
/// A GraphQL resolver that `index`es an Angel service.
///
/// The arguments passed to the resolver will be forwarded to service, and the
/// service will receive [Providers.graphql].
GraphQLFieldResolver<Value, Serialized> resolveFromService<Value, Serialized>(
Service service,
{String idField: 'id',
bool enableRead: true}) {
GraphQLFieldResolver<Value, Serialized>
resolveViaServiceIndex<Value, Serialized>(Service service,
{String idField: 'id'}) {
return (_, arguments) async {
var params = {'query': arguments, 'provider': Providers.graphql};
if (enableRead && arguments.containsKey(idField)) {
var id = arguments.remove(idField);
return await service.read(id, params) as Value;
}
return await service.index(params) as Value;
};
}
/// A GraphQL resolver that `read`s a single value from an Angel service.
///
/// The arguments passed to the resolver will be forwarded to service, and the
/// service will receive [Providers.graphql].
GraphQLFieldResolver<Value, Serialized>
resolveViaServiceRead<Value, Serialized>(Service service,
{String idField: 'id'}) {
return (_, arguments) async {
var params = {'query': arguments, 'provider': Providers.graphql};
var id = arguments.remove(idField);
return await service.read(id, params) as Value;
};
}

View file

@ -238,9 +238,8 @@ class GraphQL {
if (defaultValue != null || argumentDefinition.defaultsToNull) {
coercedValues[argumentName] = defaultValue;
} else if (argumentType is GraphQLNonNullableType) {
throw new GraphQLException.fromSourceSpan(
'Missing value for argument "$argumentName".',
value.valueOrVariable.span);
throw new GraphQLException.fromMessage(
'Missing value for argument "$argumentName".');
} else {
continue;
}

View file

@ -418,6 +418,10 @@ List<GraphQLType> _fetchAllTypesFromObject(GraphQLObjectType objectType) {
} else {
types.addAll(_fetchAllTypesFromType(field.type));
}
for (var argument in field.arguments ?? <GraphQLFieldArgument>[]) {
types.addAll(_fetchAllTypesFromType(argument.type));
}
}
return types;