Move introspection to graphql_server

This commit is contained in:
Tobe O 2018-08-03 13:26:11 -04:00
parent 89b9a3ea60
commit 01ad2d26bf
3 changed files with 23 additions and 3 deletions

View file

@ -36,6 +36,8 @@ main() async {
app.get('/graphiql', graphiql()); app.get('/graphiql', graphiql());
await todoService.create({'text': 'Clean your room!', 'completed': true}); await todoService.create({'text': 'Clean your room!', 'completed': true});
await todoService.create({'text': 'Take out the trash', 'completed': false});
await todoService.create({'text': 'Become a billionaire at the age of 5', 'completed': false});
var server = await http.startServer('127.0.0.1', 3000); var server = await http.startServer('127.0.0.1', 3000);
var uri = var uri =

View file

@ -2,7 +2,7 @@ import 'dart:async';
import 'package:graphql_parser/graphql_parser.dart'; import 'package:graphql_parser/graphql_parser.dart';
import 'package:graphql_schema/graphql_schema.dart'; import 'package:graphql_schema/graphql_schema.dart';
import 'package:graphql_schema/introspection.dart'; import 'introspection.dart';
class GraphQL { class GraphQL {
final Map<String, GraphQLType> customTypes = {}; final Map<String, GraphQLType> customTypes = {};
@ -290,7 +290,7 @@ class GraphQL {
if (fieldType is GraphQLListType) { if (fieldType is GraphQLListType) {
if (result is! Iterable) { if (result is! Iterable) {
throw new GraphQLException( throw new GraphQLException(
'Value of field "$fieldName" must be a list or iterable.'); 'Value of field "$fieldName" must be a list or iterable, got $result instead.');
} }
var innerType = fieldType.innerType; var innerType = fieldType.innerType;

View file

@ -1,10 +1,12 @@
import 'package:graphql_parser/graphql_parser.dart';
import 'package:graphql_schema/graphql_schema.dart'; import 'package:graphql_schema/graphql_schema.dart';
// TODO: How to handle custom types??? // TODO: How to handle custom types???
GraphQLSchema reflectSchema(GraphQLSchema schema, List<GraphQLType> allTypes) { GraphQLSchema reflectSchema(GraphQLSchema schema, List<GraphQLType> allTypes) {
var objectTypes = fetchAllTypes(schema); var objectTypes = fetchAllTypes(schema);
allTypes.addAll(objectTypes);
var typeType = _reflectSchemaTypes(); var typeType = _reflectSchemaTypes();
var directiveType = _reflectDirectiveType();
allTypes.addAll(objectTypes);
var schemaType = objectType('__Schema', fields: [ var schemaType = objectType('__Schema', fields: [
field( field(
@ -22,9 +24,15 @@ GraphQLSchema reflectSchema(GraphQLSchema schema, List<GraphQLType> allTypes) {
type: typeType, type: typeType,
resolve: (_, __) => schema.mutation, resolve: (_, __) => schema.mutation,
), ),
field(
'directives',
type: listType(directiveType).nonNullable(),
resolve: (_, __) => [], // TODO: Actually fetch directives
),
]); ]);
allTypes.addAll([ allTypes.addAll([
directiveType,
typeType, typeType,
schemaType, schemaType,
_reflectFields(), _reflectFields(),
@ -156,6 +164,16 @@ GraphQLObjectType _createFieldType() {
]); ]);
} }
GraphQLObjectType _reflectDirectiveType() {
return objectType('__Directive', fields: [
field(
'name',
type: graphQLString,
resolve: (obj, _) => (obj as DirectiveContext).NAME.span.text,
),
]);
}
List<GraphQLObjectType> fetchAllTypes(GraphQLSchema schema) { List<GraphQLObjectType> fetchAllTypes(GraphQLSchema schema) {
var typess = <GraphQLType>[]; var typess = <GraphQLType>[];
typess.addAll(_fetchAllTypesFromObject(schema.query)); typess.addAll(_fetchAllTypesFromObject(schema.query));