diff options
author | Michael Muré <batolettre@gmail.com> | 2018-12-23 17:11:37 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-12-23 17:11:37 +0100 |
commit | 1410a1af75b1ab9ea3f980a7e372728f9a123abf (patch) | |
tree | e24db8f84c48b20158b1f1fd6d281d700421279c /vendor/github.com/99designs/gqlgen/cmd/init.go | |
parent | 8fc15a032f021c855abf66ed303c003d57c340ea (diff) | |
download | git-bug-1410a1af75b1ab9ea3f980a7e372728f9a123abf.tar.gz |
upgrade gqlgen to v0.7.1
Diffstat (limited to 'vendor/github.com/99designs/gqlgen/cmd/init.go')
-rw-r--r-- | vendor/github.com/99designs/gqlgen/cmd/init.go | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/vendor/github.com/99designs/gqlgen/cmd/init.go b/vendor/github.com/99designs/gqlgen/cmd/init.go new file mode 100644 index 00000000..1e7c18b9 --- /dev/null +++ b/vendor/github.com/99designs/gqlgen/cmd/init.go @@ -0,0 +1,162 @@ +package cmd + +import ( + "bytes" + "fmt" + "io/ioutil" + "os" + "strings" + + "github.com/99designs/gqlgen/codegen" + "github.com/pkg/errors" + "github.com/urfave/cli" + "gopkg.in/yaml.v2" +) + +var configComment = ` +# .gqlgen.yml example +# +# Refer to https://gqlgen.com/config/ +# for detailed .gqlgen.yml documentation. +` + +var schemaDefault = ` +# GraphQL schema example +# +# https://gqlgen.com/getting-started/ + +type Todo { + id: ID! + text: String! + done: Boolean! + user: User! +} + +type User { + id: ID! + name: String! +} + +type Query { + todos: [Todo!]! +} + +input NewTodo { + text: String! + userId: String! +} + +type Mutation { + createTodo(input: NewTodo!): Todo! +} +` + +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, ctx.String("server")) + }, +} + +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) + } + + if err := codegen.Generate(*config); err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + os.Exit(1) + } + + if err := codegen.GenerateServer(*config, 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 + var err error + configFilename := ctx.String("config") + if configFilename != "" { + config, err = codegen.LoadConfig(configFilename) + } else { + config, err = codegen.LoadConfigFromDefaultLocations() + } + + if config != nil { + fmt.Fprintf(os.Stderr, "init failed: a configuration file already exists at %s\n", config.FilePath) + os.Exit(1) + } + + if !os.IsNotExist(errors.Cause(err)) { + fmt.Fprintln(os.Stderr, err.Error()) + os.Exit(1) + } + + if configFilename == "" { + configFilename = "gqlgen.yml" + } + config = codegen.DefaultConfig() + + config.Resolver = codegen.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) + } + + err = ioutil.WriteFile(configFilename, buf.Bytes(), 0644) + if err != nil { + fmt.Fprintln(os.Stderr, "unable to write config file: "+err.Error()) + os.Exit(1) + } + + return config +} + +func initSchema(schemaFilename string) { + _, err := os.Stat(schemaFilename) + if !os.IsNotExist(err) { + return + } + + err = ioutil.WriteFile(schemaFilename, []byte(strings.TrimSpace(schemaDefault)), 0644) + if err != nil { + fmt.Fprintln(os.Stderr, "unable to write schema file: "+err.Error()) + os.Exit(1) + } +} |