Skip to content

Commit

Permalink
remove wonky input directives
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 5, 2019
1 parent 6047355 commit 533b08b
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 566 deletions.
10 changes: 3 additions & 7 deletions codegen/args.gotpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{{ range $name, $args := .Args }}
func (e *executableSchema){{ $name }}(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
func (e *executableSchema) {{ $name }}(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
var err error
args := map[string]interface{}{}
{{- range $i, $arg := . }}
var arg{{$i}} {{ $arg.TypeReference.GO | ref}}
if tmp, ok := rawArgs[{{$arg.Name|quote}}]; ok {
{{- if $arg.Directives }}
getArg0 := func(ctx context.Context) (interface{}, error) { return unmarshal{{$arg.TypeReference.GQL.Name}}2{{ $arg.TypeReference.GO | ts }}(tmp) }
getArg0 := func(ctx context.Context) (interface{}, error) { return e.unmarshal{{$arg.TypeReference.GQL.Name}}2{{ $arg.TypeReference.GO | ts }}(tmp) }

{{- range $i, $directive := $arg.Directives }}
getArg{{add $i 1}} := func(ctx context.Context) (res interface{}, err error) {
Expand All @@ -30,15 +30,11 @@ func (e *executableSchema){{ $name }}(ctx context.Context, rawArgs map[string]in
return nil, fmt.Errorf(`unexpected type %T from directive, should be {{ $arg.TypeReference.GO }}`, tmp)
}
{{- else }}
arg{{$i}}, err = unmarshal{{$arg.TypeReference.GQL.Name}}2{{ $arg.TypeReference.GO | ts }}(tmp)
arg{{$i}}, err = e.unmarshal{{$arg.TypeReference.GQL.Name}}2{{ $arg.TypeReference.GO | ts }}(tmp)
if err != nil {
return nil, err
}
{{- end }}

{{/*{{- if eq $arg.TypeReference.Definition.Kind "INPUT_OBJECT" }}*/}}
{{/*{{ $arg.Middleware (print "arg" $i) (print "arg" $i) }}*/}}
{{/*{{- end }}*/}}
}
args[{{$arg.Name|quote}}] = arg{{$i}}
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion codegen/build_typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (b *builder) buildTypeDef(schemaType *ast.Definition) (*TypeDefinition, err

// Special case to reference generated unmarshal functions
if !hasUnmarshal {
t.Unmarshaler = types.NewFunc(0, b.Config.Exec.Pkg(), "unmarshalInput"+schemaType.Name, nil)
t.Unmarshaler = types.NewFunc(0, b.Config.Exec.Pkg(), "e.unmarshalInput"+schemaType.Name, nil)
}

return t, nil
Expand Down
2 changes: 1 addition & 1 deletion codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (b *Binder) TypeReference(schemaType *ast.Type) (ret *TypeReference, err er

// Special case to reference generated unmarshal functions
if !hasUnmarshal {
ref.Unmarshaler = types.NewFunc(0, b.cfg.Exec.Pkg(), "unmarshalInput"+schemaType.Name(), nil)
ref.Unmarshaler = types.NewFunc(0, b.cfg.Exec.Pkg(), "e.unmarshalInput"+schemaType.Name(), nil)
}
}

Expand Down
82 changes: 1 addition & 81 deletions codegen/input.gotpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{- range $input := .Inputs }}
{{- if not .HasUnmarshal }}
func unmarshalInput{{ .Name }}(v interface{}) ({{.Type | ref}}, error) {
func (e *executableSchema) unmarshalInput{{ .Name }}(v interface{}) ({{.Type | ref}}, error) {
var it {{.Type | ref}}
var asMap = v.(map[string]interface{})
{{ range $field := .Fields}}
Expand All @@ -27,84 +27,4 @@
return it, nil
}
{{- end }}

func (e *executableSchema) {{ .Name }}Middleware(ctx context.Context, obj *{{.Type | ref}}) (*{{.Type | ref}}, error) {
{{ if .Directives }}
cObj, err := chainFieldMiddleware(
[]graphql.FieldMiddleware{
{{- range $directive := .Directives }}
func(ctx context.Context, n graphql.Resolver) (res interface{}, err error) {
{{- if $directive.Args }}
{{- range $arg := $directive.Args }}
{{- if and $arg.IsPtr ( notNil "Value" $arg ) }}
{{ $arg.GoVarName }} := {{ $arg.Value | dump }}
{{- end }}
{{- end }}
{{- end -}}
return e.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs "obj" "n"}})
},
{{ end }}
}...
)(ctx, func(ctx context.Context)(interface{}, error){
return obj, nil
})
if err != nil || cObj == nil {
return nil ,err
}
obj, ok := cObj.(*{{.Type | ref}})
if !ok {
return nil, errors.New("expect {{.Type | ref}}")
}
{{ end }}

{{- range $field := .Fields }}
{{ if $field.HasDirectives }}
c{{$field.GoFieldName}}, err := chainFieldMiddleware(
[]graphql.FieldMiddleware{
{{- range $directive := $field.Directives }}
func(ctx context.Context, n graphql.Resolver) (res interface{}, err error) {
{{- if $directive.Args }}
{{- range $arg := $directive.Args }}
{{- if and $arg.TypeReference.IsPtr ( notNil "Value" $arg ) }}
{{ $arg.VarName }} := {{ $arg.Value | dump }}
{{- end }}
{{- end }}
{{- end }}
{{ if $field.IsPtr -}}
return e.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs ( print "*obj." $field.GoFieldName ) "n"}})
{{- else -}}
return e.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs ( print "obj." $field.GoFieldName ) "n"}})
{{- end }}
},
{{ end }}
}...
)(ctx, func(ctx context.Context)(interface{}, error){
return {{ if $field.IsPtr }}*{{end}}obj.{{$field.GoFieldName}}, nil
})
if err != nil {
return obj ,err
}

{{ if $field.IsPtr }}
if data, ok := c{{$field.GoFieldName}}.({{ $field.Type | ref }}); ok {
obj.{{$field.GoFieldName}} = &data
} else {
return obj, errors.New("expect {{ $field.GoType | ref }}")
}
{{else}}
if data, ok := c{{$field.GoFieldName}}.({{ $field.GoType | ref }}); ok{
obj.{{$field.GoFieldName}} = data
} else {
return obj, errors.New("expected {{$field.GoFieldName}} to be {{$field.GoType | ref }}")
}
{{ end }}

{{- end }}

{{ if eq $field.Definition.GQLDefinition.Kind "INPUT_OBJECT" }}
{{ $field.Middleware (print "obj." $field.GoFieldName ) (print "obj." $field.GoFieldName ) }}
{{- end }}
{{- end }}
return obj, nil
}
{{ end }}
Loading

0 comments on commit 533b08b

Please sign in to comment.