11use anyhow:: Error ;
22use inflector:: Inflector ;
33
4- use super :: ObjectOrInterface ;
4+ use super :: QueryableType ;
55use crate :: prelude:: s:: {
66 self , Definition , Directive , Document , EnumType , Field , InterfaceType , ObjectType , Type ,
7- TypeDefinition , Value ,
7+ TypeDefinition , UnionType , Value ,
88} ;
99use crate :: prelude:: { ValueType , ENV_VARS } ;
1010use crate :: schema:: { META_FIELD_TYPE , SCHEMA_TYPE_NAME , SQL_FIELD_TYPE } ;
@@ -44,11 +44,29 @@ impl ObjectTypeExt for InterfaceType {
4444 }
4545}
4646
47+ impl ObjectTypeExt for UnionType {
48+ fn field ( & self , _name : & str ) -> Option < & Field > {
49+ None
50+ }
51+
52+ fn is_meta ( & self ) -> bool {
53+ false
54+ }
55+
56+ fn is_sql ( & self ) -> bool {
57+ self . name == SQL_FIELD_TYPE
58+ }
59+ }
60+
4761pub trait DocumentExt {
4862 fn get_object_type_definitions ( & self ) -> Vec < & ObjectType > ;
4963
5064 fn get_interface_type_definitions ( & self ) -> Vec < & InterfaceType > ;
5165
66+ fn get_union_definitions ( & self ) -> Vec < & UnionType > ;
67+
68+ fn get_union_definition ( & self , name : & str ) -> Option < & UnionType > ;
69+
5270 fn get_object_type_definition ( & self , name : & str ) -> Option < & ObjectType > ;
5371
5472 fn get_object_and_interface_type_fields ( & self ) -> HashMap < & str , & Vec < Field > > ;
@@ -63,7 +81,7 @@ pub trait DocumentExt {
6381
6482 fn get_root_subscription_type ( & self ) -> Option < & ObjectType > ;
6583
66- fn object_or_interface ( & self , name : & str ) -> Option < ObjectOrInterface < ' _ > > ;
84+ fn object_or_interface ( & self , name : & str ) -> Option < QueryableType < ' _ > > ;
6785
6886 fn get_named_type ( & self , name : & str ) -> Option < & TypeDefinition > ;
6987
@@ -129,6 +147,22 @@ impl DocumentExt for Document {
129147 . collect ( )
130148 }
131149
150+ fn get_union_definitions ( & self ) -> Vec < & UnionType > {
151+ self . definitions
152+ . iter ( )
153+ . filter_map ( |d| match d {
154+ Definition :: TypeDefinition ( TypeDefinition :: Union ( t) ) => Some ( t) ,
155+ _ => None ,
156+ } )
157+ . collect ( )
158+ }
159+
160+ fn get_union_definition ( & self , name : & str ) -> Option < & UnionType > {
161+ self . get_union_definitions ( )
162+ . into_iter ( )
163+ . find ( |object_type| object_type. name . eq ( name) )
164+ }
165+
132166 fn find_interface ( & self , name : & str ) -> Option < & InterfaceType > {
133167 self . definitions . iter ( ) . find_map ( |d| match d {
134168 Definition :: TypeDefinition ( TypeDefinition :: Interface ( t) ) if t. name == name => Some ( t) ,
@@ -183,10 +217,11 @@ impl DocumentExt for Document {
183217 . next ( )
184218 }
185219
186- fn object_or_interface ( & self , name : & str ) -> Option < ObjectOrInterface < ' _ > > {
220+ fn object_or_interface ( & self , name : & str ) -> Option < QueryableType < ' _ > > {
187221 match self . get_named_type ( name) {
188222 Some ( TypeDefinition :: Object ( t) ) => Some ( t. into ( ) ) ,
189223 Some ( TypeDefinition :: Interface ( t) ) => Some ( t. into ( ) ) ,
224+ Some ( TypeDefinition :: Union ( u) ) => Some ( u. into ( ) ) ,
190225 _ => None ,
191226 }
192227 }
0 commit comments