Skip to content

Commit

Permalink
Cleanup field/method bind code
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Feb 6, 2019
1 parent cf94d3b commit b3f139c
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 568 deletions.
2 changes: 1 addition & 1 deletion codegen/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ nextArg:
}

// no matching arg found, abort
return fmt.Errorf("arg %s not found on method", param.Name())
return fmt.Errorf("%s is not in schema", param.Name())
}

field.Args = newArgs
Expand Down
22 changes: 22 additions & 0 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"go/token"
"go/types"
"regexp"
"strings"
Expand Down Expand Up @@ -33,6 +34,27 @@ func (c *Config) NewBinder(s *ast.Schema) (*Binder, error) {
}, nil
}

func (b *Binder) TypePosition(typ types.Type) token.Position {
named, isNamed := typ.(*types.Named)
if !isNamed {
return token.Position{
Filename: "unknown",
}
}

return b.ObjectPosition(named.Obj())
}

func (b *Binder) ObjectPosition(typ types.Object) token.Position {
if typ == nil {
return token.Position{
Filename: "unknown",
}
}
pkg := b.getPkg(typ.Pkg().Path())
return pkg.Fset.Position(typ.Pos())
}

func (b *Binder) FindType(pkgName string, typeName string) (types.Type, error) {
obj, err := b.FindObject(pkgName, typeName)
if err != nil {
Expand Down
51 changes: 15 additions & 36 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,50 +132,29 @@ func (b *builder) injectIntrospectionRoots(s *Data) error {
return fmt.Errorf("root query type must be defined")
}

typeType, err := b.Binder.TypeReference(ast.NamedType("__Type", nil))
if err != nil {
return errors.Wrap(err, "unable to find root Type introspection type")
}
stringRef, err := b.Binder.TypeReference(ast.NonNullNamedType("String", nil))
if err != nil {
return errors.Wrap(err, "unable to find root string type reference")
}

obj.Fields = append(obj.Fields, &Field{
TypeReference: typeType,
FieldDefinition: &ast.FieldDefinition{
Name: "__type",
},
GoFieldType: GoFieldMethod,
GoReceiverName: "ec",
GoFieldName: "introspectType",
Args: []*FieldArgument{
__type, err := b.buildField(obj, &ast.FieldDefinition{
Name: "__type",
Type: ast.NamedType("__Type", nil),
Arguments: []*ast.ArgumentDefinition{
{
ArgumentDefinition: &ast.ArgumentDefinition{
Name: "name",
},
TypeReference: stringRef,
Object: &Object{},
Name: "name",
Type: ast.NonNullNamedType("String", nil),
},
},
Object: obj,
})

schemaType, err := b.Binder.TypeReference(ast.NamedType("__Schema", nil))
if err != nil {
return errors.Wrap(err, "unable to find root Schema introspection type")
return err
}

obj.Fields = append(obj.Fields, &Field{
TypeReference: schemaType,
FieldDefinition: &ast.FieldDefinition{
Name: "__schema",
},
GoFieldType: GoFieldMethod,
GoReceiverName: "ec",
GoFieldName: "introspectSchema",
Object: obj,
__schema, err := b.buildField(obj, &ast.FieldDefinition{
Name: "__schema",
Type: ast.NamedType("__Schema", nil),
})
if err != nil {
return err
}

obj.Fields = append(obj.Fields, __type, __schema)

return nil
}
2 changes: 1 addition & 1 deletion codegen/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestTypeUnionAsInput(t *testing.T) {
err := generate("inputunion", `testdata/unioninput.graphqls`)

require.EqualError(t, err, "unable to build object definition: Query.addBookmark: cannot use Bookmarkable! as argument b because UNION is not a valid input type")
require.EqualError(t, err, "unable to build object definition: cannot use Bookmarkable! as argument b because UNION is not a valid input type")
}

func TestTypeInInput(t *testing.T) {
Expand Down
Loading

0 comments on commit b3f139c

Please sign in to comment.