Skip to content

Always sort scalar types by name when returning them #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
`rustls` rather than `native-tls`.
- Code generated by the `graphql-client` CLI program now suppresses all
warnings from rustc and clippy.
- The generated code now sorts enums, inputs, and scalar types by name. This
fixes "flapping" in the generated code that used to happen types could be
randomly ordered each time the generator ran.

## 0.10.0 - 2021-07-04

Expand Down
21 changes: 18 additions & 3 deletions graphql_client_codegen/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,34 +587,49 @@ pub(crate) struct UsedTypes {
}

impl UsedTypes {
// We sort the returned inputs and other types in order to ensure that the
// generated code is the same every time when given the schema. Without
// sorting the order is random, and code generated with the CLI tool may
// change even when the source schema does not change.
pub(crate) fn inputs<'s, 'a: 's>(
&'s self,
schema: &'a Schema,
) -> impl Iterator<Item = (InputId, &'a StoredInputType)> + 's {
schema
let mut inputs = schema
.inputs()
.filter(move |(id, _input)| self.types.contains(&TypeId::Input(*id)))
.collect::<Vec<_>>();
inputs.sort_by_key(|(_id, input)| input.name.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.

I think using Itertools::sorted_by_key can avoid the intermediate Vec.

inputs.into_iter()
}

pub(crate) fn scalars<'s, 'a: 's>(
&'s self,
schema: &'a Schema,
) -> impl Iterator<Item = (ScalarId, &'a StoredScalar)> + 's {
self.types
let mut scalars = self
.types
.iter()
.filter_map(TypeId::as_scalar_id)
.map(move |scalar_id| (scalar_id, schema.get_scalar(scalar_id)))
.filter(|(_id, scalar)| !crate::schema::DEFAULT_SCALARS.contains(&scalar.name.as_str()))
.collect::<Vec<_>>();
scalars.sort_by_key(|(_id, scalar)| scalar.name.as_str());
scalars.into_iter()
}

pub(crate) fn enums<'a, 'schema: 'a>(
&'a self,
schema: &'schema Schema,
) -> impl Iterator<Item = (EnumId, &'schema StoredEnum)> + 'a {
self.types
let mut enums = self
.types
.iter()
.filter_map(TypeId::as_enum_id)
.map(move |enum_id| (enum_id, schema.get_enum(enum_id)))
.collect::<Vec<_>>();
enums.sort_by_key(|(_id, enum_)| enum_.name.as_str());
enums.into_iter()
}

pub(crate) fn fragment_ids(&self) -> impl Iterator<Item = ResolvedFragmentId> + '_ {
Expand Down