Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into disable-introspection
Browse files Browse the repository at this point in the history
Regenerate
  • Loading branch information
Adam Scarr committed Nov 28, 2018
2 parents 62f0d08 + e4bad0e commit 6bea1d8
Show file tree
Hide file tree
Showing 49 changed files with 583 additions and 426 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions codegen/ambient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package codegen

import (
// Import and ignore the ambient imports listed below so dependency managers
// don't prune unused code for us. Both lists should be kept in sync.
_ "github.com/99designs/gqlgen/graphql"
_ "github.com/99designs/gqlgen/graphql/introspection"
_ "github.com/vektah/gqlparser"
_ "github.com/vektah/gqlparser/ast"
)
81 changes: 27 additions & 54 deletions codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type Build struct {
Objects Objects
Inputs Objects
Interfaces []*Interface
Imports []*Import
QueryRoot *Object
MutationRoot *Object
SubscriptionRoot *Object
Expand All @@ -26,22 +25,19 @@ type Build struct {

type ModelBuild struct {
PackageName string
Imports []*Import
Models []Model
Enums []Enum
}

type ResolverBuild struct {
PackageName string
Imports []*Import
ResolverType string
Objects Objects
ResolverFound bool
}

type ServerBuild struct {
PackageName string
Imports []*Import
ExecPackageName string
ResolverPackageName string
}
Expand All @@ -50,30 +46,29 @@ type ServerBuild struct {
func (cfg *Config) models() (*ModelBuild, error) {
namedTypes := cfg.buildNamedTypes()

progLoader := newLoader(namedTypes, true)
progLoader := cfg.newLoaderWithoutErrors()

prog, err := progLoader.Load()
if err != nil {
return nil, errors.Wrap(err, "loading failed")
}
imports := buildImports(namedTypes, cfg.Model.Dir())

cfg.bindTypes(imports, namedTypes, cfg.Model.Dir(), prog)
cfg.bindTypes(namedTypes, cfg.Model.Dir(), prog)

models, err := cfg.buildModels(namedTypes, prog, imports)
models, err := cfg.buildModels(namedTypes, prog)
if err != nil {
return nil, err
}
return &ModelBuild{
PackageName: cfg.Model.Package,
Models: models,
Enums: cfg.buildEnums(namedTypes),
Imports: imports.finalize(),
}, nil
}

// bind a schema together with some code to generate a Build
func (cfg *Config) resolver() (*ResolverBuild, error) {
progLoader := newLoader(cfg.buildNamedTypes(), true)
progLoader := cfg.newLoaderWithoutErrors()
progLoader.Import(cfg.Resolver.ImportPath())

prog, err := progLoader.Load()
Expand All @@ -84,13 +79,10 @@ func (cfg *Config) resolver() (*ResolverBuild, error) {
destDir := cfg.Resolver.Dir()

namedTypes := cfg.buildNamedTypes()
imports := buildImports(namedTypes, destDir)
imports.add(cfg.Exec.ImportPath())
imports.add("github.com/99designs/gqlgen/handler") // avoid import github.com/vektah/gqlgen/handler

cfg.bindTypes(imports, namedTypes, destDir, prog)
cfg.bindTypes(namedTypes, destDir, prog)

objects, err := cfg.buildObjects(namedTypes, prog, imports)
objects, err := cfg.buildObjects(namedTypes, prog)
if err != nil {
return nil, err
}
Expand All @@ -100,52 +92,38 @@ func (cfg *Config) resolver() (*ResolverBuild, error) {

return &ResolverBuild{
PackageName: cfg.Resolver.Package,
Imports: imports.finalize(),
Objects: objects,
ResolverType: cfg.Resolver.Type,
ResolverFound: resolverFound,
}, nil
}

func (cfg *Config) server(destDir string) *ServerBuild {
imports := buildImports(NamedTypes{}, destDir)
imports.add(cfg.Exec.ImportPath())
imports.add(cfg.Resolver.ImportPath())

// extra imports only used by the server template
imports.add("context")
imports.add("log")
imports.add("net/http")
imports.add("os")
imports.add("github.com/99designs/gqlgen/handler")

return &ServerBuild{
PackageName: cfg.Resolver.Package,
Imports: imports.finalize(),
ExecPackageName: cfg.Exec.Package,
ResolverPackageName: cfg.Resolver.Package,
ExecPackageName: cfg.Exec.ImportPath(),
ResolverPackageName: cfg.Resolver.ImportPath(),
}
}

// bind a schema together with some code to generate a Build
func (cfg *Config) bind() (*Build, error) {
namedTypes := cfg.buildNamedTypes()

progLoader := newLoader(namedTypes, true)
progLoader := cfg.newLoaderWithoutErrors()
prog, err := progLoader.Load()
if err != nil {
return nil, errors.Wrap(err, "loading failed")
}

imports := buildImports(namedTypes, cfg.Exec.Dir())
cfg.bindTypes(imports, namedTypes, cfg.Exec.Dir(), prog)
cfg.bindTypes(namedTypes, cfg.Exec.Dir(), prog)

objects, err := cfg.buildObjects(namedTypes, prog, imports)
objects, err := cfg.buildObjects(namedTypes, prog)
if err != nil {
return nil, err
}

inputs, err := cfg.buildInputs(namedTypes, prog, imports)
inputs, err := cfg.buildInputs(namedTypes, prog)
if err != nil {
return nil, err
}
Expand All @@ -159,7 +137,6 @@ func (cfg *Config) bind() (*Build, error) {
Objects: objects,
Interfaces: cfg.buildInterfaces(namedTypes, prog),
Inputs: inputs,
Imports: imports.finalize(),
SchemaRaw: cfg.SchemaStr,
SchemaFilename: cfg.SchemaFilename,
Directives: directives,
Expand All @@ -182,29 +159,25 @@ func (cfg *Config) bind() (*Build, error) {
}

func (cfg *Config) validate() error {
progLoader := newLoader(cfg.buildNamedTypes(), false)
progLoader := cfg.newLoaderWithErrors()
_, err := progLoader.Load()
return err
}

func newLoader(namedTypes NamedTypes, allowErrors bool) loader.Config {
func (cfg *Config) newLoaderWithErrors() loader.Config {
conf := loader.Config{}
if allowErrors {
conf = loader.Config{
AllowErrors: true,
TypeChecker: types.Config{
Error: func(e error) {},
},
}
}
for _, imp := range ambientImports {
conf.Import(imp)
}

for _, imp := range namedTypes {
if imp.Package != "" {
conf.Import(imp.Package)
}

for _, pkg := range cfg.Models.referencedPackages() {
conf.Import(pkg)
}
return conf
}

func (cfg *Config) newLoaderWithoutErrors() loader.Config {
conf := cfg.newLoaderWithErrors()
conf.AllowErrors = true
conf.TypeChecker = types.Config{
Error: func(e error) {},
}
return conf
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestGenerateServer(t *testing.T) {
name := "graphserver"
schema := `
type Query {
user(): User
user: User
}
type User {
id: Int
Expand Down
31 changes: 31 additions & 0 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"

"github.com/99designs/gqlgen/internal/gopath"
Expand Down Expand Up @@ -209,6 +210,36 @@ func (tm TypeMap) Check() error {
return nil
}

func (tm TypeMap) referencedPackages() []string {
var pkgs []string

for _, typ := range tm {
if typ.Model == "map[string]interface{}" {
continue
}
pkg, _ := pkgAndType(typ.Model)
if pkg == "" || inStrSlice(pkgs, pkg) {
continue
}
pkgs = append(pkgs, pkg)
}

sort.Slice(pkgs, func(i, j int) bool {
return pkgs[i] > pkgs[j]
})
return pkgs
}

func inStrSlice(haystack []string, needle string) bool {
for _, v := range haystack {
if needle == v {
return true
}
}

return false
}

// findCfg searches for the config file in this directory and all parents up the tree
// looking for the closest match
func findCfg() (string, error) {
Expand Down
23 changes: 23 additions & 0 deletions codegen/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -56,3 +58,24 @@ func TestLoadDefaultConfig(t *testing.T) {
require.True(t, os.IsNotExist(err))
})
}

func TestReferencedPackages(t *testing.T) {
t.Run("valid", func(t *testing.T) {
tm := TypeMap{
"Foo": {Model: "github.com/test.Foo"},
"Bar": {Model: "github.com/test.Bar"},
"Baz": {Model: "github.com/otherpkg.Baz"},
"Map": {Model: "map[string]interface{}"},
"SkipResolver": {
Fields: map[string]TypeMapField{
"field": {Resolver: false},
},
},
}

pkgs := tm.referencedPackages()

assert.Equal(t, []string{"github.com/test", "github.com/otherpkg"}, pkgs)
})

}
29 changes: 0 additions & 29 deletions codegen/import.go

This file was deleted.

Loading

0 comments on commit 6bea1d8

Please sign in to comment.