From 65bba97f1ada438076a3658e455ecb4f01a60149 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 3 Aug 2018 20:22:50 -0400 Subject: [PATCH] Introspection supports union types --- graphql_schema/lib/src/schema.dart | 2 ++ graphql_schema/lib/src/union.dart | 18 ++++++++++++++++++ graphql_server/lib/introspection.dart | 12 +++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 graphql_schema/lib/src/union.dart diff --git a/graphql_schema/lib/src/schema.dart b/graphql_schema/lib/src/schema.dart index 5c61a3b7..3469cffe 100644 --- a/graphql_schema/lib/src/schema.dart +++ b/graphql_schema/lib/src/schema.dart @@ -19,6 +19,8 @@ part 'scalar.dart'; part 'type.dart'; +part 'union.dart'; + part 'validation_result.dart'; class GraphQLSchema { diff --git a/graphql_schema/lib/src/union.dart b/graphql_schema/lib/src/union.dart new file mode 100644 index 00000000..64c4b0cb --- /dev/null +++ b/graphql_schema/lib/src/union.dart @@ -0,0 +1,18 @@ +part of graphql_schema.src.schema; + +class GraphQLUnionType extends GraphQLType + with + _NonNullableMixin { + final List> possibleTypes; + final String description; + + GraphQLUnionType(this.possibleTypes, {this.description}) { + assert(possibleTypes.every(( + t) => t is GraphQLUnionType), 'The member types of a Union type must all be Object base types; Scalar, Interface and Union types may not be member types of a Union. Similarly, wrapping types may not be member types of a Union'); + assert(possibleTypes + .isNotEmpty, 'A Union type must define one or more member types'); + } + + @override + String get name => possibleTypes.map((t) => t.name).join(' | ') +} \ No newline at end of file diff --git a/graphql_server/lib/introspection.dart b/graphql_server/lib/introspection.dart index fcdbe37b..4f3148fb 100644 --- a/graphql_server/lib/introspection.dart +++ b/graphql_server/lib/introspection.dart @@ -122,6 +122,8 @@ GraphQLObjectType _reflectSchemaTypes() { resolve: (type, _) { if (type is GraphQLObjectType && type.isInterface) { return type.possibleTypes; + } else if (type is GraphQLUnionType) { + return type.possibleTypes; } else { return null; } @@ -205,9 +207,11 @@ GraphQLObjectType _createTypeType() { return 'NON_NULL'; else if (t is GraphQLEnumType) return 'ENUM'; + else if (t is GraphQLUnionType) + return 'UNION'; else throw new UnsupportedError( - 'Cannot get the kind of $t.'); // TODO: union + 'Cannot get the kind of $t.'); }, ), field( @@ -445,8 +449,10 @@ Iterable _fetchAllTypesFromType(GraphQLType type) { types.addAll(_fetchAllTypesFromObject(type)); } else if (type is GraphQLEnumType) { types.add(type); + } else if ( type is GraphQLUnionType) { + for (var t in type.possibleTypes) { + types.addAll(_fetchAllTypesFromType(t)); + } } - - // TODO: union return types; }