Skip to content

Commit 37ec1ab

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 37ec1ab

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
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 scalar types by name. This fixes "flapping" in
16+
the generated code that used to happen as the scalar types would be randomly
17+
ordered each time the generator ran.
1518

1619
## 0.10.0 - 2021-07-04
1720

graphql_client_codegen/src/query.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,18 @@ impl UsedTypes {
600600
&'s self,
601601
schema: &'a Schema,
602602
) -> impl Iterator<Item = (ScalarId, &'a StoredScalar)> + 's {
603-
self.types
603+
let mut scalars = self.types
604604
.iter()
605605
.filter_map(TypeId::as_scalar_id)
606606
.map(move |scalar_id| (scalar_id, schema.get_scalar(scalar_id)))
607607
.filter(|(_id, scalar)| !crate::schema::DEFAULT_SCALARS.contains(&scalar.name.as_str()))
608+
.collect::<Vec<_>>();
609+
// We sort these in order to ensure that the generated code is the
610+
// same every time when given the schema. Without sorting the order is
611+
// random, and code generated with the CLI tool may change even when
612+
// the source schema does not change.
613+
scalars.sort_by_key(|(_id, scalar)| scalar.name.as_str());
614+
scalars.into_iter()
608615
}
609616

610617
pub(crate) fn enums<'a, 'schema: 'a>(

0 commit comments

Comments
 (0)