Skip to content

Commit a09cb23

Browse files
committed
codegen: improve error messages
This is an initial step. Further improvements could offer suggestions for what is available. See #97.
1 parent 09b69a9 commit a09cb23

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

graphql_query_derive/src/codegen.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,26 @@ pub(crate) fn response_for_query(
4848
.find(|op| op.name == selected_operation)
4949
.map(|i| i.to_owned());
5050

51-
let operation = context.selected_operation.clone().unwrap_or_else(|| {
51+
let opt_operation = context.selected_operation.clone().or_else(|| {
5252
operations
5353
.iter()
5454
.next()
5555
.map(|i| i.to_owned())
56-
.expect("no operation in query document")
5756
});
57+
let operation = if let Some(operation) = opt_operation {
58+
operation
59+
} else {
60+
panic!("no operation '{}' in query document", selected_operation);
61+
};
5862

5963
let response_data_fields = {
60-
let root_name: String = operation
61-
.root_name(&context.schema)
62-
.expect("operation type not in schema");
64+
let opt_root_name = operation
65+
.root_name(&context.schema);
66+
let root_name: String = if let Some(root_name) = opt_root_name {
67+
root_name
68+
} else {
69+
panic!("operation type '{:?}' not in schema", operation.operation_type);
70+
};
6371
let definition = context
6472
.schema
6573
.objects

graphql_query_derive/src/fragments.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ impl GqlFragment {
1313
pub(crate) fn to_rust(&self, context: &QueryContext) -> Result<TokenStream, ::failure::Error> {
1414
let derives = context.response_derives();
1515
let name_ident = Ident::new(&self.name, Span::call_site());
16-
let object = context.schema.objects.get(&self.on).expect("oh, noes");
16+
let opt_object = context.schema.objects.get(&self.on);
17+
let object = if let Some(object) = opt_object {
18+
object
19+
} else {
20+
panic!("fragment '{}' cannot operate on unknown type '{}'", self.name, self.on);
21+
};
1722
let field_impls = object.field_impls_for_selection(context, &self.selection, &self.name)?;
1823
let fields = object.response_fields_for_selection(context, &self.selection, &self.name)?;
1924

0 commit comments

Comments
 (0)