main.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/99designs/gqlgen/api"
  5. "github.com/99designs/gqlgen/codegen/config"
  6. "github.com/spf13/pflag"
  7. admin_api "gshopper.com/gshopify/admin-api"
  8. "os"
  9. )
  10. var (
  11. cfg = config.DefaultConfig()
  12. fModule = pflag.StringP("module", "m", "", "module name")
  13. )
  14. func init() {
  15. pflag.Parse()
  16. if fModule == nil || *fModule == "" {
  17. _, _ = fmt.Fprintf(os.Stderr, "specify module name; available modules are: %v\n", admin_api.AllModules)
  18. os.Exit(2)
  19. }
  20. }
  21. func main() {
  22. module := admin_api.Module(*fModule)
  23. if !module.IsValid() {
  24. _, _ = fmt.Fprintf(os.Stderr, "incorrect module name has been specified; available modules are: %v\n", admin_api.AllModules)
  25. os.Exit(2)
  26. }
  27. moduleSchema := fmt.Sprintf("subgraph/%s.tpl", module)
  28. f, err := os.OpenFile(moduleSchema, os.O_RDONLY, 0644)
  29. if err != nil {
  30. _, _ = fmt.Fprintf(os.Stderr, "unknown module; %v\n", err.Error())
  31. os.Exit(2)
  32. }
  33. defer f.Close()
  34. if stat, err := f.Stat(); err != nil || stat.Size() < 1 {
  35. fmt.Printf("illegal module definition schema")
  36. _, _ = fmt.Fprintf(os.Stderr, "illegal module definition schema")
  37. os.Exit(2)
  38. }
  39. // Federation
  40. cfg.Federation = config.PackageConfig{
  41. Filename: fmt.Sprintf("graph/%s/generated/federation.go", module),
  42. Version: 2,
  43. }
  44. // Exec
  45. cfg.Exec = config.ExecConfig{
  46. Filename: fmt.Sprintf("graph/%s/generated/generated.go", module),
  47. }
  48. // Model
  49. cfg.Model = config.PackageConfig{
  50. Filename: fmt.Sprintf("graph/%s/generated/models.go", module),
  51. }
  52. cfg.Resolver = config.ResolverConfig{
  53. Layout: config.LayoutFollowSchema,
  54. DirName: fmt.Sprintf("graph/%s", module),
  55. }
  56. // SchemaFilename
  57. cfg.SchemaFilename = config.StringList{
  58. moduleSchema,
  59. "ext/*.graphql",
  60. }
  61. if err := config.CompleteConfig(cfg); err != nil {
  62. _, _ = fmt.Fprintln(os.Stderr, err.Error())
  63. os.Exit(3)
  64. }
  65. if err := api.Generate(cfg); err != nil {
  66. _, _ = fmt.Fprintln(os.Stderr, err.Error())
  67. os.Exit(3)
  68. }
  69. os.Exit(0)
  70. }