Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move object directive logic to object function #3308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@
}
f.TypeReference = tr
}
if f.TypeReference != nil {
dirs, err := b.getDirectives(f.TypeReference.Definition.Directives)
if err != nil {
errret = err
}
for _, dir := range obj.Directives {
if dir.IsLocation(ast.LocationInputObject) {
dirs = append(dirs, dir)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why duplicate directives on LocationInputObject?

}
}
f.Directives = append(dirs, f.Directives...)
}
//if f.TypeReference != nil {

Check failure on line 95 in codegen/field.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.23)

commentFormatting: put a space between `//` and comment text (gocritic)
// dirs, err := b.getDirectives(f.TypeReference.Definition.Directives)
// if err != nil {
// errret = err
// }
// for _, dir := range obj.Directives {
// if dir.IsLocation(ast.LocationInputObject) {
// dirs = append(dirs, dir)
// }
// }
// f.Directives = append(dirs, f.Directives...)
//}
}()

f.Stream = obj.Stream
Expand Down
37 changes: 37 additions & 0 deletions codegen/object.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,43 @@ func (ec *executionContext) _{{$object.Name}}(ctx context.Context, sel ast.Selec
}
{{- else }}
func (ec *executionContext) _{{$object.Name}}(ctx context.Context, sel ast.SelectionSet{{ if not $object.Root }},obj {{$object.Reference | ref }}{{ end }}) graphql.Marshaler {
{{- if gt (len $object.Directives) 0 }}
directive0 := func(rctx context.Context) (interface{}, error) {
ctx = rctx // use context from middleware stack in children
return obj, nil
}

{{range $i, $directive := .Directives -}}
directive{{add $i 1}} := func(ctx context.Context) (interface{}, error) {
{{- range $arg := $directive.Args }}
{{- if notNil "Value" $arg }}
{{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Value | dump }})
if err != nil{
return obj, err
}
{{- else if notNil "Default" $arg }}
{{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Default | dump }})
if err != nil{
return obj, err
}
{{- end }}
{{- end }}
{{- if not $directive.IsBuiltIn}}
if {{$directive.CallPath}} == nil {
return obj, errors.New("directive {{$directive.Name}} is not implemented")
}
{{- end}}
return {{$directive.CallPath}}({{$directive.ResolveArgs "obj" $i }})
}
{{ end -}}

_, err := directive{{.Directives|len}}(ctx)
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
{{end}}

fields := graphql.CollectFields(ec.OperationContext, sel, {{$object.Name|lcFirst}}Implementors)
{{- if $object.Root }}
ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{
Expand Down
Loading