Skip to content

Commit

Permalink
Add a better error message when passing a type into an input
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Apr 26, 2018
1 parent 0424f04 commit a780ce6
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/docs/public
/example/chat/node_modules
/example/chat/package-lock.json
/codegen/tests/gen
5 changes: 4 additions & 1 deletion codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func (cfg *Config) models() (*ModelBuild, error) {

cfg.bindTypes(imports, namedTypes, cfg.modelDir, prog)

models := cfg.buildModels(namedTypes, prog)
models, err := cfg.buildModels(namedTypes, prog)
if err != nil {
return nil, err
}
return &ModelBuild{
PackageName: cfg.ModelPackageName,
Models: models,
Expand Down
4 changes: 4 additions & 0 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"syscall"

"github.com/pkg/errors"
"github.com/vektah/gqlgen/codegen/templates"
Expand Down Expand Up @@ -36,6 +37,9 @@ func Generate(cfg Config) error {
return err
}

_ = syscall.Unlink(cfg.ExecFilename)
_ = syscall.Unlink(cfg.ModelFilename)

modelsBuild, err := cfg.models()
if err != nil {
return errors.Wrap(err, "model plan failed")
Expand Down
9 changes: 6 additions & 3 deletions codegen/models_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"golang.org/x/tools/go/loader"
)

func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program) []Model {
func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program) ([]Model, error) {
var models []Model

for _, typ := range cfg.schema.Types {
var model Model
switch typ := typ.(type) {
case *schema.Object:
obj := cfg.buildObject(types, typ)
obj, err := cfg.buildObject(types, typ)
if err != nil {
return nil, err
}
if obj.Root || obj.IsUserDefined {
continue
}
Expand Down Expand Up @@ -43,7 +46,7 @@ func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program) []Model {
return strings.Compare(models[i].GQLType, models[j].GQLType) == -1
})

return models
return models, nil
}

func (cfg *Config) obj2Model(obj *Object) Model {
Expand Down
14 changes: 11 additions & 3 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"
"strings"

"github.com/pkg/errors"
"github.com/vektah/gqlgen/neelance/schema"
"golang.org/x/tools/go/loader"
)
Expand All @@ -14,7 +15,10 @@ func (cfg *Config) buildObjects(types NamedTypes, prog *loader.Program, imports
for _, typ := range cfg.schema.Types {
switch typ := typ.(type) {
case *schema.Object:
obj := cfg.buildObject(types, typ)
obj, err := cfg.buildObject(types, typ)
if err != nil {
return nil, err
}

def, err := findGoType(prog, obj.Package, obj.GoType)
if err != nil {
Expand All @@ -35,7 +39,7 @@ func (cfg *Config) buildObjects(types NamedTypes, prog *loader.Program, imports
return objects, nil
}

func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) *Object {
func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) (*Object, error) {
obj := &Object{NamedType: types[typ.TypeName()]}

for _, i := range typ.Interfaces {
Expand All @@ -51,6 +55,10 @@ func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) *Object {
Object: obj,
}

if !newArg.Type.IsInput && !newArg.Type.IsScalar {
return nil, errors.Errorf("%s cannot be used as argument of %s.%s. only input and scalar types are allowed", arg.Type, obj.GQLType, field.Name)
}

if arg.Default != nil {
newArg.Default = arg.Default.Value(nil)
newArg.StripPtr()
Expand Down Expand Up @@ -80,5 +88,5 @@ func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) *Object {
obj.Stream = true
}
}
return obj
return obj, nil
}
24 changes: 24 additions & 0 deletions codegen/tests/input_union_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tests

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/vektah/gqlgen/codegen"
)

func TestInputUnion(t *testing.T) {
err := codegen.Generate(codegen.Config{
SchemaStr: `
type Query {
addBookmark(b: Bookmarkable!): Boolean!
}
type Item {}
union Bookmarkable = Item
`,
ExecFilename: "gen/inputunion/exec.go",
ModelFilename: "gen/inputunion/model.go",
})

require.EqualError(t, err, "model plan failed: Bookmarkable! cannot be used as argument of Query.addBookmark. only input and scalar types are allowed")
}
4 changes: 0 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"os"
"syscall"

"github.com/vektah/gqlgen/codegen"
)
Expand Down Expand Up @@ -38,9 +37,6 @@ func main() {
os.Exit(1)
}

_ = syscall.Unlink(*output)
_ = syscall.Unlink(*models)

types := loadTypeMap()

err = codegen.Generate(codegen.Config{
Expand Down

0 comments on commit a780ce6

Please sign in to comment.