diff --git a/codegen/config/binder.go b/codegen/config/binder.go index 7ac9bff01eb..0ebf415735a 100644 --- a/codegen/config/binder.go +++ b/codegen/config/binder.go @@ -23,12 +23,7 @@ type Binder struct { SawInvalid bool } -func (c *Config) NewBinder(s *ast.Schema) (*Binder, error) { - pkgs, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, c.Models.ReferencedPackages()...) - if err != nil { - return nil, err - } - +func (c *Config) NewBinder(s *ast.Schema, pkgs []*packages.Package) (*Binder, error) { mp := map[string]*packages.Package{} var pkgErrs PkgErrors for _, p := range pkgs { diff --git a/codegen/config/config.go b/codegen/config/config.go index 45f8a7bd81a..f766206f591 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -385,14 +385,10 @@ func (c *Config) normalize() error { return nil } -func (c *Config) Autobind(s *ast.Schema) error { +func (c *Config) Autobind(s *ast.Schema, ps []*packages.Package) error { if len(c.AutoBind) == 0 { return nil } - ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes}, c.AutoBind...) - if err != nil { - return err - } for _, t := range s.Types { if c.Models.UserDefined(t.Name) { diff --git a/codegen/data.go b/codegen/data.go index 71206885be1..211cb593909 100644 --- a/codegen/data.go +++ b/codegen/data.go @@ -7,6 +7,7 @@ import ( "github.com/99designs/gqlgen/codegen/config" "github.com/pkg/errors" "github.com/vektah/gqlparser/ast" + "golang.org/x/tools/go/packages" ) // Data is a unified model of the code to be generated. Plugins may modify this structure to do things like implement @@ -51,14 +52,19 @@ func BuildData(cfg *config.Config) (*Data, error) { return nil, err } - err = cfg.Autobind(b.Schema) + cfg.InjectBuiltins(b.Schema) + + ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, append(cfg.Models.ReferencedPackages(), cfg.AutoBind...)...) if err != nil { return nil, err } - cfg.InjectBuiltins(b.Schema) + err = cfg.Autobind(b.Schema, ps) + if err != nil { + return nil, err + } - b.Binder, err = b.Config.NewBinder(b.Schema) + b.Binder, err = b.Config.NewBinder(b.Schema, ps) if err != nil { return nil, err } diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index b7b224d7cbc..9adc220023c 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -9,6 +9,7 @@ import ( "github.com/99designs/gqlgen/codegen/templates" "github.com/99designs/gqlgen/plugin" "github.com/vektah/gqlparser/ast" + "golang.org/x/tools/go/packages" ) type BuildMutateHook = func(b *ModelBuild) *ModelBuild @@ -81,14 +82,19 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error { return err } - err = cfg.Autobind(schema) + cfg.InjectBuiltins(schema) + + ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, append(cfg.Models.ReferencedPackages(), cfg.AutoBind...)...) if err != nil { return err } - cfg.InjectBuiltins(schema) + err = cfg.Autobind(schema, ps) + if err != nil { + return err + } - binder, err := cfg.NewBinder(schema) + binder, err := cfg.NewBinder(schema, ps) if err != nil { return err }