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

View file

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

View file

@ -1,25 +1,30 @@
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:graphql_schema/graphql_schema.dart'; import 'package:graphql_schema/graphql_schema.dart';
/// A GraphQL resolver that indexes an Angel service. /// A GraphQL resolver that `index`es an Angel service.
///
/// If [enableRead] is `true`, and the [idField] is present in the input,
/// a `read` will be performed, rather than an `index`.
/// ///
/// The arguments passed to the resolver will be forwarded to service, and the /// The arguments passed to the resolver will be forwarded to service, and the
/// service will receive [Providers.graphql]. /// service will receive [Providers.graphql].
GraphQLFieldResolver<Value, Serialized> resolveFromService<Value, Serialized>( GraphQLFieldResolver<Value, Serialized>
Service service, resolveViaServiceIndex<Value, Serialized>(Service service,
{String idField: 'id', {String idField: 'id'}) {
bool enableRead: true}) {
return (_, arguments) async { return (_, arguments) async {
var params = {'query': arguments, 'provider': Providers.graphql}; 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; 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) { if (defaultValue != null || argumentDefinition.defaultsToNull) {
coercedValues[argumentName] = defaultValue; coercedValues[argumentName] = defaultValue;
} else if (argumentType is GraphQLNonNullableType) { } else if (argumentType is GraphQLNonNullableType) {
throw new GraphQLException.fromSourceSpan( throw new GraphQLException.fromMessage(
'Missing value for argument "$argumentName".', 'Missing value for argument "$argumentName".');
value.valueOrVariable.span);
} else { } else {
continue; continue;
} }

View file

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