Skip to content

Commit

Permalink
Merge buildInput into buildObject
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Jan 9, 2019
1 parent db33d7b commit 4b85d1b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 79 deletions.
2 changes: 1 addition & 1 deletion codegen/unified/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewSchema(cfg *config.Config) (*Schema, error) {

g.Objects = append(g.Objects, obj)
case ast.InputObject:
input, err := g.buildInput(schemaType)
input, err := g.buildObject(schemaType)
if err != nil {
return nil, errors.Wrap(err, "unable to build input definition")
}
Expand Down
47 changes: 0 additions & 47 deletions codegen/unified/build_input.go

This file was deleted.

53 changes: 24 additions & 29 deletions codegen/unified/build_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,34 @@ package unified
import (
"go/types"
"log"

"strings"

"github.com/pkg/errors"
"github.com/vektah/gqlparser/ast"
)

func (g *Schema) buildObject(typ *ast.Definition) (*Object, error) {
obj := &Object{
Definition: g.NamedTypes[typ.Name],
InTypemap: g.Config.Models.UserDefined(typ.Name),
}

tt := types.NewTypeName(0, g.Config.Exec.Pkg(), obj.Definition.GQLDefinition.Name+"Resolver", nil)
obj.ResolverInterface = types.NewNamed(tt, nil, nil)

if typ == g.Schema.Query {
obj.Root = true
obj.InTypemap = true
dirs, err := g.getDirectives(typ.Directives)
if err != nil {
return nil, err
}

if typ == g.Schema.Mutation {
obj.Root = true
obj.DisableConcurrency = true
obj.InTypemap = true
}
isRoot := typ == g.Schema.Query || typ == g.Schema.Mutation || typ == g.Schema.Subscription

if typ == g.Schema.Subscription {
obj.Root = true
obj.Stream = true
obj.InTypemap = true
obj := &Object{
Definition: g.NamedTypes[typ.Name],
InTypemap: g.Config.Models.UserDefined(typ.Name) || isRoot,
Root: isRoot,
DisableConcurrency: typ == g.Schema.Mutation,
Stream: typ == g.Schema.Subscription,
Directives: dirs,
ResolverInterface: types.NewNamed(
types.NewTypeName(0, g.Config.Exec.Pkg(), typ.Name+"Resolver", nil),
nil,
nil,
),
}

obj.Satisfies = append(obj.Satisfies, typ.Interfaces...)

for _, intf := range g.Schema.GetImplements(typ) {
obj.Implements = append(obj.Implements, g.NamedTypes[intf.Name])
}
Expand All @@ -52,14 +45,16 @@ func (g *Schema) buildObject(typ *ast.Definition) (*Object, error) {
return nil, err
}

obj.Fields = append(obj.Fields, f)
}
if typ.Kind == ast.InputObject && !f.TypeReference.Definition.GQLDefinition.IsInputType() {
return nil, errors.Errorf(
"%s cannot be used as a field of %s. only input and scalar types are allowed",
f.Definition.GQLDefinition.Name,
obj.Definition.GQLDefinition.Name,
)
}

dirs, err := g.getDirectives(typ.Directives)
if err != nil {
return nil, err
obj.Fields = append(obj.Fields, f)
}
obj.Directives = dirs

if _, isMap := obj.Definition.GoType.(*types.Map); !isMap && obj.InTypemap {
for _, bindErr := range bindObject(obj, g.Config.StructTag) {
Expand Down
3 changes: 1 addition & 2 deletions codegen/unified/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const (
type Object struct {
Definition *TypeDefinition
Fields []*Field
Satisfies []string
Implements []*TypeDefinition
ResolverInterface types.Type
Root bool
Expand Down Expand Up @@ -65,7 +64,7 @@ type Objects []*Object

func (o *Object) Implementors() string {
satisfiedBy := strconv.Quote(o.Definition.GQLDefinition.Name)
for _, s := range o.Satisfies {
for _, s := range o.Definition.GQLDefinition.Interfaces {
satisfiedBy += ", " + strconv.Quote(s)
}
return "[]string{" + satisfiedBy + "}"
Expand Down

0 comments on commit 4b85d1b

Please sign in to comment.