Skip to content

Commit b708e80

Browse files
committed
Always sort scalar types by name when returning them
Previously these were returned in an essentially random order. This would lead to flapping in the generated code when running the client CLI against the same schema multiple times. In the application I'm writing, I check in the generated schema, so this sort of flapping in the generated code creates unwanted noise in the commit history.
1 parent e79e30c commit b708e80

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
`rustls` rather than `native-tls`.
1313
- Code generated by the `graphql-client` CLI program now suppresses all
1414
warnings from rustc and clippy.
15+
- The generated code now sorts enums, inputs, and scalar types by name. This
16+
fixes "flapping" in the generated code that used to happen types could be
17+
randomly ordered each time the generator ran.
1518

1619
## 0.10.0 - 2021-07-04
1720

graphql_client_codegen/src/query.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,34 +587,49 @@ pub(crate) struct UsedTypes {
587587
}
588588

589589
impl UsedTypes {
590+
// We sort the returned inputs and other types in order to ensure that the
591+
// generated code is the same every time when given the schema. Without
592+
// sorting the order is random, and code generated with the CLI tool may
593+
// change even when the source schema does not change.
590594
pub(crate) fn inputs<'s, 'a: 's>(
591595
&'s self,
592596
schema: &'a Schema,
593597
) -> impl Iterator<Item = (InputId, &'a StoredInputType)> + 's {
594-
schema
598+
let mut inputs = schema
595599
.inputs()
596600
.filter(move |(id, _input)| self.types.contains(&TypeId::Input(*id)))
601+
.collect::<Vec<_>>();
602+
inputs.sort_by_key(|(_id, input)| input.name.as_str());
603+
inputs.into_iter()
597604
}
598605

599606
pub(crate) fn scalars<'s, 'a: 's>(
600607
&'s self,
601608
schema: &'a Schema,
602609
) -> impl Iterator<Item = (ScalarId, &'a StoredScalar)> + 's {
603-
self.types
610+
let mut scalars = self
611+
.types
604612
.iter()
605613
.filter_map(TypeId::as_scalar_id)
606614
.map(move |scalar_id| (scalar_id, schema.get_scalar(scalar_id)))
607615
.filter(|(_id, scalar)| !crate::schema::DEFAULT_SCALARS.contains(&scalar.name.as_str()))
616+
.collect::<Vec<_>>();
617+
scalars.sort_by_key(|(_id, scalar)| scalar.name.as_str());
618+
scalars.into_iter()
608619
}
609620

610621
pub(crate) fn enums<'a, 'schema: 'a>(
611622
&'a self,
612623
schema: &'schema Schema,
613624
) -> impl Iterator<Item = (EnumId, &'schema StoredEnum)> + 'a {
614-
self.types
625+
let mut enums = self
626+
.types
615627
.iter()
616628
.filter_map(TypeId::as_enum_id)
617629
.map(move |enum_id| (enum_id, schema.get_enum(enum_id)))
630+
.collect::<Vec<_>>();
631+
enums.sort_by_key(|(_id, enum_)| enum_.name.as_str());
632+
enums.into_iter()
618633
}
619634

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

0 commit comments

Comments
 (0)