Skip to content

Commit

Permalink
deduplicate package load between AutoBind and Binder
Browse files Browse the repository at this point in the history
  • Loading branch information
vikstrous committed Nov 26, 2019
1 parent d3f6384 commit 1b2e565
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
7 changes: 1 addition & 6 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 9 additions & 3 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 9 additions & 3 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1b2e565

Please sign in to comment.