enum_build.go

 1package codegen
 2
 3import (
 4	"sort"
 5	"strings"
 6
 7	"github.com/99designs/gqlgen/codegen/templates"
 8	"github.com/vektah/gqlparser/ast"
 9)
10
11func (cfg *Config) buildEnums(types NamedTypes) []Enum {
12	var enums []Enum
13
14	for _, typ := range cfg.schema.Types {
15		namedType := types[typ.Name]
16		if typ.Kind != ast.Enum || strings.HasPrefix(typ.Name, "__") || namedType.IsUserDefined {
17			continue
18		}
19
20		var values []EnumValue
21		for _, v := range typ.EnumValues {
22			values = append(values, EnumValue{v.Name, v.Description})
23		}
24
25		enum := Enum{
26			NamedType:   namedType,
27			Values:      values,
28			Description: typ.Description,
29		}
30		enum.GoType = templates.ToCamel(enum.GQLType)
31		enums = append(enums, enum)
32	}
33
34	sort.Slice(enums, func(i, j int) bool {
35		return enums[i].GQLType < enums[j].GQLType
36	})
37
38	return enums
39}