Skip to content

Commit c25738a

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

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
)
@@ -587,6 +589,12 @@ func defineFieldMap(ttype Named, fieldMap Fields) (FieldDefinitionMap, error) {
587589
}
588590
fieldDef.Args = append(fieldDef.Args, fieldArg)
589591
}
592+
593+
// Sort args so that their order is deterministic (alpha-numeric descending)
594+
sort.Slice(fieldDef.Args, func(i, j int) bool {
595+
return strings.Compare(fieldDef.Args[i].Name(), fieldDef.Args[j].Name()) == -1
596+
})
597+
590598
resultFieldMap[fieldName] = fieldDef
591599
}
592600
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)