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 f4d8fc3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 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
8 changes: 7 additions & 1 deletion codegen/config/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
"golang.org/x/tools/go/packages"
)

func TestBindingToInvalid(t *testing.T) {
Expand Down Expand Up @@ -58,7 +59,12 @@ func createBinder(cfg Config) (*Binder, *ast.Schema) {
}
`})

b, err := cfg.NewBinder(s)
ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, cfg.Models.ReferencedPackages()...)
if err != nil {
panic(err)
}

b, err := cfg.NewBinder(s, ps)
if err != nil {
panic(err)
}
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
6 changes: 5 additions & 1 deletion codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
"golang.org/x/tools/go/packages"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -132,7 +133,10 @@ func TestAutobinding(t *testing.T) {
type Message { id: ID }
`})

require.NoError(t, cfg.Autobind(s))
ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes}, cfg.AutoBind...)
require.NoError(t, err)

require.NoError(t, cfg.Autobind(s, ps))

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])
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 f4d8fc3

Please sign in to comment.