Skip to content

Commit 9f8e2b2

Browse files
authored
Fix non-deterministic generated code involving interfaces and fragments. (#323)
In #209, @vikstrous2 added a bugfix to sort the list of interfaces when generating code, so the generated output was stable. This worked great, but there was one more case where we need to do that, involving interfaces in fragments. I have: - [x] Written a clear PR title and description (above) - [x] Signed the [Khan Academy CLA](https://www.khanacademy.org/r/cla) - [x] Added tests covering my changes, if applicable - [ ] Included a link to the issue fixed, if applicable - [ ] Included documentation, for new features - [x] Added an entry to the changelog
1 parent ae275ac commit 9f8e2b2

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

docs/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ Note that genqlient is now tested from Go 1.20 through Go 1.22.
3636
### Bug fixes:
3737
- The presence of negative pointer directives, i.e., `# @genqlient(pointer: false)` are now respected even in the when `optional: pointer` is set in the configuration file.
3838
- Made name collisions between query/mutation arguments and local function variables less likely.
39-
- Fix generation issue related to golang type implementation of complex graphql union fragments
39+
- Fix generation issue related to golang type implementation of complex graphql union fragments.
4040
- Bind correctly to types in the same package as the generated code.
41+
- Fix non-deterministic generated code when querying graphql interfaces via named fragments.
4142

4243
## v0.6.0
4344

generate/convert.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ func (g *generator) convertNamedFragment(fragment *ast.FragmentDefinition) (goTy
841841
return goType, nil
842842
case ast.Interface, ast.Union:
843843
implementationTypes := g.schema.GetPossibleTypes(typ)
844+
// Make sure we generate stable output by sorting the types by name when we get them
845+
sort.Slice(implementationTypes, func(i, j int) bool { return implementationTypes[i].Name < implementationTypes[j].Name })
844846
goType := &goInterfaceType{
845847
GoName: fragment.Name,
846848
SharedFields: fields,

generate/testdata/snapshots/TestGenerate-ComplexNamedFragments.graphql-ComplexNamedFragments.graphql.go

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generate/testdata/snapshots/TestGenerate-Flatten.graphql-Flatten.graphql.go

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)