Skip to content

Commit

Permalink
Make output deterministic for graphql interfaces (#209)
Browse files Browse the repository at this point in the history
We noticed that the output from genqlient can be non-deterministic
when a graphql query queries a field that's an interface. This PR
fixes that by sorting the types as soon as we extract them from
the schema.
  • Loading branch information
vikstrous2 authored Jul 28, 2022
1 parent 03b6b6b commit 2ae8ea4
Show file tree
Hide file tree
Showing 14 changed files with 441 additions and 436 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ When releasing a new version:

### Bug fixes:

- Fixed non-deterministic generated code when querying graphql interfaces

## v0.5.0

Version 0.5.0 adds several new configuration options and convenience features. Note that genqlient now requires Go 1.16 or higher, and is tested through Go 1.18.
Expand Down
3 changes: 3 additions & 0 deletions generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package generate

import (
"fmt"
"sort"

"github.com/vektah/gqlparser/v2/ast"
)
Expand Down Expand Up @@ -465,6 +466,8 @@ func (g *generator) convertDefinition(
}

implementationTypes := g.schema.GetPossibleTypes(def)
// Make sure we generate stable output by sorting the types by name when we get them
sort.Slice(implementationTypes, func(i, j int) bool { return implementationTypes[i].Name < implementationTypes[j].Name })
goType := &goInterfaceType{
GoName: name,
SharedFields: sharedFields,
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2ae8ea4

Please sign in to comment.