Skip to content

Commit

Permalink
Generate code after init
Browse files Browse the repository at this point in the history
  • Loading branch information
creativej committed Jul 31, 2018
1 parent 58831ac commit df95f00
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,32 @@ var initCmd = &cobra.Command{
Long: "",
Run: func(cmd *cobra.Command, args []string) {
initSchema()
initConfig()
config := initConfig()

generateGraph(config)
},
}

func initConfig() {
func generateGraph(config *codegen.Config) {
schemaRaw, err := ioutil.ReadFile(config.SchemaFilename)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error())
os.Exit(1)
}
config.SchemaStr = string(schemaRaw)

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

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

func initConfig() *codegen.Config {
var config *codegen.Config
var err error
if configFilename != "" {
Expand All @@ -84,6 +105,10 @@ func initConfig() {
configFilename = ".gqlgen.yml"
}
config = codegen.DefaultConfig()
config.Resolver = codegen.PackageConfig{
Filename: "resolver.go",
Type: "Resolver",
}
} else if config != nil {
fmt.Fprintln(os.Stderr, "config file is already exists")
os.Exit(0)
Expand Down Expand Up @@ -130,6 +155,8 @@ func initConfig() {
fmt.Fprintln(os.Stderr, "unable to write config file: "+err.Error())
os.Exit(1)
}

return config
}

func initSchema() {
Expand Down

0 comments on commit df95f00

Please sign in to comment.