@@ -2,7 +2,7 @@ use constants::*;
2
2
use failure;
3
3
use proc_macro2:: { Ident , Span , TokenStream } ;
4
4
use query:: QueryContext ;
5
- use selection:: { Selection , SelectionItem } ;
5
+ use selection:: { Selection , SelectionFragmentSpread , SelectionItem } ;
6
6
use std:: cell:: Cell ;
7
7
use std:: collections:: BTreeSet ;
8
8
@@ -45,40 +45,73 @@ pub(crate) fn union_variants(
45
45
} else {
46
46
true
47
47
}
48
- } ) . map ( |item| {
49
- match item {
50
- SelectionItem :: Field ( _) => Err ( format_err ! ( "field selection on union" ) ) ?,
51
- SelectionItem :: FragmentSpread ( _) => Err ( format_err ! ( "fragment spread on union" ) ) ?,
52
- SelectionItem :: InlineFragment ( frag) => {
53
- let variant_name = Ident :: new ( & frag. on , Span :: call_site ( ) ) ;
54
- used_variants. push ( frag. on . to_string ( ) ) ;
55
-
56
- let new_prefix = format ! ( "{}On{}" , prefix, frag. on) ;
57
-
58
- let variant_type = Ident :: new ( & new_prefix, Span :: call_site ( ) ) ;
59
-
60
- let field_object_type = query_context. schema . objects . get ( & frag. on ) . map ( |_f| {
61
- query_context. maybe_expand_field ( & frag. on , & frag. fields , & new_prefix)
62
- } ) ;
63
- let field_interface = query_context. schema . interfaces . get ( & frag. on ) . map ( |_f| {
64
- query_context. maybe_expand_field ( & frag. on , & frag. fields , & new_prefix)
65
- } ) ;
66
- // nested unions, is that even a thing?
67
- let field_union_type = query_context. schema . unions . get ( & frag. on ) . map ( |_f| {
68
- query_context. maybe_expand_field ( & frag. on , & frag. fields , & new_prefix)
69
- } ) ;
70
-
71
- match field_object_type. or ( field_interface) . or ( field_union_type) {
72
- Some ( tokens) => children_definitions. push ( tokens?) ,
73
- None => Err ( UnionError :: UnknownType {
74
- ty : frag. on . to_string ( ) ,
75
- } ) ?,
76
- } ;
77
-
78
- Ok ( quote ! {
79
- #variant_name( #variant_type)
80
- } )
81
- }
48
+ } ) . map ( |item| match item {
49
+ SelectionItem :: Field ( _) => Err ( format_err ! ( "field selection on union" ) ) ?,
50
+ SelectionItem :: FragmentSpread ( SelectionFragmentSpread { fragment_name } ) => {
51
+ let fragment = query_context
52
+ . fragments
53
+ . get ( fragment_name)
54
+ . ok_or_else ( || format_err ! ( "Unknown fragment: {}" , & fragment_name) ) ?;
55
+
56
+ let variant_name = Ident :: new ( & fragment. on , Span :: call_site ( ) ) ;
57
+ used_variants. push ( fragment. on . to_string ( ) ) ;
58
+
59
+ let new_prefix = format ! ( "{}On{}" , prefix, fragment. on) ;
60
+
61
+ let variant_type = Ident :: new ( & new_prefix, Span :: call_site ( ) ) ;
62
+
63
+ let field_object_type = query_context. schema . objects . get ( & fragment. on ) . map ( |_f| {
64
+ query_context. maybe_expand_field ( & fragment. on , & fragment. selection , & new_prefix)
65
+ } ) ;
66
+
67
+ let field_interface = query_context. schema . interfaces . get ( & fragment. on ) . map ( |_f| {
68
+ query_context. maybe_expand_field ( & fragment. on , & fragment. selection , & new_prefix)
69
+ } ) ;
70
+
71
+ let field_union_type = query_context. schema . unions . get ( & fragment. on ) . map ( |_f| {
72
+ query_context. maybe_expand_field ( & fragment. on , & fragment. selection , & new_prefix)
73
+ } ) ;
74
+
75
+ match field_object_type. or ( field_interface) . or ( field_union_type) {
76
+ Some ( tokens) => children_definitions. push ( tokens?) ,
77
+ None => Err ( UnionError :: UnknownType {
78
+ ty : fragment. on . to_string ( ) ,
79
+ } ) ?,
80
+ } ;
81
+
82
+ Ok ( quote ! {
83
+ #variant_name( #variant_type)
84
+ } )
85
+ }
86
+ SelectionItem :: InlineFragment ( frag) => {
87
+ let variant_name = Ident :: new ( & frag. on , Span :: call_site ( ) ) ;
88
+ used_variants. push ( frag. on . to_string ( ) ) ;
89
+
90
+ let new_prefix = format ! ( "{}On{}" , prefix, frag. on) ;
91
+
92
+ let variant_type = Ident :: new ( & new_prefix, Span :: call_site ( ) ) ;
93
+
94
+ let field_object_type = query_context. schema . objects . get ( & frag. on ) . map ( |_f| {
95
+ query_context. maybe_expand_field ( & frag. on , & frag. fields , & new_prefix)
96
+ } ) ;
97
+ let field_interface = query_context. schema . interfaces . get ( & frag. on ) . map ( |_f| {
98
+ query_context. maybe_expand_field ( & frag. on , & frag. fields , & new_prefix)
99
+ } ) ;
100
+
101
+ let field_union_type = query_context. schema . unions . get ( & frag. on ) . map ( |_f| {
102
+ query_context. maybe_expand_field ( & frag. on , & frag. fields , & new_prefix)
103
+ } ) ;
104
+
105
+ match field_object_type. or ( field_interface) . or ( field_union_type) {
106
+ Some ( tokens) => children_definitions. push ( tokens?) ,
107
+ None => Err ( UnionError :: UnknownType {
108
+ ty : frag. on . to_string ( ) ,
109
+ } ) ?,
110
+ } ;
111
+
112
+ Ok ( quote ! {
113
+ #variant_name( #variant_type)
114
+ } )
82
115
}
83
116
} ) . collect ( ) ;
84
117
0 commit comments