Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions graphql_client_codegen/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ use proc_macro2::{Ident, Span, TokenStream};
use query::QueryContext;
use selection::*;

fn format_available_fields(fields: &[GqlObjectField]) -> String {
fields
.iter()
.map(|ref field| &field.name)
.fold(String::new(), |mut acc, item| {
acc.push_str(item);
acc.push_str(", ");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Itertools::format would just be better.

fields.iter().map(|ref field| &field.name).format("`, `")

This keeps the field names enclosed in "`", but leaves the commas without markup.

acc
})
.trim_end_matches(", ")
.to_owned()
}

pub(crate) fn render_object_field(
field_name: &str,
field_type: &TokenStream,
Expand Down Expand Up @@ -77,7 +90,13 @@ pub(crate) fn field_impls_for_selection(
let ty = fields
.iter()
.find(|f| &f.name == name)
.ok_or_else(|| format_err!("could not find field `{}`", name))?
.ok_or_else(|| {
format_err!(
"Could not find field `{}`. Available fields: `{}`.",
name,
format_available_fields(fields).as_str()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This as_str seems superfluous now (same below).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - thanks!

)
})?
.type_
.inner_name_str();
let prefix = format!("{}{}", prefix.to_camel_case(), alias.to_camel_case());
Expand Down Expand Up @@ -111,15 +130,7 @@ pub(crate) fn response_fields_for_selection(
"Could not find field `{}` on `{}`. Available fields: `{}`.",
*name,
type_name,
schema_fields
.iter()
.map(|ref field| &field.name)
.fold(String::new(), |mut acc, item| {
acc.push_str(item);
acc.push_str(", ");
acc
})
.trim_end_matches(", ")
format_available_fields(schema_fields).as_str()
)
})?;
let ty = schema_field.type_.to_rust(
Expand Down