File tree Expand file tree Collapse file tree 7 files changed +33
-22
lines changed
graphql_client_codegen/src Expand file tree Collapse file tree 7 files changed +33
-22
lines changed Original file line number Diff line number Diff line change @@ -56,16 +56,20 @@ fn fragment_on_union() {
56
56
57
57
let expected = fragment_on_union:: ResponseData {
58
58
names : Some ( vec ! [
59
- fragment_on_union:: MyQueryNames :: Person ( fragment_on_union:: MyQueryNamesOnPerson {
60
- first_name: "Audrey" . to_string( ) ,
61
- last_name: Some ( "Lorde" . to_string( ) ) ,
62
- } ) ,
63
- fragment_on_union:: MyQueryNames :: Dog ( fragment_on_union:: MyQueryNamesOnDog {
59
+ fragment_on_union:: FragmentOnUnionNames :: Person (
60
+ fragment_on_union:: MyQueryNamesOnPerson {
61
+ first_name: "Audrey" . to_string( ) ,
62
+ last_name: Some ( "Lorde" . to_string( ) ) ,
63
+ } ,
64
+ ) ,
65
+ fragment_on_union:: FragmentOnUnionNames :: Dog ( fragment_on_union:: MyQueryNamesOnDog {
64
66
name: "Laïka" . to_string( ) ,
65
67
} ) ,
66
- fragment_on_union:: MyQueryNames :: Organization ( fragment_on_union:: MyQueryNamesOnOrganization {
67
- title: "Mozilla" . to_string( ) ,
68
- } ) ,
68
+ fragment_on_union:: FragmentOnUnionNames :: Organization (
69
+ fragment_on_union:: MyQueryNamesOnOrganization {
70
+ title: "Mozilla" . to_string( ) ,
71
+ } ,
72
+ ) ,
69
73
fragment_on_union:: MyQueryNames :: Dog ( fragment_on_union:: MyQueryNamesOnDog {
70
74
name: "Norbert" . to_string( ) ,
71
75
} ) ,
Original file line number Diff line number Diff line change @@ -29,7 +29,6 @@ fragment NamesFragment on NamedThing {
29
29
30
30
query FragmentOnUnion {
31
31
names {
32
- __typename
33
32
... NamesFragment
34
33
}
35
34
}
Original file line number Diff line number Diff line change @@ -90,14 +90,8 @@ pub fn response_for_query(
90
90
) ) ?
91
91
}
92
92
93
- definitions. extend (
94
- definition
95
- . field_impls_for_selection ( & context, & selection, & prefix)
96
- . unwrap ( ) ,
97
- ) ;
98
- definition
99
- . response_fields_for_selection ( & context, & selection, & prefix)
100
- . unwrap ( )
93
+ definitions. extend ( definition. field_impls_for_selection ( & context, & selection, & prefix) ?) ;
94
+ definition. response_fields_for_selection ( & context, & selection, & prefix) ?
101
95
} ;
102
96
103
97
let enum_definitions = context. schema . enums . values ( ) . filter_map ( |enm| {
Original file line number Diff line number Diff line change @@ -131,7 +131,7 @@ impl GqlInterface {
131
131
let name = Ident :: new ( & prefix, Span :: call_site ( ) ) ;
132
132
let derives = query_context. response_derives ( ) ;
133
133
134
- selection. extract_typename ( ) . ok_or_else ( || {
134
+ selection. extract_typename ( query_context ) . ok_or_else ( || {
135
135
format_err ! (
136
136
"Missing __typename in selection for the {} interface (type: {})" ,
137
137
prefix,
Original file line number Diff line number Diff line change @@ -41,7 +41,10 @@ impl SelectionItem {
41
41
pub struct Selection ( pub Vec < SelectionItem > ) ;
42
42
43
43
impl Selection {
44
- pub fn extract_typename ( & self ) -> Option < & SelectionField > {
44
+ pub ( crate ) fn extract_typename (
45
+ & self ,
46
+ context : & crate :: query:: QueryContext ,
47
+ ) -> Option < & SelectionField > {
45
48
self . 0 . iter ( ) . filter_map ( |f| f. as_typename ( ) ) . next ( )
46
49
}
47
50
}
@@ -88,6 +91,14 @@ mod tests {
88
91
use super :: * ;
89
92
use graphql_parser;
90
93
94
+ #[ test]
95
+ fn selection_extract_typename_simple_case ( ) {
96
+ let mut selection = Selection ( Vec :: new ( ) ) ;
97
+ let context = query:: QueryContext :: new_empty ( ) ;
98
+
99
+ assert ! ( selection. extract_typename( & context) . is_none( ) ) ;
100
+ }
101
+
91
102
#[ test]
92
103
fn selection_from_graphql_parser_selection_set ( ) {
93
104
let query = r##"
Original file line number Diff line number Diff line change @@ -105,7 +105,8 @@ impl GqlUnion {
105
105
let struct_name = Ident :: new ( prefix, Span :: call_site ( ) ) ;
106
106
let derives = query_context. response_derives ( ) ;
107
107
108
- let typename_field = selection. extract_typename ( ) ;
108
+ // TODO: do this inside fragments
109
+ let typename_field = selection. extract_typename ( query_context) ;
109
110
110
111
if typename_field. is_none ( ) {
111
112
Err ( UnionError :: MissingTypename {
Original file line number Diff line number Diff line change @@ -19,8 +19,10 @@ pub fn graphql_query_derive(input: proc_macro::TokenStream) -> proc_macro::Token
19
19
let ast = syn:: parse2 ( input) . expect ( "Derive input is well formed" ) ;
20
20
let ( query_path, schema_path) = build_query_and_schema_path ( & ast) ;
21
21
let options = build_graphql_client_derive_options ( & ast) ;
22
- let gen = generate_module_token_stream ( query_path, schema_path, Some ( options) ) . unwrap ( ) ;
23
- gen. into ( )
22
+ match generate_module_token_stream ( query_path, schema_path, Some ( options) ) {
23
+ Ok ( module) => module. into ( ) ,
24
+ Err ( err) => panic ! ( "{}" , err) ,
25
+ }
24
26
}
25
27
26
28
fn build_query_and_schema_path (
You can’t perform that action at this time.
0 commit comments