Skip to content

Commit

Permalink
less whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 11, 2018
1 parent c76f3b9 commit 8fa5f62
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 730 deletions.
198 changes: 12 additions & 186 deletions example/dataloader/generated.go

Large diffs are not rendered by default.

321 changes: 18 additions & 303 deletions example/starwars/generated.go

Large diffs are not rendered by default.

188 changes: 11 additions & 177 deletions example/todo/generated.go

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"syscall"

"github.com/vektah/gqlgen/neelance/schema"

"golang.org/x/tools/imports"
)

Expand Down Expand Up @@ -68,7 +67,6 @@ func main() {
"schema": "github.com/vektah/gqlgen/neelance/schema",
"validation": "github.com/vektah/gqlgen/neelance/validation",
"jsonw": "github.com/vektah/gqlgen/jsonw",
"graphql": "github.com/vektah/gqlgen",
},
}
e.extract()
Expand Down
26 changes: 13 additions & 13 deletions templates/args.gotpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{define "args" -}}
{{- range $i, $arg := . -}}
var arg{{$i}} {{$arg.Type.Local -}}
{{ if eq $arg.Type.FullName "time.Time" }}
{{- 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)
Expand All @@ -15,16 +15,16 @@
continue
}
}
{{ else if eq $arg.Type.Name "map[string]interface{}" }}
{{- else if eq $arg.Type.Name "map[string]interface{}" }}
if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok {
{{ if $arg.Type.IsPtr }}
{{- if $arg.Type.IsPtr }}
tmp2 := tmp.({{$arg.Type.Name}})
arg{{$i}} = &tmp2
{{ else }}
{{- else }}
arg{{$i}} = tmp.({{$arg.Type.Name}})
{{ end }}
{{- end }}
}
{{ else if $arg.Type.Scalar }}
{{- 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 {
Expand All @@ -33,12 +33,12 @@
}
arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2
}
{{ else }}
{{- else }}
err := unpackComplexArg(&arg{{$i}}, field.Args[{{$arg.Name|quote}}])
if err != nil {
ec.Error(err)
continue
}
{{end}}
{{- end -}}
{{- end -}}
{{- end}}
{{- end }}
{{- end }}
20 changes: 9 additions & 11 deletions templates/file.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
package {{ .PackageName }}

import (
{{ range $as, $import := .Imports }}
{{- range $as, $import := .Imports }}
{{- $as }} "{{ $import }}"
{{ end }}
)

type Resolvers interface {
{{ range $object := .Objects -}}
{{- range $object := .Objects -}}
{{ range $field := $object.Fields -}}
{{ $field.ResolverDeclaration $object }}
{{ end }}
{{ end }}
{{- end }}
}

func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[string]interface{}, io.Writer) []*errors.QueryError {
Expand Down Expand Up @@ -44,18 +44,16 @@ func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[
var result jsonw.JsonWriter
if op.Type == query.Query {
result = c._{{.QueryRoot|lcFirst}}(op.Selections, nil)
{{if .MutationRoot}}
{{- if .MutationRoot}}
} else if op.Type == query.Mutation {
result = c._{{.MutationRoot|lcFirst}}(op.Selections, nil)
{{end}}
{{- end}}
} else {
return []*errors.QueryError{errors.Errorf("unsupported operation type")}
}

c.wg.Wait()

// TODO: parallelize if query.

writer := jsonw.New(w)
writer.BeginObject()

Expand All @@ -81,13 +79,13 @@ type executionContext struct {
wg sync.WaitGroup
}

{{range $object := .Objects }}
{{- range $object := .Objects }}
{{ template "object" $object }}
{{end}}
{{- end}}

{{ range $interface := .Interfaces }}
{{- range $interface := .Interfaces }}
{{ template "interface" $interface }}
{{ end }}
{{- end }}

var parsedSchema = schema.MustParse({{.SchemaRaw|quote}})

Expand Down
10 changes: 5 additions & 5 deletions templates/interface.gotpl
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{{define "interface"}}
{{ $interface := . }}
{{- define "interface"}}
{{- $interface := . }}

func (ec *executionContext) _{{$interface.Type.GraphQLName|lcFirst}}(sel []query.Selection, it {{$interface.Type.Local}}) jsonw.JsonWriter {
switch it := it.(type) {
case nil:
return jsonw.NullWriter
{{range $implementor := $interface.Type.Implementors}}
{{- 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)

{{end}}
{{- end }}
default:
panic(fmt.Errorf("unexpected type %T", it))
}
}

{{end}}
{{- end}}
66 changes: 33 additions & 33 deletions templates/object.gotpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{{ define "object" }}
{{- define "object" }}
{{ $object := . }}
type _{{ .Type.GraphQLName}}Node struct {
_fields []collectedField

{{range $field := .Fields -}}
{{ if $field.Type.Scalar -}}
{{- range $field := .Fields }}
{{- if $field.Type.Scalar }}
{{$field.GraphQLName|ucFirst}} {{ $field.Type.Local }}
{{ else -}}
{{- else }}
{{$field.GraphQLName|ucFirst}} {{if $field.Type.IsSlice}}[]{{end}}jsonw.JsonWriter
{{ end -}}
{{end}}
{{- end }}
{{- end}}
}

var {{ $object.Type.GraphQLName|lcFirst}}Implementors = {{$object.Implementors}}
Expand All @@ -22,58 +22,58 @@ func (ec *executionContext) _{{$object.Type.GraphQLName|lcFirst}}(sel []query.Se

for _, field := range node._fields {
switch field.Name {
{{range $field := $object.Fields -}}
{{- range $field := $object.Fields }}
case "{{$field.GraphQLName}}":
{{ template "args" $field.Args -}}
{{- template "args" $field.Args }}

{{ if $field.IsResolver }}
{{- if $field.IsResolver }}
ec.wg.Add(1)
go func(field collectedField) {
defer ec.wg.Done()
{{ end }}
{{- end }}

{{- if $field.VarName -}}
{{- if $field.VarName }}
res := {{$field.VarName}}
{{ else if $field.MethodName -}}
{{ if $field.NoErr -}}
{{- else if $field.MethodName }}
{{- if $field.NoErr }}
res := {{$field.MethodName}}({{ $field.CallArgs $object }})
{{ else -}}
{{- else }}
res, err := {{$field.MethodName}}({{ $field.CallArgs $object }})
if err != nil {
ec.Error(err)
continue
}
{{ end }}
{{ else }}
{{- end }}
{{- else }}
res, err := ec.resolvers.{{ $object.Name }}_{{ $field.GraphQLName }}({{ $field.CallArgs $object }})
if err != nil {
ec.Error(err)
return
}
{{ end -}}
{{- end }}

{{ if $field.Type.Scalar }}
{{- if $field.Type.Scalar }}
node.{{$field.GraphQLName|ucFirst}} = res
{{ else }}
{{ if or $field.Type.IsPtr $field.Type.IsSlice }}
{{- else }}
{{- if or $field.Type.IsPtr $field.Type.IsSlice }}
if res != nil {
{{ end}}
{{ if $field.Type.IsSlice }}
{{- end }}
{{- if $field.Type.IsSlice }}
for i := range {{$field.Type.ByVal "res" }} {
node.{{$field.GraphQLName|ucFirst}} = append(node.{{$field.GraphQLName|ucFirst}}, ec._{{$field.Type.GraphQLName|lcFirst}}(field.Selections, {{ $field.Type.Elem.ByRef "res[i]" }}))
}
{{ else }}
{{- else }}
node.{{$field.GraphQLName|ucFirst}} = ec._{{$field.Type.GraphQLName|lcFirst}}(field.Selections, {{$field.Type.ByRef "res" }})
{{ end }}
{{ if or $field.Type.IsPtr $field.Type.IsSlice }}
{{- end }}
{{- if or $field.Type.IsPtr $field.Type.IsSlice }}
}
{{ end}}
{{ end }}
{{- end}}
{{- end }}

{{ if $field.IsResolver }}
{{- if $field.IsResolver }}
}(field)
{{ end }}
{{ end}}
{{- end }}
{{- end }}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
Expand All @@ -86,14 +86,14 @@ func (t *_{{ $object.Type.GraphQLName }}Node) WriteJson(w *jsonw.Writer) {
w.BeginObject()
for _, field := range t._fields {
switch field.Name {
{{range $field := $object.Fields -}}
{{- range $field := $object.Fields }}
case "{{$field.GraphQLName}}":
w.ObjectKey({{$field.GraphQLName|quote}})
{{ $field.WriteJson }}
{{ end }}
{{- end }}
}
}
w.EndObject()
}

{{end}}
{{- end}}

0 comments on commit 8fa5f62

Please sign in to comment.