diff --git a/.editorconfig b/.editorconfig index fb788e5274f..17934a118ca 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,8 +8,5 @@ insert_final_newline = true indent_style = space indent_size = 4 -[*.go] -indent_style = tab - -[*.twig] +[*.{go,gotpl}] indent_style = tab diff --git a/README.md b/README.md index 62cfdedd789..d9ba4dede42 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This is a library for quickly creating a strictly typed graphql servers in golan Please use [dep](https://github.com/golang/dep) to pin your versions, the apis here should be considered unstable. Ideally you should version the binary used to generate the code, as well as the library itself. Version mismatches -between the generated code and the runtime will be ugly. (gorunpkg)[https://github.com/vektah/gorunpkg] makes this +between the generated code and the runtime will be ugly. [gorunpkg](https://github.com/vektah/gorunpkg) makes this as easy as: Gopkg.toml diff --git a/templates/args.gotpl b/templates/args.gotpl index 49130501d78..9029e41e1c2 100644 --- a/templates/args.gotpl +++ b/templates/args.gotpl @@ -1,44 +1,44 @@ {{- define "args" }} - {{- range $i, $arg := . }} - var arg{{$i}} {{$arg.Type.Local }} - {{- if eq $arg.Type.FullName "time.Time" }} - if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok { - if tmpStr, ok := tmp.(string); ok { - tmpDate, err := time.Parse(time.RFC3339, tmpStr) - if err != nil { - ec.Error(err) - continue - } - arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmpDate - } else { - ec.Errorf("Time '{{$arg.Name}}' should be RFC3339 formatted string") - continue - } - } - {{- else if eq $arg.Type.Name "map[string]interface{}" }} - if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok { - {{- if $arg.Type.IsPtr }} - tmp2 := tmp.({{$arg.Type.Name}}) - arg{{$i}} = &tmp2 - {{- else }} - arg{{$i}} = tmp.({{$arg.Type.Name}}) - {{- end }} - } - {{- else if $arg.Type.Scalar }} - if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok { - tmp2, err := coerce{{$arg.Type.Name|ucFirst}}(tmp) - if err != nil { - ec.Error(err) - continue - } - arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2 - } - {{- else }} - err := unpackComplexArg(&arg{{$i}}, field.Args[{{$arg.Name|quote}}]) - if err != nil { - ec.Error(err) - continue - } - {{- end}} - {{- end }} + {{- range $i, $arg := . }} + var arg{{$i}} {{$arg.Type.Local }} + {{- if eq $arg.Type.FullName "time.Time" }} + if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok { + if tmpStr, ok := tmp.(string); ok { + tmpDate, err := time.Parse(time.RFC3339, tmpStr) + if err != nil { + ec.Error(err) + continue + } + arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmpDate + } else { + ec.Errorf("Time '{{$arg.Name}}' should be RFC3339 formatted string") + continue + } + } + {{- else if eq $arg.Type.Name "map[string]interface{}" }} + if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok { + {{- if $arg.Type.IsPtr }} + tmp2 := tmp.({{$arg.Type.Name}}) + arg{{$i}} = &tmp2 + {{- else }} + arg{{$i}} = tmp.({{$arg.Type.Name}}) + {{- end }} + } + {{- else if $arg.Type.Scalar }} + if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok { + tmp2, err := coerce{{$arg.Type.Name|ucFirst}}(tmp) + if err != nil { + ec.Error(err) + continue + } + arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2 + } + {{- else }} + err := unpackComplexArg(&arg{{$i}}, field.Args[{{$arg.Name|quote}}]) + if err != nil { + ec.Error(err) + continue + } + {{- end}} + {{- end }} {{- end }} diff --git a/templates/file.gotpl b/templates/file.gotpl index cf3aa3b9fe0..0dd20dec63d 100644 --- a/templates/file.gotpl +++ b/templates/file.gotpl @@ -5,15 +5,15 @@ package {{ .PackageName }} import ( {{- range $as, $import := .Imports }} - {{- $as }} "{{ $import }}" + {{- $as }} "{{ $import }}" {{ end }} ) type Resolvers interface { {{- range $object := .Objects -}} - {{ range $field := $object.Fields -}} - {{ $field.ResolverDeclaration }} - {{ end }} + {{ range $field := $object.Fields -}} + {{ $field.ResolverDeclaration }} + {{ end }} {{- end }} } @@ -43,22 +43,22 @@ func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[ var data jsonw.Writer if op.Type == query.Query { - data = c._{{.QueryRoot|lcFirst}}(op.Selections, nil) - {{- if .MutationRoot}} + data = c._{{.QueryRoot|lcFirst}}(op.Selections, nil) + {{- if .MutationRoot}} } else if op.Type == query.Mutation { - data = c._{{.MutationRoot|lcFirst}}(op.Selections, nil) - {{- end}} + data = c._{{.MutationRoot|lcFirst}}(op.Selections, nil) + {{- end}} } else { return []*errors.QueryError{errors.Errorf("unsupported operation type")} } - c.wg.Wait() + c.wg.Wait() - result := &jsonw.OrderedMap{} - result.Add("data", data) + result := &jsonw.OrderedMap{} + result.Add("data", data) if len(c.Errors) > 0 { - result.Add("errors", errors.ErrorWriter(c.Errors)) + result.Add("errors", errors.ErrorWriter(c.Errors)) } result.WriteJson(w) @@ -76,11 +76,11 @@ type executionContext struct { } {{- range $object := .Objects }} - {{ template "object" $object }} + {{ template "object" $object }} {{- end}} {{- range $interface := .Interfaces }} - {{ template "interface" $interface }} + {{ template "interface" $interface }} {{- end }} var parsedSchema = schema.MustParse({{.SchemaRaw|quote}}) diff --git a/templates/interface.gotpl b/templates/interface.gotpl index 0e2f98a98e9..98c3d15fb03 100644 --- a/templates/interface.gotpl +++ b/templates/interface.gotpl @@ -2,20 +2,20 @@ {{- $interface := . }} func (ec *executionContext) _{{$interface.Type.GraphQLName|lcFirst}}(sel []query.Selection, it *{{$interface.Type.Local}}) jsonw.Writer { - switch it := (*it).(type) { - case nil: - return jsonw.Null - {{- range $implementor := $interface.Type.Implementors }} - case {{$implementor.Local}}: - return ec._{{$implementor.GraphQLName|lcFirst}}(sel, &it) + switch it := (*it).(type) { + case nil: + return jsonw.Null + {{- range $implementor := $interface.Type.Implementors }} + case {{$implementor.Local}}: + return ec._{{$implementor.GraphQLName|lcFirst}}(sel, &it) - case *{{$implementor.Local}}: - return ec._{{$implementor.GraphQLName|lcFirst}}(sel, it) + case *{{$implementor.Local}}: + return ec._{{$implementor.GraphQLName|lcFirst}}(sel, it) - {{- end }} - default: - panic(fmt.Errorf("unexpected type %T", it)) - } + {{- end }} + default: + panic(fmt.Errorf("unexpected type %T", it)) + } } {{- end}} diff --git a/templates/object.gotpl b/templates/object.gotpl index 8a5ce90c96d..27b84ca7e24 100644 --- a/templates/object.gotpl +++ b/templates/object.gotpl @@ -5,55 +5,55 @@ var {{ $object.Type.GraphQLName|lcFirst}}Implementors = {{$object.Implementors}} // nolint: gocyclo, errcheck, gas, goconst func (ec *executionContext) _{{$object.Type.GraphQLName|lcFirst}}(sel []query.Selection, it *{{$object.Type.Local}}) jsonw.Writer { - fields := ec.collectFields(sel, {{$object.Type.GraphQLName|lcFirst}}Implementors, map[string]bool{}) - out := jsonw.NewOrderedMap(len(fields)) + fields := ec.collectFields(sel, {{$object.Type.GraphQLName|lcFirst}}Implementors, map[string]bool{}) + out := jsonw.NewOrderedMap(len(fields)) for i, field := range fields { - out.Keys[i] = field.Alias - out.Values[i] = jsonw.Null + out.Keys[i] = field.Alias + out.Values[i] = jsonw.Null switch field.Name { - {{- range $field := $object.Fields }} + {{- range $field := $object.Fields }} case "{{$field.GraphQLName}}": - {{- template "args" $field.Args }} - - {{- if $field.IsConcurrent }} - ec.wg.Add(1) - go func(i int, field collectedField) { - defer ec.wg.Done() - {{- end }} - - {{- if $field.VarName }} - res := {{$field.VarName}} - {{- else if $field.MethodName }} - {{- if $field.NoErr }} - res := {{$field.MethodName}}({{ $field.CallArgs }}) - {{- else }} - res, err := {{$field.MethodName}}({{ $field.CallArgs }}) - if err != nil { - ec.Error(err) - {{ if $field.IsConcurrent }}return{{ else }}continue{{end}} - } - {{- end }} - {{- else }} - res, err := ec.resolvers.{{ $object.Name }}_{{ $field.GraphQLName }}({{ $field.CallArgs }}) - if err != nil { - ec.Error(err) - {{ if $field.IsConcurrent }}return{{ else }}continue{{end}} - } - {{- end }} - - {{ $field.WriteJson "out.Values[i]" }} - - {{- if $field.IsConcurrent }} - }(i, field) - {{- end }} - {{- end }} - default: - panic("unknown field " + strconv.Quote(field.Name)) + {{- template "args" $field.Args }} + + {{- if $field.IsConcurrent }} + ec.wg.Add(1) + go func(i int, field collectedField) { + defer ec.wg.Done() + {{- end }} + + {{- if $field.VarName }} + res := {{$field.VarName}} + {{- else if $field.MethodName }} + {{- if $field.NoErr }} + res := {{$field.MethodName}}({{ $field.CallArgs }}) + {{- else }} + res, err := {{$field.MethodName}}({{ $field.CallArgs }}) + if err != nil { + ec.Error(err) + {{ if $field.IsConcurrent }}return{{ else }}continue{{end}} + } + {{- end }} + {{- else }} + res, err := ec.resolvers.{{ $object.Name }}_{{ $field.GraphQLName }}({{ $field.CallArgs }}) + if err != nil { + ec.Error(err) + {{ if $field.IsConcurrent }}return{{ else }}continue{{end}} + } + {{- end }} + + {{ $field.WriteJson "out.Values[i]" }} + + {{- if $field.IsConcurrent }} + }(i, field) + {{- end }} + {{- end }} + default: + panic("unknown field " + strconv.Quote(field.Name)) } } - return out + return out }