diff --git a/cmd/init.go b/cmd/init.go index 45bf7a6c78b..fabe6a18d8d 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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 != "" { @@ -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) @@ -130,6 +155,8 @@ func initConfig() { fmt.Fprintln(os.Stderr, "unable to write config file: "+err.Error()) os.Exit(1) } + + return config } func initSchema() {