Skip to content

Commit

Permalink
Extract config into its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr authored and vektah committed Jan 5, 2019
1 parent 34b8787 commit bec38c7
Show file tree
Hide file tree
Showing 24 changed files with 324 additions and 242 deletions.
35 changes: 13 additions & 22 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"

"github.com/99designs/gqlgen/codegen"
"github.com/99designs/gqlgen/codegen/config"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
Expand All @@ -18,43 +18,34 @@ var genCmd = cli.Command{
cli.StringFlag{Name: "config, c", Usage: "the config filename"},
},
Action: func(ctx *cli.Context) {
var config *codegen.Config
var cfg *config.Config
var err error
if configFilename := ctx.String("config"); configFilename != "" {
config, err = codegen.LoadConfig(configFilename)
if configFilename := ctx.String("gen"); configFilename != "" {
cfg, err = config.LoadConfig(configFilename)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else {
config, err = codegen.LoadConfigFromDefaultLocations()
cfg, err = config.LoadConfigFromDefaultLocations()
if os.IsNotExist(errors.Cause(err)) {
config = codegen.DefaultConfig()
cfg = config.DefaultConfig()
} else if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}

for _, filename := range config.SchemaFilename {
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error())
os.Exit(1)
os.Exit(2)
}
config.SchemaStr[filename] = string(schemaRaw)
}

if err = config.Check(); err != nil {
fmt.Fprintln(os.Stderr, "invalid config format: "+err.Error())
os.Exit(1)
gen, err := codegen.New(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(3)
}

err = codegen.Generate(*config)
err = gen.Generate()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(2)
os.Exit(4)
}
},
}
58 changes: 24 additions & 34 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/99designs/gqlgen/codegen"
"github.com/99designs/gqlgen/codegen/config"
"github.com/pkg/errors"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -68,46 +69,37 @@ var initCmd = cli.Command{
},
}

func GenerateGraphServer(config *codegen.Config, serverFilename string) {
for _, filename := range config.SchemaFilename {
schemaRaw, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error())
os.Exit(1)
}
config.SchemaStr[filename] = string(schemaRaw)
}

if err := config.Check(); err != nil {
fmt.Fprintln(os.Stderr, "invalid config format: "+err.Error())
os.Exit(1)
func GenerateGraphServer(cfg *config.Config, serverFilename string) {
gen, err := codegen.New(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}

if err := codegen.Generate(*config); err != nil {
if err := gen.Generate(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

if err := codegen.GenerateServer(*config, serverFilename); err != nil {
if err := gen.GenerateServer(serverFilename); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

fmt.Fprintf(os.Stdout, "Exec \"go run ./%s\" to start GraphQL server\n", serverFilename)
}

func initConfig(ctx *cli.Context) *codegen.Config {
var config *codegen.Config
func initConfig(ctx *cli.Context) *config.Config {
var cfg *config.Config
var err error
configFilename := ctx.String("config")
configFilename := ctx.String("cfg")
if configFilename != "" {
config, err = codegen.LoadConfig(configFilename)
cfg, err = config.LoadConfig(configFilename)
} else {
config, err = codegen.LoadConfigFromDefaultLocations()
cfg, err = config.LoadConfigFromDefaultLocations()
}

if config != nil {
fmt.Fprintf(os.Stderr, "init failed: a configuration file already exists at %s\n", config.FilePath)
if cfg != nil {
fmt.Fprintf(os.Stderr, "init failed: a configuration file already exists\n")
os.Exit(1)
}

Expand All @@ -119,33 +111,31 @@ func initConfig(ctx *cli.Context) *codegen.Config {
if configFilename == "" {
configFilename = "gqlgen.yml"
}
config = codegen.DefaultConfig()
cfg = config.DefaultConfig()

config.Resolver = codegen.PackageConfig{
cfg.Resolver = config.PackageConfig{
Filename: "resolver.go",
Type: "Resolver",
}

var buf bytes.Buffer
buf.WriteString(strings.TrimSpace(configComment))
buf.WriteString("\n\n")
{
var b []byte
b, err = yaml.Marshal(config)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to marshal yaml: "+err.Error())
os.Exit(1)
}
buf.Write(b)
var b []byte
b, err = yaml.Marshal(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to marshal yaml: "+err.Error())
os.Exit(1)
}
buf.Write(b)

err = ioutil.WriteFile(configFilename, buf.Bytes(), 0644)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to write config file: "+err.Error())
fmt.Fprintln(os.Stderr, "unable to write cfg file: "+err.Error())
os.Exit(1)
}

return config
return cfg
}

func initSchema(schemaFilename string) {
Expand Down
19 changes: 10 additions & 9 deletions codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/types"
"os"

"github.com/99designs/gqlgen/codegen/config"
"github.com/pkg/errors"
"golang.org/x/tools/go/loader"
)
Expand All @@ -19,7 +20,7 @@ type Build struct {
MutationRoot *Object
SubscriptionRoot *Object
SchemaRaw map[string]string
SchemaFilename SchemaFilenames
SchemaFilename config.SchemaFilenames
Directives map[string]*Directive
}

Expand All @@ -43,7 +44,7 @@ type ServerBuild struct {
}

// Create a list of models that need to be generated
func (cfg *Config) models() (*ModelBuild, error) {
func (cfg *Generator) models() (*ModelBuild, error) {
namedTypes := cfg.buildNamedTypes()

progLoader := cfg.newLoaderWithoutErrors()
Expand All @@ -67,7 +68,7 @@ func (cfg *Config) models() (*ModelBuild, error) {
}

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

Expand Down Expand Up @@ -98,7 +99,7 @@ func (cfg *Config) resolver() (*ResolverBuild, error) {
}, nil
}

func (cfg *Config) server(destDir string) *ServerBuild {
func (cfg *Generator) server(destDir string) *ServerBuild {
return &ServerBuild{
PackageName: cfg.Resolver.Package,
ExecPackageName: cfg.Exec.ImportPath(),
Expand All @@ -107,7 +108,7 @@ func (cfg *Config) server(destDir string) *ServerBuild {
}

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

progLoader := cfg.newLoaderWithoutErrors()
Expand Down Expand Up @@ -158,22 +159,22 @@ func (cfg *Config) bind() (*Build, error) {
return b, nil
}

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

func (cfg *Config) newLoaderWithErrors() loader.Config {
func (cfg *Generator) newLoaderWithErrors() loader.Config {
conf := loader.Config{}

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

func (cfg *Config) newLoaderWithoutErrors() loader.Config {
func (cfg *Generator) newLoaderWithoutErrors() loader.Config {
conf := cfg.newLoaderWithErrors()
conf.AllowErrors = true
conf.TypeChecker = types.Config{
Expand Down
36 changes: 27 additions & 9 deletions codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"syscall"
"testing"

"github.com/99designs/gqlgen/codegen/config"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/gqlerror"
"golang.org/x/tools/go/loader"
)

Expand All @@ -24,21 +28,35 @@ func TestGenerateServer(t *testing.T) {
}
`
serverFilename := "gen/" + name + "/server/server.go"
cfg := Config{
SchemaFilename: SchemaFilenames{"schema.graphql"},
SchemaStr: map[string]string{"schema.graphql": schema},
Exec: PackageConfig{Filename: "gen/" + name + "/exec.go"},
Model: PackageConfig{Filename: "gen/" + name + "/model.go"},
Resolver: PackageConfig{Filename: "gen/" + name + "/resolver.go", Type: "Resolver"},
gen := Generator{
Config: &config.Config{
SchemaFilename: config.SchemaFilenames{"schema.graphql"},
Exec: config.PackageConfig{Filename: "gen/" + name + "/exec.go"},
Model: config.PackageConfig{Filename: "gen/" + name + "/model.go"},
Resolver: config.PackageConfig{Filename: "gen/" + name + "/resolver.go", Type: "Resolver"},
},

SchemaStr: map[string]string{"schema.graphql": schema},
}

err := gen.Config.Check()
if err != nil {
panic(err)
}

var gerr *gqlerror.Error
gen.schema, gerr = gqlparser.LoadSchema(&ast.Source{Name: "schema.graphql", Input: schema})
if gerr != nil {
panic(gerr)
}

_ = syscall.Unlink(cfg.Resolver.Filename)
_ = syscall.Unlink(gen.Resolver.Filename)
_ = syscall.Unlink(serverFilename)

err := Generate(cfg)
err = gen.Generate()
require.NoError(t, err)

err = GenerateServer(cfg, serverFilename)
err = gen.GenerateServer(serverFilename)
require.NoError(t, err)

conf := loader.Config{}
Expand Down
Loading

0 comments on commit bec38c7

Please sign in to comment.