@@ -24,20 +24,21 @@ impl GqlUnion {
24
24
) -> Result < TokenStream , failure:: Error > {
25
25
let struct_name = Ident :: new ( prefix, Span :: call_site ( ) ) ;
26
26
let mut children_definitions: Vec < TokenStream > = Vec :: new ( ) ;
27
- let fields : Result < Vec < TokenStream > , failure:: Error > = selection. 0 . iter ( ) . map ( |item| {
27
+ let variants : Result < Vec < TokenStream > , failure:: Error > = selection. 0 . iter ( ) . map ( |item| {
28
28
match item {
29
29
SelectionItem :: Field ( _) => unreachable ! ( "field selection on union" ) ,
30
30
SelectionItem :: FragmentSpread ( _) => unreachable ! ( "fragment spread on union" ) ,
31
31
SelectionItem :: InlineFragment ( frag) => {
32
- let field_name = Ident :: new (
33
- & format ! ( "on_{}" , frag. on) . to_snake_case ( ) ,
32
+ let variant_name = Ident :: new (
33
+ & frag. on ,
34
34
Span :: call_site ( ) ,
35
35
) ;
36
36
37
- let field_type = Ident :: new ( & format ! ( "{}On{}" , prefix, frag. on) , Span :: call_site ( ) ) ;
38
-
39
37
let new_prefix = format ! ( "{}On{}" , prefix, frag. on) ;
40
38
39
+ let variant_type = Ident :: new ( & new_prefix, Span :: call_site ( ) ) ;
40
+
41
+
41
42
let field_object_type = query_context. schema . objects . get ( & frag. on )
42
43
. map ( |f| query_context. maybe_expand_field ( & frag. on , & frag. fields , & new_prefix) ) ;
43
44
let field_interface = query_context. schema . interfaces . get ( & frag. on )
@@ -52,20 +53,21 @@ impl GqlUnion {
52
53
} ;
53
54
54
55
Ok ( quote ! {
55
- #field_name : #field_type
56
+ #variant_name ( #variant_type )
56
57
} )
57
58
}
58
59
}
59
60
} ) . collect ( ) ;
60
61
61
- let fields = fields ?;
62
+ let variants = variants ?;
62
63
63
64
Ok ( quote ! {
64
65
#( #children_definitions) *
65
66
66
67
#[ derive( Deserialize ) ]
67
- pub struct #struct_name {
68
- #( #fields) , *
68
+ #[ serde( tag = "__typename" ) ]
69
+ pub enum #struct_name {
70
+ #( #variants) , *
69
71
}
70
72
} )
71
73
}
@@ -161,7 +163,9 @@ mod tests {
161
163
"pub struct MeowOnUser { first_name : String , } " ,
162
164
"# [ derive ( Debug , Serialize , Deserialize ) ] " ,
163
165
"pub struct MeowOnOrganization { title : String , } " ,
164
- "# [ derive ( Deserialize ) ] pub struct Meow { on_user : MeowOnUser , on_organization : MeowOnOrganization }" ,
166
+ "# [ derive ( Deserialize ) ] " ,
167
+ "# [ serde ( tag = \" __typename\" ) ] " ,
168
+ "pub enum Meow { User ( MeowOnUser ) , Organization ( MeowOnOrganization ) }" ,
165
169
] . into_iter( ) . collect:: <String >( ) ,
166
170
) ;
167
171
}
0 commit comments