Skip to content

Commit

Permalink
Don't emit complexity fields for reserved objects
Browse files Browse the repository at this point in the history
  • Loading branch information
edsrzf committed Aug 28, 2018
1 parent 8da5d61 commit 239b1d2
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 2,690 deletions.
6 changes: 5 additions & 1 deletion codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ func (o *Object) IsConcurrent() bool {
return false
}

func (o *Object) IsReserved() bool {
return strings.HasPrefix(o.GQLType, "__")
}

func (f *Field) IsResolver() bool {
return f.GoFieldName == ""
}

func (f *Field) IsSystem() bool {
func (f *Field) IsReserved() bool {
return strings.HasPrefix(f.GQLName, "__")
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/data.go

Large diffs are not rendered by default.

52 changes: 28 additions & 24 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ type DirectiveRoot struct {

type ComplexityRoot struct {
{{ range $object := .Objects }}
{{ $object.GQLType|toCamel }} struct {
{{ range $field := $object.Fields -}}
{{ if $field.IsSystem }}{{ else -}}
{{ $field.GQLName|toCamel }} {{ $field.ComplexitySignature }}
{{ end }}
{{ if $object.IsReserved }}{{ else -}}
{{ $object.GQLType|toCamel }} struct {
{{ range $field := $object.Fields -}}
{{ if $field.IsReserved }}{{ else -}}
{{ $field.GQLName|toCamel }} {{ $field.ComplexitySignature }}
{{ end }}
{{- end }}
}
{{- end }}
}
{{ end }}
}

Expand All @@ -72,25 +74,27 @@ func (e *executableSchema) Schema() *ast.Schema {
func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) {
switch typeName + "." + field {
{{ range $object := .Objects }}
{{ range $field := $object.Fields }}
{{ if $field.IsSystem }}{{ else }}
case "{{$object.GQLType}}.{{$field.GQLName}}":
if e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}} == nil {
break
}
{{ if $field.Args }}args := map[string]interface{}{} {{end}}
{{ range $i, $arg := $field.Args }}
var arg{{$i}} {{$arg.Signature }}
if tmp, ok := rawArgs[{{$arg.GQLName|quote}}]; ok {
var err error
{{$arg.Unmarshal (print "arg" $i) "tmp" }}
if err != nil {
return 0, false
}
{{ if $object.IsReserved }}{{ else }}
{{ range $field := $object.Fields }}
{{ if $field.IsReserved }}{{ else }}
case "{{$object.GQLType}}.{{$field.GQLName}}":
if e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}} == nil {
break
}
args[{{$arg.GQLName|quote}}] = arg{{$i}}
{{ end }}
return e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true
{{ if $field.Args }}args := map[string]interface{}{} {{end}}
{{ range $i, $arg := $field.Args }}
var arg{{$i}} {{$arg.Signature }}
if tmp, ok := rawArgs[{{$arg.GQLName|quote}}]; ok {
var err error
{{$arg.Unmarshal (print "arg" $i) "tmp" }}
if err != nil {
return 0, false
}
}
args[{{$arg.GQLName|quote}}] = arg{{$i}}
{{ end }}
return e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true
{{ end }}
{{ end }}
{{ end }}
{{ end }}
Expand Down
296 changes: 0 additions & 296 deletions codegen/testserver/generated.go

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

Loading

0 comments on commit 239b1d2

Please sign in to comment.