From 01ad2d26bf658e7da0a267a3da14e8d468bd2cc1 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 3 Aug 2018 13:26:11 -0400 Subject: [PATCH] Move introspection to graphql_server --- angel_graphql/example/main.dart | 2 ++ graphql_server/lib/graphql_server.dart | 4 ++-- .../lib/introspection.dart | 20 ++++++++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) rename {graphql_schema => graphql_server}/lib/introspection.dart (90%) diff --git a/angel_graphql/example/main.dart b/angel_graphql/example/main.dart index c4deebb4..3e3de3bd 100644 --- a/angel_graphql/example/main.dart +++ b/angel_graphql/example/main.dart @@ -36,6 +36,8 @@ main() async { 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': 'Become a billionaire at the age of 5', 'completed': false}); var server = await http.startServer('127.0.0.1', 3000); var uri = diff --git a/graphql_server/lib/graphql_server.dart b/graphql_server/lib/graphql_server.dart index 8b5f3035..2c420326 100644 --- a/graphql_server/lib/graphql_server.dart +++ b/graphql_server/lib/graphql_server.dart @@ -2,7 +2,7 @@ import 'dart:async'; import 'package:graphql_parser/graphql_parser.dart'; import 'package:graphql_schema/graphql_schema.dart'; -import 'package:graphql_schema/introspection.dart'; +import 'introspection.dart'; class GraphQL { final Map customTypes = {}; @@ -290,7 +290,7 @@ class GraphQL { if (fieldType is GraphQLListType) { if (result is! Iterable) { 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; diff --git a/graphql_schema/lib/introspection.dart b/graphql_server/lib/introspection.dart similarity index 90% rename from graphql_schema/lib/introspection.dart rename to graphql_server/lib/introspection.dart index cfe1ac8a..6e9b19f0 100644 --- a/graphql_schema/lib/introspection.dart +++ b/graphql_server/lib/introspection.dart @@ -1,10 +1,12 @@ +import 'package:graphql_parser/graphql_parser.dart'; import 'package:graphql_schema/graphql_schema.dart'; // TODO: How to handle custom types??? GraphQLSchema reflectSchema(GraphQLSchema schema, List allTypes) { var objectTypes = fetchAllTypes(schema); - allTypes.addAll(objectTypes); var typeType = _reflectSchemaTypes(); + var directiveType = _reflectDirectiveType(); + allTypes.addAll(objectTypes); var schemaType = objectType('__Schema', fields: [ field( @@ -22,9 +24,15 @@ GraphQLSchema reflectSchema(GraphQLSchema schema, List allTypes) { type: typeType, resolve: (_, __) => schema.mutation, ), + field( + 'directives', + type: listType(directiveType).nonNullable(), + resolve: (_, __) => [], // TODO: Actually fetch directives + ), ]); allTypes.addAll([ + directiveType, typeType, schemaType, _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 fetchAllTypes(GraphQLSchema schema) { var typess = []; typess.addAll(_fetchAllTypesFromObject(schema.query));