main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. fModule = pflag.StringP("module", "m", "", "module name")
  12. )
  13. func init() {
  14. pflag.Parse()
  15. if fModule == nil || *fModule == "" {
  16. _, _ = fmt.Fprintf(os.Stderr, "specify module name; available modules are: %v\n", admin_api.AllModules)
  17. os.Exit(2)
  18. }
  19. }
  20. func main() {
  21. var modules []admin_api.Module
  22. if *fModule == "all" {
  23. modules = admin_api.AllModules
  24. } else {
  25. module := admin_api.Module(*fModule)
  26. if !module.IsValid() {
  27. _, _ = fmt.Fprintf(os.Stderr, "incorrect module has been specified; available modules are: %v\n", admin_api.AllModules)
  28. os.Exit(2)
  29. }
  30. modules = append(modules, module)
  31. }
  32. for _, module := range modules {
  33. if err := generate(module); err != nil {
  34. _, _ = fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  35. os.Exit(2)
  36. }
  37. }
  38. os.Exit(0)
  39. }
  40. func generate(m admin_api.Module) error {
  41. schema := fmt.Sprintf("subgraph/%s.tpl", m)
  42. if stat, err := os.Stat(schema); err != nil || stat.Size() < 1 {
  43. return fmt.Errorf("illegal module schema definition")
  44. }
  45. cfg := makeConfig(m)
  46. if err := config.CompleteConfig(cfg); err != nil {
  47. return err
  48. }
  49. if err := api.Generate(cfg); err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. func makeConfig(m admin_api.Module) *config.Config {
  55. cfg := config.DefaultConfig()
  56. // Federation
  57. cfg.Federation = config.PackageConfig{
  58. Filename: fmt.Sprintf("graph/%s/generated/federation.go", m),
  59. Version: 2,
  60. }
  61. // Exec
  62. cfg.Exec = config.ExecConfig{
  63. Filename: fmt.Sprintf("graph/%s/generated/generated.go", m),
  64. }
  65. // Model
  66. cfg.Model = config.PackageConfig{
  67. Filename: fmt.Sprintf("graph/%s/generated/models.go", m),
  68. }
  69. // Resolver
  70. cfg.Resolver = config.ResolverConfig{
  71. Layout: config.LayoutFollowSchema,
  72. DirName: fmt.Sprintf("graph/%s", m),
  73. }
  74. // SchemaFilename
  75. cfg.SchemaFilename = config.StringList{
  76. fmt.Sprintf("subgraph/%s.tpl", m), "ext/*.graphql"}
  77. //cfg.Models = map[string]config.TypeMapEntry{
  78. // "DateTime": {
  79. // Model: config.StringList{"github.com/gshopify/service-wrapper/scalar.DateTime"},
  80. // },
  81. // "LanguageCode": {
  82. // Model: config.StringList{"github.com/gshopify/service-wrapper/model.LanguageCode"},
  83. // },
  84. // "CountryCode": {
  85. // Model: config.StringList{"github.com/gshopify/service-wrapper/model.CountryCode"},
  86. // },
  87. // "UnsignedInt64": {
  88. // Model: config.StringList{"github.com/gshopify/service-wrapper/model.UInt"},
  89. // },
  90. // "Decimal": {
  91. // Model: config.StringList{"github.com/gshopify/service-wrapper/scalar.Decimal"},
  92. // },
  93. //}
  94. return cfg
  95. }