From d5d6f830475fcaa5790bb1d6390f3876cd9073a2 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 5 Feb 2020 09:32:15 +1100 Subject: [PATCH] Give an appropriate error message when autoload isnt a valid package --- codegen/config/config.go | 5 ++- codegen/config/config_test.go | 60 +++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/codegen/config/config.go b/codegen/config/config.go index 10d95aac9f2..a4d676a2a66 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -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 diff --git a/codegen/config/config_test.go b/codegen/config/config_test.go index 292090294c8..9f0e0a32494 100644 --- a/codegen/config/config_test.go +++ b/codegen/config/config_test.go @@ -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" ) @@ -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") + }) }