Skip to content

Commit e1e6e38

Browse files
committed
codegen: improve "could not find field" error message
1 parent 8e9d696 commit e1e6e38

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

graphql_client_codegen/src/shared.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ use proc_macro2::{Ident, Span, TokenStream};
66
use query::QueryContext;
77
use selection::*;
88

9+
fn format_available_fields(fields: &[GqlObjectField]) -> String {
10+
fields
11+
.iter()
12+
.map(|ref field| &field.name)
13+
.fold(String::new(), |mut acc, item| {
14+
acc.push_str(item);
15+
acc.push_str(", ");
16+
acc
17+
})
18+
.trim_end_matches(", ")
19+
.to_owned()
20+
}
21+
922
pub(crate) fn render_object_field(
1023
field_name: &str,
1124
field_type: &TokenStream,
@@ -77,7 +90,13 @@ pub(crate) fn field_impls_for_selection(
7790
let ty = fields
7891
.iter()
7992
.find(|f| &f.name == name)
80-
.ok_or_else(|| format_err!("could not find field `{}`", name))?
93+
.ok_or_else(|| {
94+
format_err!(
95+
"Could not find field `{}`. Available fields: `{}`.",
96+
name,
97+
format_available_fields(fields).as_str()
98+
)
99+
})?
81100
.type_
82101
.inner_name_str();
83102
let prefix = format!("{}{}", prefix.to_camel_case(), alias.to_camel_case());
@@ -111,15 +130,7 @@ pub(crate) fn response_fields_for_selection(
111130
"Could not find field `{}` on `{}`. Available fields: `{}`.",
112131
*name,
113132
type_name,
114-
schema_fields
115-
.iter()
116-
.map(|ref field| &field.name)
117-
.fold(String::new(), |mut acc, item| {
118-
acc.push_str(item);
119-
acc.push_str(", ");
120-
acc
121-
})
122-
.trim_end_matches(", ")
133+
format_available_fields(schema_fields).as_str()
123134
)
124135
})?;
125136
let ty = schema_field.type_.to_rust(

0 commit comments

Comments
 (0)