forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_build.go
97 lines (80 loc) · 2.37 KB
/
input_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
91
92
93
94
95
96
97
package codegen
import (
"go/types"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/ast"
"golang.org/x/tools/go/loader"
)
func (cfg *Config) buildInputs(namedTypes NamedTypes, prog *loader.Program, imports *Imports) (Objects, error) {
var inputs Objects
for _, typ := range cfg.schema.Types {
switch typ.Kind {
case ast.InputObject:
input, err := cfg.buildInput(namedTypes, typ)
if err != nil {
return nil, err
}
def, err := findGoType(prog, input.Package, input.GoType)
if err != nil {
return nil, errors.Wrap(err, "cannot find type")
}
if def != nil {
input.Marshaler = buildInputMarshaler(typ, def)
bindErrs := bindObject(def.Type(), input, imports, cfg.StructTag)
if len(bindErrs) > 0 {
return nil, bindErrs
}
}
inputs = append(inputs, input)
}
}
sort.Slice(inputs, func(i, j int) bool {
return strings.Compare(inputs[i].GQLType, inputs[j].GQLType) == -1
})
return inputs, nil
}
func (cfg *Config) buildInput(types NamedTypes, typ *ast.Definition) (*Object, error) {
obj := &Object{NamedType: types[typ.Name]}
typeEntry, entryExists := cfg.Models[typ.Name]
for _, field := range typ.Fields {
newField := Field{
GQLName: field.Name,
Type: types.getType(field.Type),
Object: obj,
}
if entryExists {
if typeField, ok := typeEntry.Fields[field.Name]; ok {
newField.GoFieldName = typeField.FieldName
}
}
if field.DefaultValue != nil {
var err error
newField.Default, err = field.DefaultValue.Value(nil)
if err != nil {
return nil, errors.Errorf("default value for %s.%s is not valid: %s", typ.Name, field.Name, err.Error())
}
}
if !newField.Type.IsInput && !newField.Type.IsScalar {
return nil, errors.Errorf("%s cannot be used as a field of %s. only input and scalar types are allowed", newField.GQLType, obj.GQLType)
}
obj.Fields = append(obj.Fields, newField)
}
return obj, nil
}
// if user has implemented an UnmarshalGQL method on the input type manually, use it
// otherwise we will generate one.
func buildInputMarshaler(typ *ast.Definition, def types.Object) *Ref {
switch def := def.(type) {
case *types.TypeName:
namedType := def.Type().(*types.Named)
for i := 0; i < namedType.NumMethods(); i++ {
method := namedType.Method(i)
if method.Name() == "UnmarshalGQL" {
return nil
}
}
}
return &Ref{GoType: typ.Name}
}