Skip to content

Commit

Permalink
Give an appropriate error message when autoload isnt a valid package
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 5, 2020
1 parent 0ddb3ef commit d5d6f83
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
5 changes: 4 additions & 1 deletion codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,10 @@ func (c *Config) autobind() error {
continue
}

for _, p := range ps {
for i, p := range ps {
if p == nil {
return fmt.Errorf("unable to load %s - make sure you're using an import path to a package that exists", c.AutoBind[i])
}
if t := p.Types.Scope().Lookup(t.Name); t != nil {
c.Models.Add(t.Name(), t.Pkg().Path()+"."+t.Name())
break
Expand Down
60 changes: 39 additions & 21 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"runtime"
"testing"

"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"

"github.com/99designs/gqlgen/internal/code"
)
Expand Down Expand Up @@ -118,22 +117,41 @@ func TestConfigCheck(t *testing.T) {
}

func TestAutobinding(t *testing.T) {
cfg := Config{
Models: TypeMap{},
AutoBind: []string{
"github.com/99designs/gqlgen/example/chat",
"github.com/99designs/gqlgen/example/scalars/model",
},
Packages: &code.Packages{},
}

cfg.Schema = gqlparser.MustLoadSchema(&ast.Source{Name: "TestAutobinding.schema", Input: `
scalar Banned
type Message { id: ID }
`})

require.NoError(t, cfg.autobind())

require.Equal(t, "github.com/99designs/gqlgen/example/scalars/model.Banned", cfg.Models["Banned"].Model[0])
require.Equal(t, "github.com/99designs/gqlgen/example/chat.Message", cfg.Models["Message"].Model[0])
t.Run("valid paths", func(t *testing.T) {
cfg := Config{
Models: TypeMap{},
AutoBind: []string{
"github.com/99designs/gqlgen/example/chat",
"github.com/99designs/gqlgen/example/scalars/model",
},
Packages: &code.Packages{},
}

cfg.Schema = gqlparser.MustLoadSchema(&ast.Source{Name: "TestAutobinding.schema", Input: `
scalar Banned
type Message { id: ID }
`})

require.NoError(t, cfg.autobind())

require.Equal(t, "github.com/99designs/gqlgen/example/scalars/model.Banned", cfg.Models["Banned"].Model[0])
require.Equal(t, "github.com/99designs/gqlgen/example/chat.Message", cfg.Models["Message"].Model[0])
})

t.Run("with file path", func(t *testing.T) {
cfg := Config{
Models: TypeMap{},
AutoBind: []string{
"../chat",
},
Packages: &code.Packages{},
}

cfg.Schema = gqlparser.MustLoadSchema(&ast.Source{Name: "TestAutobinding.schema", Input: `
scalar Banned
type Message { id: ID }
`})

require.EqualError(t, cfg.autobind(), "unable to load ../chat - make sure you're using an import path to a package that exists")
})
}

0 comments on commit d5d6f83

Please sign in to comment.