Skip to content

Commit

Permalink
Return introspection document in stable order
Browse files Browse the repository at this point in the history
This avoids spurious changes when generating client code using
something like graphql-codegen.
  • Loading branch information
nyergler committed Jul 28, 2021
1 parent 07c1f93 commit eb36f04
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions graphql/introspection/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package introspection

import (
"sort"
"strings"

"github.com/vektah/gqlparser/v2/ast"
Expand All @@ -11,12 +12,20 @@ type Schema struct {
}

func (s *Schema) Types() []Type {
types := make([]Type, 0, len(s.schema.Types))
typeIndex := map[string]Type{}
typeNames := make([]string, 0, len(s.schema.Types))
for _, typ := range s.schema.Types {
if strings.HasPrefix(typ.Name, "__") {
continue
}
types = append(types, *WrapTypeFromDef(s.schema, typ))
typeNames = append(typeNames, typ.Name)
typeIndex[typ.Name] = *WrapTypeFromDef(s.schema, typ)
}
sort.Strings(typeNames)

types := make([]Type, len(typeNames))
for i, t := range typeNames {
types[i] = typeIndex[t]
}
return types
}
Expand All @@ -34,10 +43,18 @@ func (s *Schema) SubscriptionType() *Type {
}

func (s *Schema) Directives() []Directive {
res := make([]Directive, 0, len(s.schema.Directives))
dIndex := map[string]Directive{}
dNames := make([]string, 0, len(s.schema.Directives))

for _, d := range s.schema.Directives {
res = append(res, s.directiveFromDef(d))
dNames = append(dNames, d.Name)
dIndex[d.Name] = s.directiveFromDef(d)
}
sort.Strings(dNames)

res := make([]Directive, len(dNames))
for i, d := range dNames {
res[i] = dIndex[d]
}

return res
Expand Down

0 comments on commit eb36f04

Please sign in to comment.