forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_build.go
90 lines (74 loc) · 1.81 KB
/
models_build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package codegen
import (
"sort"
"github.com/vektah/gqlparser/ast"
"golang.org/x/tools/go/loader"
)
func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program, imports *Imports) ([]Model, error) {
var models []Model
for _, typ := range cfg.schema.Types {
var model Model
switch typ.Kind {
case ast.Object:
obj, err := cfg.buildObject(types, typ, imports)
if err != nil {
return nil, err
}
if obj.Root || obj.IsUserDefined {
continue
}
model = cfg.obj2Model(obj)
case ast.InputObject:
obj, err := cfg.buildInput(types, typ)
if err != nil {
return nil, err
}
if obj.IsUserDefined {
continue
}
model = cfg.obj2Model(obj)
case ast.Interface, ast.Union:
intf := cfg.buildInterface(types, typ, prog)
if intf.IsUserDefined {
continue
}
model = int2Model(intf)
default:
continue
}
model.Description = typ.Description // It's this or change both obj2Model and buildObject
models = append(models, model)
}
sort.Slice(models, func(i, j int) bool {
return models[i].GQLType < models[j].GQLType
})
return models, nil
}
func (cfg *Config) obj2Model(obj *Object) Model {
model := Model{
NamedType: obj.NamedType,
Fields: []ModelField{},
}
model.GoType = ucFirst(obj.GQLType)
model.Marshaler = &Ref{GoType: obj.GoType}
for i := range obj.Fields {
field := &obj.Fields[i]
mf := ModelField{Type: field.Type, GQLName: field.GQLName}
if field.GoFieldName != "" {
mf.GoFieldName = field.GoFieldName
} else {
mf.GoFieldName = field.GoNameExported()
}
model.Fields = append(model.Fields, mf)
}
return model
}
func int2Model(obj *Interface) Model {
model := Model{
NamedType: obj.NamedType,
Fields: []ModelField{},
}
model.GoType = ucFirst(obj.GQLType)
model.Marshaler = &Ref{GoType: obj.GoType}
return model
}