Skip to content

Commit

Permalink
Merge branch 'master' into query-complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
edsrzf committed Aug 30, 2018
2 parents 6e408d5 + d08b9c4 commit 2ab857e
Show file tree
Hide file tree
Showing 35 changed files with 2,992 additions and 705 deletions.
48 changes: 16 additions & 32 deletions Gopkg.lock

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

78 changes: 10 additions & 68 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ import (

"github.com/99designs/gqlgen/codegen"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/urfave/cli"
)

func init() {
rootCmd.AddCommand(genCmd)
}

var genCmd = &cobra.Command{
Use: "gen",
Short: "Generate models & resolvers .go",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
var genCmd = cli.Command{
Name: "generate",
Usage: "generate a graphql server based on schema",
Flags: []cli.Flag{
cli.BoolFlag{Name: "verbose, v", Usage: "show logs"},
cli.StringFlag{Name: "config, c", Usage: "the config filename"},
},
Action: func(ctx *cli.Context) {
var config *codegen.Config
var err error
if configFilename != "" {
if configFilename := ctx.String("config"); configFilename != "" {
config, err = codegen.LoadConfig(configFilename)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
Expand All @@ -38,28 +36,6 @@ var genCmd = &cobra.Command{
}
}

// overwrite by commandline options
var emitYamlGuidance bool
if schemaFilename != "" {
config.SchemaFilename = schemaFilename
}
if models != "" {
config.Model.Filename = models
}
if output != "" {
config.Exec.Filename = output
}
if packageName != "" {
config.Exec.Package = packageName
}
if modelPackageName != "" {
config.Model.Package = modelPackageName
}
if typemap != "" {
config.Models = loadModelMap()
emitYamlGuidance = true
}

schemaRaw, err := ioutil.ReadFile(config.SchemaFilename)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error())
Expand All @@ -72,44 +48,10 @@ var genCmd = &cobra.Command{
os.Exit(1)
}

if emitYamlGuidance {
var b []byte
b, err = yaml.Marshal(config)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to marshal yaml: "+err.Error())
os.Exit(1)
}

fmt.Fprintf(os.Stderr, "DEPRECATION WARNING: we are moving away from the json typemap, instead create a gqlgen.yml with the following content:\n\n%s\n", string(b))
}

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

func loadModelMap() codegen.TypeMap {
var goTypes map[string]string
b, err := ioutil.ReadFile(typemap)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open typemap: "+err.Error())
return nil
}

if err = yaml.Unmarshal(b, &goTypes); err != nil {
fmt.Fprintln(os.Stderr, "unable to parse typemap: "+err.Error())
os.Exit(1)
}

typeMap := make(codegen.TypeMap)
for typeName, entityPath := range goTypes {
typeMap[typeName] = codegen.TypeMapEntry{
Model: entityPath,
}
}

return typeMap
}
61 changes: 18 additions & 43 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ import (

"github.com/99designs/gqlgen/codegen"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)

func init() {
rootCmd.AddCommand(initCmd)
}

var configComment = `
# .gqlgen.yml example
#
Expand Down Expand Up @@ -55,19 +51,24 @@ type Mutation {
}
`

var initCmd = &cobra.Command{
Use: "init",
Short: "Generate gqlgen skeleton",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
initSchema()
config := initConfig()
var initCmd = cli.Command{
Name: "init",
Usage: "create a new gqlgen project",
Flags: []cli.Flag{
cli.BoolFlag{Name: "verbose, v", Usage: "show logs"},
cli.StringFlag{Name: "config, c", Usage: "the config filename"},
cli.StringFlag{Name: "server", Usage: "where to write the server stub to", Value: "server/server.go"},
cli.StringFlag{Name: "schema", Usage: "where to write the schema stub to", Value: "schema.graphql"},
},
Action: func(ctx *cli.Context) {
initSchema(ctx.String("schema"))
config := initConfig(ctx)

GenerateGraphServer(config)
GenerateGraphServer(config, ctx.String("server"))
},
}

func GenerateGraphServer(config *codegen.Config) {
func GenerateGraphServer(config *codegen.Config, serverFilename string) {
schemaRaw, err := ioutil.ReadFile(config.SchemaFilename)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error())
Expand All @@ -80,10 +81,6 @@ func GenerateGraphServer(config *codegen.Config) {
os.Exit(1)
}

if serverFilename == "" {
serverFilename = "server/server.go"
}

if err := codegen.Generate(*config); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
Expand All @@ -97,9 +94,10 @@ func GenerateGraphServer(config *codegen.Config) {
fmt.Fprintf(os.Stdout, "Exec \"go run ./%s\" to start GraphQL server\n", serverFilename)
}

func initConfig() *codegen.Config {
func initConfig(ctx *cli.Context) *codegen.Config {
var config *codegen.Config
var err error
configFilename := ctx.String("config")
if configFilename != "" {
config, err = codegen.LoadConfig(configFilename)
} else {
Expand All @@ -126,25 +124,6 @@ func initConfig() *codegen.Config {
Type: "Resolver",
}

if schemaFilename != "" {
config.SchemaFilename = schemaFilename
}
if models != "" {
config.Model.Filename = models
}
if output != "" {
config.Exec.Filename = output
}
if packageName != "" {
config.Exec.Package = packageName
}
if modelPackageName != "" {
config.Model.Package = modelPackageName
}
if typemap != "" {
config.Models = loadModelMap()
}

var buf bytes.Buffer
buf.WriteString(strings.TrimSpace(configComment))
buf.WriteString("\n\n")
Expand All @@ -167,11 +146,7 @@ func initConfig() *codegen.Config {
return config
}

func initSchema() {
if schemaFilename == "" {
schemaFilename = "schema.graphql"
}

func initSchema(schemaFilename string) {
_, err := os.Stat(schemaFilename)
if !os.IsNotExist(err) {
return
Expand Down
Loading

0 comments on commit 2ab857e

Please sign in to comment.