Skip to content

Commit 2451189

Browse files
authored
fix: Define field args in deterministic order (#5)
* Define field arguments in a deterministic order * Yield introspected input fields in deterministic order
1 parent bc95f52 commit 2451189

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

definition.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"reflect"
77
"regexp"
8+
"sort"
9+
"strings"
810

911
"github.com/graphql-go/graphql/language/ast"
1012
)
@@ -588,6 +590,12 @@ func defineFieldMap(ttype Named, fieldMap Fields) (FieldDefinitionMap, error) {
588590
}
589591
fieldDef.Args = append(fieldDef.Args, fieldArg)
590592
}
593+
594+
// Sort args so that their order is deterministic (alpha-numeric descending)
595+
sort.Slice(fieldDef.Args, func(i, j int) bool {
596+
return strings.Compare(fieldDef.Args[i].Name(), fieldDef.Args[j].Name()) == -1
597+
})
598+
591599
resultFieldMap[fieldName] = fieldDef
592600
}
593601
return resultFieldMap, nil

introspection.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"reflect"
66
"sort"
7+
"strings"
78

89
"github.com/graphql-go/graphql/language/ast"
910
"github.com/graphql-go/graphql/language/printer"
@@ -618,6 +619,12 @@ func init() {
618619
for _, field := range ttype.Fields() {
619620
fields = append(fields, field)
620621
}
622+
623+
// Sort args so that their order is deterministic (alpha-numeric descending)
624+
sort.Slice(fields, func(i, j int) bool {
625+
return strings.Compare(fields[i].Name(), fields[j].Name()) == -1
626+
})
627+
621628
return fields, nil
622629
}
623630
return nil, nil

0 commit comments

Comments
 (0)