Skip to content

Commit

Permalink
fix: Define field args in deterministic order (#5)
Browse files Browse the repository at this point in the history
* Define field arguments in a deterministic order

* Yield introspected input fields in deterministic order
  • Loading branch information
AndrewSisley authored Jul 15, 2022
1 parent bc95f52 commit 2451189
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"reflect"
"regexp"
"sort"
"strings"

"github.com/graphql-go/graphql/language/ast"
)
Expand Down Expand Up @@ -588,6 +590,12 @@ func defineFieldMap(ttype Named, fieldMap Fields) (FieldDefinitionMap, error) {
}
fieldDef.Args = append(fieldDef.Args, fieldArg)
}

// Sort args so that their order is deterministic (alpha-numeric descending)
sort.Slice(fieldDef.Args, func(i, j int) bool {
return strings.Compare(fieldDef.Args[i].Name(), fieldDef.Args[j].Name()) == -1
})

resultFieldMap[fieldName] = fieldDef
}
return resultFieldMap, nil
Expand Down
7 changes: 7 additions & 0 deletions introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"sort"
"strings"

"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/printer"
Expand Down Expand Up @@ -618,6 +619,12 @@ func init() {
for _, field := range ttype.Fields() {
fields = append(fields, field)
}

// Sort args so that their order is deterministic (alpha-numeric descending)
sort.Slice(fields, func(i, j int) bool {
return strings.Compare(fields[i].Name(), fields[j].Name()) == -1
})

return fields, nil
}
return nil, nil
Expand Down

0 comments on commit 2451189

Please sign in to comment.