package main import ( "fmt" "github.com/99designs/gqlgen/api" "github.com/99designs/gqlgen/codegen/config" "github.com/spf13/pflag" admin_api "gshopper.com/gshopify/admin-api" "os" ) var ( cfg = config.DefaultConfig() fModule = pflag.StringP("module", "m", "", "module name") ) func init() { pflag.Parse() if fModule == nil || *fModule == "" { _, _ = fmt.Fprintf(os.Stderr, "specify module name; available modules are: %v\n", admin_api.AllModules) os.Exit(2) } } func main() { module := admin_api.Module(*fModule) if !module.IsValid() { _, _ = fmt.Fprintf(os.Stderr, "incorrect module name has been specified; available modules are: %v\n", admin_api.AllModules) os.Exit(2) } moduleSchema := fmt.Sprintf("subgraph/%s.tpl", module) f, err := os.OpenFile(moduleSchema, os.O_RDONLY, 0644) if err != nil { _, _ = fmt.Fprintf(os.Stderr, "unknown module; %v\n", err.Error()) os.Exit(2) } defer f.Close() if stat, err := f.Stat(); err != nil || stat.Size() < 1 { fmt.Printf("illegal module definition schema") _, _ = fmt.Fprintf(os.Stderr, "illegal module definition schema") os.Exit(2) } // Federation cfg.Federation = config.PackageConfig{ Filename: fmt.Sprintf("graph/%s/generated/federation.go", module), Version: 2, } // Exec cfg.Exec = config.ExecConfig{ Filename: fmt.Sprintf("graph/%s/generated/generated.go", module), } // Model cfg.Model = config.PackageConfig{ Filename: fmt.Sprintf("graph/%s/generated/models.go", module), } cfg.Resolver = config.ResolverConfig{ Layout: config.LayoutFollowSchema, DirName: fmt.Sprintf("graph/%s", module), } // SchemaFilename cfg.SchemaFilename = config.StringList{ moduleSchema, "ext/*.graphql", } if err := config.CompleteConfig(cfg); err != nil { _, _ = fmt.Fprintln(os.Stderr, err.Error()) os.Exit(3) } if err := api.Generate(cfg); err != nil { _, _ = fmt.Fprintln(os.Stderr, err.Error()) os.Exit(3) } os.Exit(0) }