Skip to content

Commit

Permalink
Merge pull request 99designs#217 from vektah/resolver-middleware-all
Browse files Browse the repository at this point in the history
Run Resolver Middleware For All Fields
  • Loading branch information
Mathew Byrne authored Jul 24, 2018
2 parents 11fb5d9 + 19443d8 commit c382918
Show file tree
Hide file tree
Showing 12 changed files with 3,290 additions and 331 deletions.
2 changes: 1 addition & 1 deletion codegen/templates/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 19 additions & 24 deletions codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,27 @@
rctx.PushField(field.Alias)
defer rctx.Pop()
{{- end }}

{{- if $field.IsResolver }}
resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
{{- if $field.IsResolver }}
return ec.resolvers.{{ $field.ShortInvocation }}
})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
if resTmp == nil {
return graphql.Null
}
res := resTmp.({{$field.Signature}})
{{- else if $field.GoVarName }}
res := obj.{{$field.GoVarName}}
{{- else if $field.GoMethodName }}
{{- if $field.NoErr }}
res := {{$field.GoMethodName}}({{ $field.CallArgs }})
{{- else }}
res, err := {{$field.GoMethodName}}({{ $field.CallArgs }})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
{{- else if $field.GoVarName }}
return obj.{{$field.GoVarName}}, nil
{{- else if $field.GoMethodName }}
{{- if $field.NoErr }}
return {{$field.GoMethodName}}({{ $field.CallArgs }}), nil
{{- else }}
return {{$field.GoMethodName}}({{ $field.CallArgs }})
{{- end }}
{{- end }}
{{- end }}
})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
if resTmp == nil {
return graphql.Null
}
res := resTmp.({{$field.Signature}})
{{ $field.WriteJson }}
{{- if $field.IsConcurrent }}
})
Expand Down
17 changes: 10 additions & 7 deletions codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type Ref struct {
type Type struct {
*NamedType

Modifiers []string
CastType *Ref // the type to cast to when unmarshalling
Modifiers []string
AliasedType *Ref
}

const (
Expand All @@ -47,6 +47,9 @@ func (t Ref) PkgDot() string {
}

func (t Type) Signature() string {
if t.AliasedType != nil {
return strings.Join(t.Modifiers, "") + t.AliasedType.FullName()
}
return strings.Join(t.Modifiers, "") + t.FullName()
}

Expand Down Expand Up @@ -125,11 +128,11 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
}

realResult := result
if t.CastType != nil {
if t.AliasedType != nil {
result = "castTmp"
}

return tpl(`{{- if .t.CastType }}
return tpl(`{{- if .t.AliasedType }}
var castTmp {{.t.FullName}}
{{ end }}
{{- if eq .t.GoType "map[string]interface{}" }}
Expand All @@ -139,8 +142,8 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
{{- else -}}
err = (&{{.result}}).UnmarshalGQL({{.raw}})
{{- end }}
{{- if .t.CastType }}
{{ .realResult }} = {{.t.CastType.FullName}}(castTmp)
{{- if .t.AliasedType }}
{{ .realResult }} = {{.t.AliasedType.FullName}}(castTmp)
{{- end }}`, map[string]interface{}{
"realResult": realResult,
"result": result,
Expand All @@ -150,7 +153,7 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
}

func (t Type) Marshal(val string) string {
if t.CastType != nil {
if t.AliasedType != nil {
val = t.GoType + "(" + val + ")"
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func validateTypeBinding(imports *Imports, field *Field, goType types.Type) erro
field.Type.Modifiers = modifiersFromGoType(goType)
pkg, typ := pkgAndType(goType.String())
imp := imports.findByPath(pkg)
field.CastType = &Ref{GoType: typ, Import: imp}
field.AliasedType = &Ref{GoType: typ, Import: imp}
return nil
}

Expand Down
Loading

0 comments on commit c382918

Please sign in to comment.