Skip to content

Commit 03f583d

Browse files
committed
Improve the error message when no operation name matches
1 parent b6035a4 commit 03f583d

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

graphql_client/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,14 @@ impl Display for Error {
206206
.path
207207
.as_ref()
208208
.map(|fragments| {
209-
format!(
210-
"{}",
211-
fragments
212-
.iter()
213-
.fold(String::new(), |mut acc, item| {
214-
acc.push_str(&format!("{}/", item));
215-
acc
216-
})
217-
.trim_end_matches("/")
218-
)
209+
fragments
210+
.iter()
211+
.fold(String::new(), |mut acc, item| {
212+
acc.push_str(&format!("{}/", item));
213+
acc
214+
})
215+
.trim_end_matches('/')
216+
.to_string()
219217
})
220218
.unwrap_or_else(|| "<query>".to_string());
221219

graphql_client_codegen/src/lib.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,10 @@ pub fn generate_module_token_stream(
9393
(Some(ops), _) => ops,
9494
(None, &CodegenMode::Cli) => codegen::all_operations(&query),
9595
(None, &CodegenMode::Derive) => {
96-
return Err(
97-
format_err!("The struct name does not match any defined operation in the query file.\nStruct name: {}\nDefined 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+
));
103100
}
104101
};
105102

@@ -142,7 +139,7 @@ graphql_parser::query::OperationDefinition::SelectionSet(_) => panic!("Bare sele
142139
query_string: query_string.as_str(),
143140
schema: &schema,
144141
query_document: &query,
145-
operation: operation,
142+
operation,
146143
options: &options,
147144
}
148145
.to_token_stream()?;
@@ -172,3 +169,42 @@ fn read_file(path: &::std::path::Path) -> Result<String, failure::Error> {
172169
file.read_to_string(&mut out)?;
173170
Ok(out)
174171
}
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.\nStruct name: {}\nDefined operations: {}",
207+
struct_ident,
208+
available_operations,
209+
);
210+
}

0 commit comments

Comments
 (0)