Skip to content

Commit

Permalink
Generate complexity for all fields. Fix bugs. Re-generate examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
edsrzf committed Aug 28, 2018
1 parent e5265ac commit 8da5d61
Show file tree
Hide file tree
Showing 14 changed files with 4,649 additions and 44 deletions.
1 change: 0 additions & 1 deletion codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ type TypeMapEntry struct {

type TypeMapField struct {
Resolver bool `yaml:"resolver"`
Complexity bool `yaml:"complexity"`
FieldName string `yaml:"fieldName"`
}

Expand Down
34 changes: 14 additions & 20 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ type Object struct {
type Field struct {
*Type

GQLName string // The name of the field in graphql
GoFieldType GoFieldType // The field type in go, if any
GoReceiverName string // The name of method & var receiver in go, if any
GoFieldName string // The name of the method or var in go, if any
Args []FieldArgument // A list of arguments to be passed to this field
ForceResolver bool // Should be emit Resolver method
CustomComplexity bool // Uses a custom complexity calculation
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
Object *Object // A link back to the parent object
Default interface{} // The default value
GQLName string // The name of the field in graphql
GoFieldType GoFieldType // The field type in go, if any
GoReceiverName string // The name of method & var receiver in go, if any
GoFieldName string // The name of the method or var in go, if any
Args []FieldArgument // A list of arguments to be passed to this field
ForceResolver bool // Should be emit Resolver method
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
Object *Object // A link back to the parent object
Default interface{} // The default value
}

type FieldArgument struct {
Expand Down Expand Up @@ -82,19 +81,14 @@ func (o *Object) IsConcurrent() bool {
return false
}

func (o *Object) HasComplexity() bool {
for _, f := range o.Fields {
if f.CustomComplexity {
return true
}
}
return false
}

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

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

func (f *Field) IsMethod() bool {
return f.GoFieldType == GoFieldMethod
}
Expand Down Expand Up @@ -226,7 +220,7 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
ec.Errorf(ctx, "must not be null")
}
{{- end }}
return graphql.Null
return graphql.Null
}
{{.next }}`, map[string]interface{}{
"val": val,
Expand Down
15 changes: 6 additions & 9 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I
}

var forceResolver bool
var customComplexity bool
var goName string
if entryExists {
if typeField, ok := typeEntry.Fields[field.Name]; ok {
goName = typeField.FieldName
forceResolver = typeField.Resolver
customComplexity = typeField.Complexity
}
}

Expand Down Expand Up @@ -170,13 +168,12 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I
}

obj.Fields = append(obj.Fields, Field{
GQLName: field.Name,
Type: types.getType(field.Type),
Args: args,
Object: obj,
GoFieldName: goName,
ForceResolver: forceResolver,
CustomComplexity: customComplexity,
GQLName: field.Name,
Type: types.getType(field.Type),
Args: args,
Object: obj,
GoFieldName: goName,
ForceResolver: forceResolver,
})
}

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

Large diffs are not rendered by default.

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

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

Expand Down Expand Up @@ -75,12 +73,12 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
switch typeName + "." + field {
{{ range $object := .Objects }}
{{ range $field := $object.Fields }}
{{ if $field.CustomComplexity }}
{{ if $field.IsSystem }}{{ else }}
case "{{$object.GQLType}}.{{$field.GQLName}}":
if e.complexity.{{$object.GoType}}.{{$field.GQLName|toCamel}} == nil {
if e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}} == nil {
break
}
{{ if . }}args := map[string]interface{}{} {{end}}
{{ 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 {
Expand All @@ -92,7 +90,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
}
args[{{$arg.GQLName|quote}}] = arg{{$i}}
{{ end }}
return e.complexity.{{$object.GoType}}.{{$field.GQLName|toCamel}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true
return e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true
{{ end }}
{{ end }}
{{ end }}
Expand Down
Loading

0 comments on commit 8da5d61

Please sign in to comment.