@@ -93,13 +93,10 @@ pub fn generate_module_token_stream(
93
93
( Some ( ops) , _) => ops,
94
94
( None , & CodegenMode :: Cli ) => codegen:: all_operations ( & query) ,
95
95
( None , & CodegenMode :: Derive ) => {
96
- return Err (
97
- format_err ! ( "The struct name does not match any defined operation in the query file.\n Struct name: {}\n Defined operations: {}" , options. struct_ident( ) . map( |i| i. to_string( ) ) . as_ref( ) . map( String :: as_str) . unwrap_or( "" ) , query. definitions. iter( ) . filter_map( |definition| match definition { graphql_parser:: query:: Definition :: Operation ( op) => match op { graphql_parser:: query:: OperationDefinition :: Mutation ( m) => Some ( m. name. as_ref( ) . unwrap( ) ) ,
98
- graphql_parser:: query:: OperationDefinition :: Query ( m) => Some ( m. name. as_ref( ) . unwrap( ) ) ,
99
- graphql_parser:: query:: OperationDefinition :: Subscription ( m) => Some ( m. name. as_ref( ) . unwrap( ) ) ,
100
- graphql_parser:: query:: OperationDefinition :: SelectionSet ( _) => panic!( "Bare selection sets are not supported" ) ,
101
- } , _ => None } ) . fold( String :: new( ) , |mut acc, item| { acc. push_str( & item) ; acc. push_str( ", " ) ; acc } ) . trim_end_matches( ", " ) ) ,
102
- )
96
+ return Err ( derive_operation_not_found_error (
97
+ options. struct_ident ( ) ,
98
+ & query,
99
+ ) ) ;
103
100
}
104
101
} ;
105
102
@@ -142,7 +139,7 @@ graphql_parser::query::OperationDefinition::SelectionSet(_) => panic!("Bare sele
142
139
query_string : query_string. as_str ( ) ,
143
140
schema : & schema,
144
141
query_document : & query,
145
- operation : operation ,
142
+ operation,
146
143
options : & options,
147
144
}
148
145
. to_token_stream ( ) ?;
@@ -172,3 +169,42 @@ fn read_file(path: &::std::path::Path) -> Result<String, failure::Error> {
172
169
file. read_to_string ( & mut out) ?;
173
170
Ok ( out)
174
171
}
172
+
173
+ /// In derive mode, build an error when the operation with the same name as the struct is not found.
174
+ fn derive_operation_not_found_error (
175
+ ident : Option < & proc_macro2:: Ident > ,
176
+ query : & graphql_parser:: query:: Document ,
177
+ ) -> failure:: Error {
178
+ use graphql_parser:: query:: * ;
179
+
180
+ let operation_name = ident. map ( |i| i. to_string ( ) ) ;
181
+ let struct_ident = operation_name. as_ref ( ) . map ( String :: as_str) . unwrap_or ( "" ) ;
182
+
183
+ let available_operations = query
184
+ . definitions
185
+ . iter ( )
186
+ . filter_map ( |definition| match definition {
187
+ Definition :: Operation ( op) => match op {
188
+ OperationDefinition :: Mutation ( m) => Some ( m. name . as_ref ( ) . unwrap ( ) ) ,
189
+ OperationDefinition :: Query ( m) => Some ( m. name . as_ref ( ) . unwrap ( ) ) ,
190
+ OperationDefinition :: Subscription ( m) => Some ( m. name . as_ref ( ) . unwrap ( ) ) ,
191
+ OperationDefinition :: SelectionSet ( _) => {
192
+ unreachable ! ( "Bare selection sets are not supported." )
193
+ }
194
+ } ,
195
+ _ => None ,
196
+ } )
197
+ . fold ( String :: new ( ) , |mut acc, item| {
198
+ acc. push_str ( & item) ;
199
+ acc. push_str ( ", " ) ;
200
+ acc
201
+ } ) ;
202
+
203
+ let available_operations = available_operations. trim_end_matches ( ", " ) ;
204
+
205
+ return format_err ! (
206
+ "The struct name does not match any defined operation in the query file.\n Struct name: {}\n Defined operations: {}" ,
207
+ struct_ident,
208
+ available_operations,
209
+ ) ;
210
+ }
0 commit comments