@@ -17,31 +17,14 @@ enum UnionError {
17
17
MissingTypename { union_name : String } ,
18
18
}
19
19
20
- impl GqlUnion {
21
- pub fn response_for_selection (
22
- & self ,
23
- query_context : & QueryContext ,
24
- selection : & Selection ,
25
- prefix : & str ,
26
- ) -> Result < TokenStream , failure:: Error > {
27
- let struct_name = Ident :: new ( prefix, Span :: call_site ( ) ) ;
28
- let mut children_definitions: Vec < TokenStream > = Vec :: new ( ) ;
29
-
30
- let typename_field = selection. 0 . iter ( ) . find ( |f| {
31
- if let SelectionItem :: Field ( f) = f {
32
- f. name == TYPENAME_FIELD
33
- } else {
34
- false
35
- }
36
- } ) ;
20
+ pub fn union_variants (
21
+ selection : & Selection ,
22
+ query_context : & QueryContext ,
23
+ prefix : & str ,
24
+ ) -> Result < ( Vec < TokenStream > , Vec < TokenStream > ) , failure:: Error > {
25
+ let mut children_definitions = Vec :: new ( ) ;
37
26
38
- if typename_field. is_none ( ) {
39
- Err ( UnionError :: MissingTypename {
40
- union_name : prefix. into ( ) ,
41
- } ) ?;
42
- }
43
-
44
- let variants: Result < Vec < TokenStream > , failure:: Error > = selection
27
+ let variants: Result < Vec < TokenStream > , failure:: Error > = selection
45
28
. 0
46
29
. iter ( )
47
30
// ignore __typename
@@ -98,7 +81,29 @@ impl GqlUnion {
98
81
} )
99
82
. collect ( ) ;
100
83
101
- let variants = variants?;
84
+ let variants = variants?;
85
+
86
+ Ok ( ( variants, children_definitions) )
87
+ }
88
+
89
+ impl GqlUnion {
90
+ pub fn response_for_selection (
91
+ & self ,
92
+ query_context : & QueryContext ,
93
+ selection : & Selection ,
94
+ prefix : & str ,
95
+ ) -> Result < TokenStream , failure:: Error > {
96
+ let struct_name = Ident :: new ( prefix, Span :: call_site ( ) ) ;
97
+
98
+ let typename_field = selection. extract_typename ( ) ;
99
+
100
+ if typename_field. is_none ( ) {
101
+ Err ( UnionError :: MissingTypename {
102
+ union_name : prefix. into ( ) ,
103
+ } ) ?;
104
+ }
105
+
106
+ let ( variants, children_definitions) = union_variants ( selection, query_context, prefix) ?;
102
107
103
108
Ok ( quote ! {
104
109
#( #children_definitions) *
0 commit comments