aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/codegen/input_build.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-14 12:40:31 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-14 12:41:59 +0200
commitb478cd1bcb4756b20f7f4b15fcf81f23e1a60a02 (patch)
tree8ce232dcab3dd00708f8ba66c334472457e5980d /vendor/github.com/99designs/gqlgen/codegen/input_build.go
parenta3fc9abb921f5ce7084d6ab7473442d0b72b1d78 (diff)
downloadgit-bug-b478cd1bcb4756b20f7f4b15fcf81f23e1a60a02.tar.gz
graphql: update gqlgen to 0.5.1
fix #6
Diffstat (limited to 'vendor/github.com/99designs/gqlgen/codegen/input_build.go')
-rw-r--r--vendor/github.com/99designs/gqlgen/codegen/input_build.go96
1 files changed, 96 insertions, 0 deletions
diff --git a/vendor/github.com/99designs/gqlgen/codegen/input_build.go b/vendor/github.com/99designs/gqlgen/codegen/input_build.go
new file mode 100644
index 00000000..06ff37a0
--- /dev/null
+++ b/vendor/github.com/99designs/gqlgen/codegen/input_build.go
@@ -0,0 +1,96 @@
+package codegen
+
+import (
+ "go/types"
+ "sort"
+
+ "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 inputs[i].GQLType < inputs[j].GQLType
+ })
+
+ 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}
+}