From f46b7c8e38a8309243a46002cd9bb47b2f750dcb Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Thu, 10 Jan 2019 11:52:03 +1100 Subject: [PATCH] Reclaim main package for public interface to code generator --- codegen/ambient.go => ambient.go | 2 +- cmd/gen.go | 5 ++- cmd/init.go | 7 ++-- codegen/{unified => }/build.go | 2 +- codegen/{unified => }/build_bind.go | 2 +- codegen/{unified => }/build_bind_test.go | 2 +- codegen/{unified => }/build_directive.go | 2 +- codegen/{unified => }/build_enum.go | 2 +- codegen/{unified => }/build_interface.go | 2 +- codegen/{unified => }/build_object.go | 2 +- codegen/{unified => }/build_typedef.go | 2 +- codegen/{unified => }/directive.go | 2 +- codegen/{unified => }/enum.go | 2 +- codegen/{unified => }/interface.go | 2 +- codegen/{unified => }/object.go | 2 +- codegen/{unified => }/schema.go | 2 +- codegen/{unified => }/schema_test.go | 2 +- .../{unified => }/testdata/typeinput.graphqls | 0 .../testdata/unioninput.graphqls | 0 codegen/testserver/generated_test.go | 5 +-- codegen/{unified => }/type_definition.go | 2 +- codegen/{unified => }/type_reference.go | 2 +- codegen/{unified => }/util.go | 2 +- codegen/codegen_test.go => codegen_test.go | 2 +- example/chat/resolvers.go | 2 +- example/config/resolver.go | 2 +- example/dataloader/resolvers.go | 2 +- example/scalars/resolvers.go | 2 +- example/selection/.gqlgen.yml | 5 +++ example/selection/selection.go | 2 +- example/starwars/resolvers.go | 2 +- example/todo/todo.go | 2 +- example/type-system-extension/resolver.go | 2 +- codegen/exec.go => exec.go | 14 ++++---- codegen/generate.go => generate.go | 8 ++--- integration/resolver.go | 2 +- internal/imports/testdata/unused.go | 7 ++-- main.go | 13 -------- codegen/model.go => model.go | 12 +++---- codegen/resolver.go => resolver.go | 10 +++--- codegen/server.go => server.go | 6 ++-- .../generateserver.graphqls | 0 testdata/gqlgen.go | 32 +++++++++++++++++++ 43 files changed, 102 insertions(+), 78 deletions(-) rename codegen/ambient.go => ambient.go (95%) rename codegen/{unified => }/build.go (99%) rename codegen/{unified => }/build_bind.go (99%) rename codegen/{unified => }/build_bind_test.go (99%) rename codegen/{unified => }/build_directive.go (99%) rename codegen/{unified => }/build_enum.go (97%) rename codegen/{unified => }/build_interface.go (98%) rename codegen/{unified => }/build_object.go (99%) rename codegen/{unified => }/build_typedef.go (99%) rename codegen/{unified => }/directive.go (98%) rename codegen/{unified => }/enum.go (90%) rename codegen/{unified => }/interface.go (92%) rename codegen/{unified => }/object.go (99%) rename codegen/{unified => }/schema.go (96%) rename codegen/{unified => }/schema_test.go (98%) rename codegen/{unified => }/testdata/typeinput.graphqls (100%) rename codegen/{unified => }/testdata/unioninput.graphqls (100%) rename codegen/{unified => }/type_definition.go (98%) rename codegen/{unified => }/type_reference.go (99%) rename codegen/{unified => }/util.go (99%) rename codegen/codegen_test.go => codegen_test.go (98%) create mode 100644 example/selection/.gqlgen.yml rename codegen/exec.go => exec.go (75%) rename codegen/generate.go => generate.go (88%) delete mode 100644 main.go rename codegen/model.go => model.go (86%) rename codegen/resolver.go => resolver.go (78%) rename codegen/server.go => server.go (92%) rename {codegen/testdata => testdata}/generateserver.graphqls (100%) create mode 100644 testdata/gqlgen.go diff --git a/codegen/ambient.go b/ambient.go similarity index 95% rename from codegen/ambient.go rename to ambient.go index c9909fcc78..3ec2601cc9 100644 --- a/codegen/ambient.go +++ b/ambient.go @@ -1,4 +1,4 @@ -package codegen +package gqlgen import ( // Import and ignore the ambient imports listed below so dependency managers diff --git a/cmd/gen.go b/cmd/gen.go index 182231d625..6a077ec97e 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "github.com/99designs/gqlgen/codegen" + "github.com/99designs/gqlgen" "github.com/99designs/gqlgen/codegen/config" "github.com/pkg/errors" "github.com/urfave/cli" @@ -36,8 +36,7 @@ var genCmd = cli.Command{ } } - err = codegen.Generate(cfg) - if err != nil { + if err = gqlgen.Generate(cfg); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(3) } diff --git a/cmd/init.go b/cmd/init.go index 992a37b168..aad42ad5d6 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -7,7 +7,8 @@ import ( "os" "strings" - "github.com/99designs/gqlgen/codegen" + "github.com/99designs/gqlgen" + "github.com/99designs/gqlgen/codegen/config" "github.com/pkg/errors" "github.com/urfave/cli" @@ -70,12 +71,12 @@ var initCmd = cli.Command{ } func GenerateGraphServer(cfg *config.Config, serverFilename string) { - err := codegen.Generate(cfg) + err := gqlgen.Generate(cfg) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) } - if err := codegen.GenerateServer(serverFilename, cfg); err != nil { + if err := gqlgen.GenerateServer(serverFilename, cfg); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } diff --git a/codegen/unified/build.go b/codegen/build.go similarity index 99% rename from codegen/unified/build.go rename to codegen/build.go index 069c983737..588ca01d23 100644 --- a/codegen/unified/build.go +++ b/codegen/build.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "go/types" diff --git a/codegen/unified/build_bind.go b/codegen/build_bind.go similarity index 99% rename from codegen/unified/build_bind.go rename to codegen/build_bind.go index c1bd5ba5b9..208785ec6f 100644 --- a/codegen/unified/build_bind.go +++ b/codegen/build_bind.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "fmt" diff --git a/codegen/unified/build_bind_test.go b/codegen/build_bind_test.go similarity index 99% rename from codegen/unified/build_bind_test.go rename to codegen/build_bind_test.go index 341393b08a..ef4d6564b7 100644 --- a/codegen/unified/build_bind_test.go +++ b/codegen/build_bind_test.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "go/ast" diff --git a/codegen/unified/build_directive.go b/codegen/build_directive.go similarity index 99% rename from codegen/unified/build_directive.go rename to codegen/build_directive.go index d5cf1c3780..9edb653f4a 100644 --- a/codegen/unified/build_directive.go +++ b/codegen/build_directive.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "fmt" diff --git a/codegen/unified/build_enum.go b/codegen/build_enum.go similarity index 97% rename from codegen/unified/build_enum.go rename to codegen/build_enum.go index d11b67fc90..552fa8c425 100644 --- a/codegen/unified/build_enum.go +++ b/codegen/build_enum.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "go/types" diff --git a/codegen/unified/build_interface.go b/codegen/build_interface.go similarity index 98% rename from codegen/unified/build_interface.go rename to codegen/build_interface.go index 8acb697800..173f4805cd 100644 --- a/codegen/unified/build_interface.go +++ b/codegen/build_interface.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "go/types" diff --git a/codegen/unified/build_object.go b/codegen/build_object.go similarity index 99% rename from codegen/unified/build_object.go rename to codegen/build_object.go index ca29ffc73c..8e57651a68 100644 --- a/codegen/unified/build_object.go +++ b/codegen/build_object.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "go/types" diff --git a/codegen/unified/build_typedef.go b/codegen/build_typedef.go similarity index 99% rename from codegen/unified/build_typedef.go rename to codegen/build_typedef.go index 918140f0e2..44b6eafbda 100644 --- a/codegen/unified/build_typedef.go +++ b/codegen/build_typedef.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "fmt" diff --git a/codegen/unified/directive.go b/codegen/directive.go similarity index 98% rename from codegen/unified/directive.go rename to codegen/directive.go index 2cc65c244c..f9f000bafb 100644 --- a/codegen/unified/directive.go +++ b/codegen/directive.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "fmt" diff --git a/codegen/unified/enum.go b/codegen/enum.go similarity index 90% rename from codegen/unified/enum.go rename to codegen/enum.go index e7f365f678..526e9aa9f0 100644 --- a/codegen/unified/enum.go +++ b/codegen/enum.go @@ -1,4 +1,4 @@ -package unified +package codegen type Enum struct { Definition *TypeDefinition diff --git a/codegen/unified/interface.go b/codegen/interface.go similarity index 92% rename from codegen/unified/interface.go rename to codegen/interface.go index 3517b853cf..a8e422d5ef 100644 --- a/codegen/unified/interface.go +++ b/codegen/interface.go @@ -1,4 +1,4 @@ -package unified +package codegen type Interface struct { Definition *TypeDefinition diff --git a/codegen/unified/object.go b/codegen/object.go similarity index 99% rename from codegen/unified/object.go rename to codegen/object.go index 46598d2d3e..d35e39eacf 100644 --- a/codegen/unified/object.go +++ b/codegen/object.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "bytes" diff --git a/codegen/unified/schema.go b/codegen/schema.go similarity index 96% rename from codegen/unified/schema.go rename to codegen/schema.go index 0485c8121b..d9956d221b 100644 --- a/codegen/unified/schema.go +++ b/codegen/schema.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "github.com/99designs/gqlgen/codegen/config" diff --git a/codegen/unified/schema_test.go b/codegen/schema_test.go similarity index 98% rename from codegen/unified/schema_test.go rename to codegen/schema_test.go index fd2f6aeb4f..072500dd22 100644 --- a/codegen/unified/schema_test.go +++ b/codegen/schema_test.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "testing" diff --git a/codegen/unified/testdata/typeinput.graphqls b/codegen/testdata/typeinput.graphqls similarity index 100% rename from codegen/unified/testdata/typeinput.graphqls rename to codegen/testdata/typeinput.graphqls diff --git a/codegen/unified/testdata/unioninput.graphqls b/codegen/testdata/unioninput.graphqls similarity index 100% rename from codegen/unified/testdata/unioninput.graphqls rename to codegen/testdata/unioninput.graphqls diff --git a/codegen/testserver/generated_test.go b/codegen/testserver/generated_test.go index 50f62a60b9..8ea80bef1b 100644 --- a/codegen/testserver/generated_test.go +++ b/codegen/testserver/generated_test.go @@ -1,12 +1,11 @@ //go:generate rm -f resolver.go -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../../testdata/gqlgen.go package testserver import ( "context" "fmt" - "github.com/pkg/errors" "net/http" "net/http/httptest" "reflect" @@ -16,6 +15,8 @@ import ( "testing" "time" + "github.com/pkg/errors" + "github.com/99designs/gqlgen/graphql/introspection" "github.com/99designs/gqlgen/client" diff --git a/codegen/unified/type_definition.go b/codegen/type_definition.go similarity index 98% rename from codegen/unified/type_definition.go rename to codegen/type_definition.go index 49e91edf71..90655ea2e4 100644 --- a/codegen/unified/type_definition.go +++ b/codegen/type_definition.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "go/types" diff --git a/codegen/unified/type_reference.go b/codegen/type_reference.go similarity index 99% rename from codegen/unified/type_reference.go rename to codegen/type_reference.go index b420eaf523..107c62647f 100644 --- a/codegen/unified/type_reference.go +++ b/codegen/type_reference.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "go/types" diff --git a/codegen/unified/util.go b/codegen/util.go similarity index 99% rename from codegen/unified/util.go rename to codegen/util.go index 787bc3ad2e..c06801cdbe 100644 --- a/codegen/unified/util.go +++ b/codegen/util.go @@ -1,4 +1,4 @@ -package unified +package codegen import ( "go/build" diff --git a/codegen/codegen_test.go b/codegen_test.go similarity index 98% rename from codegen/codegen_test.go rename to codegen_test.go index 3045f286b2..0d24de856f 100644 --- a/codegen/codegen_test.go +++ b/codegen_test.go @@ -1,4 +1,4 @@ -package codegen +package gqlgen import ( "testing" diff --git a/example/chat/resolvers.go b/example/chat/resolvers.go index ff7a2af24c..735a1d4eeb 100644 --- a/example/chat/resolvers.go +++ b/example/chat/resolvers.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../../testdata/gqlgen.go package chat diff --git a/example/config/resolver.go b/example/config/resolver.go index 1e17bba53e..c089094fa3 100644 --- a/example/config/resolver.go +++ b/example/config/resolver.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../../testdata/gqlgen.go package config diff --git a/example/dataloader/resolvers.go b/example/dataloader/resolvers.go index 8c132b1f28..956251cdb0 100644 --- a/example/dataloader/resolvers.go +++ b/example/dataloader/resolvers.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../../testdata/gqlgen.go package dataloader diff --git a/example/scalars/resolvers.go b/example/scalars/resolvers.go index af7c62122e..3a89683fdb 100644 --- a/example/scalars/resolvers.go +++ b/example/scalars/resolvers.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../../testdata/gqlgen.go package scalars diff --git a/example/selection/.gqlgen.yml b/example/selection/.gqlgen.yml new file mode 100644 index 0000000000..846418d6bc --- /dev/null +++ b/example/selection/.gqlgen.yml @@ -0,0 +1,5 @@ +schema: schema.graphql +model: + filename: models_gen.go +exec: + filename: generated.go diff --git a/example/selection/selection.go b/example/selection/selection.go index 6e83692595..e423a8a7e1 100644 --- a/example/selection/selection.go +++ b/example/selection/selection.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../../testdata/gqlgen.go package selection diff --git a/example/starwars/resolvers.go b/example/starwars/resolvers.go index 55c4c3e019..a472ac22fa 100644 --- a/example/starwars/resolvers.go +++ b/example/starwars/resolvers.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../../testdata/gqlgen.go package starwars diff --git a/example/todo/todo.go b/example/todo/todo.go index 05846810e2..899f1ac3e6 100644 --- a/example/todo/todo.go +++ b/example/todo/todo.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../../testdata/gqlgen.go package todo diff --git a/example/type-system-extension/resolver.go b/example/type-system-extension/resolver.go index 73215cb8b2..5a8d590cd0 100644 --- a/example/type-system-extension/resolver.go +++ b/example/type-system-extension/resolver.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../../testdata/gqlgen.go package type_system_extension diff --git a/codegen/exec.go b/exec.go similarity index 75% rename from codegen/exec.go rename to exec.go index 34f5cbd9cd..4ec4f7c666 100644 --- a/codegen/exec.go +++ b/exec.go @@ -1,23 +1,23 @@ -package codegen +package gqlgen import ( "fmt" + "github.com/99designs/gqlgen/codegen" "github.com/99designs/gqlgen/codegen/templates" - "github.com/99designs/gqlgen/codegen/unified" ) type ExecBuild struct { - *unified.Schema + *codegen.Schema PackageName string - QueryRoot *unified.Object - MutationRoot *unified.Object - SubscriptionRoot *unified.Object + QueryRoot *codegen.Object + MutationRoot *codegen.Object + SubscriptionRoot *codegen.Object } // bind a schema together with some code to generate a Build -func buildExec(s *unified.Schema) error { +func buildExec(s *codegen.Schema) error { b := &ExecBuild{ Schema: s, PackageName: s.Config.Exec.Package, diff --git a/codegen/generate.go b/generate.go similarity index 88% rename from codegen/generate.go rename to generate.go index 34ab76f6f6..b339fd7b38 100644 --- a/codegen/generate.go +++ b/generate.go @@ -1,11 +1,11 @@ -package codegen +package gqlgen import ( "path/filepath" "syscall" + "github.com/99designs/gqlgen/codegen" "github.com/99designs/gqlgen/codegen/config" - "github.com/99designs/gqlgen/codegen/unified" "github.com/pkg/errors" ) @@ -13,7 +13,7 @@ func Generate(cfg *config.Config) error { _ = syscall.Unlink(cfg.Exec.Filename) _ = syscall.Unlink(cfg.Model.Filename) - schema, err := unified.NewSchema(cfg) + schema, err := codegen.NewSchema(cfg) if err != nil { return errors.Wrap(err, "merging failed") } @@ -23,7 +23,7 @@ func Generate(cfg *config.Config) error { } // Merge again now that the generated models have been injected into the typemap - schema, err = unified.NewSchema(schema.Config) + schema, err = codegen.NewSchema(schema.Config) if err != nil { return errors.Wrap(err, "merging failed") } diff --git a/integration/resolver.go b/integration/resolver.go index c0486f7b87..5f28e1d741 100644 --- a/integration/resolver.go +++ b/integration/resolver.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen +//go:generate go run ../testdata/gqlgen.go package integration diff --git a/internal/imports/testdata/unused.go b/internal/imports/testdata/unused.go index 63ea8d7f84..db4c9b4d27 100644 --- a/internal/imports/testdata/unused.go +++ b/internal/imports/testdata/unused.go @@ -1,6 +1,5 @@ package testdata -import "unused" import _ "underscore" import a "fmt" import "time" @@ -14,8 +13,8 @@ func fn() { } type Message struct { - ID string `json:"id"` - Text string `json:"text"` - CreatedBy string `json:"createdBy"` + ID string `json:"id"` + Text string `json:"text"` + CreatedBy string `json:"createdBy"` CreatedAt time.Time `json:"createdAt"` } diff --git a/main.go b/main.go deleted file mode 100644 index 04c2bf5400..0000000000 --- a/main.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "fmt" - "os" - - "github.com/99designs/gqlgen/cmd" -) - -func main() { - fmt.Fprintf(os.Stderr, "warning: running gqlgen from this binary is deprecated and may be removed in a future release. See https://github.com/99designs/gqlgen/issues/415\n") - cmd.Execute() -} diff --git a/codegen/model.go b/model.go similarity index 86% rename from codegen/model.go rename to model.go index df377b9cfd..ec0dd65f67 100644 --- a/codegen/model.go +++ b/model.go @@ -1,23 +1,23 @@ -package codegen +package gqlgen import ( "sort" "go/types" + "github.com/99designs/gqlgen/codegen" "github.com/99designs/gqlgen/codegen/templates" - "github.com/99designs/gqlgen/codegen/unified" ) type ModelBuild struct { - Interfaces []*unified.Interface - Models unified.Objects - Enums []unified.Enum + Interfaces []*codegen.Interface + Models codegen.Objects + Enums []codegen.Enum PackageName string } // Create a list of models that need to be generated -func buildModels(s *unified.Schema) error { +func buildModels(s *codegen.Schema) error { b := &ModelBuild{ PackageName: s.Config.Model.Package, } diff --git a/codegen/resolver.go b/resolver.go similarity index 78% rename from codegen/resolver.go rename to resolver.go index a917917c24..8146856481 100644 --- a/codegen/resolver.go +++ b/resolver.go @@ -1,22 +1,22 @@ -package codegen +package gqlgen import ( "log" "os" + "github.com/99designs/gqlgen/codegen" "github.com/99designs/gqlgen/codegen/templates" - "github.com/99designs/gqlgen/codegen/unified" "github.com/pkg/errors" ) type ResolverBuild struct { - *unified.Schema + *codegen.Schema PackageName string ResolverType string } -func GenerateResolver(schema *unified.Schema) error { +func GenerateResolver(schema *codegen.Schema) error { resolverBuild, err := buildResolver(schema) if err != nil { return errors.Wrap(err, "resolver build failed") @@ -34,7 +34,7 @@ func GenerateResolver(schema *unified.Schema) error { return nil } -func buildResolver(s *unified.Schema) (*ResolverBuild, error) { +func buildResolver(s *codegen.Schema) (*ResolverBuild, error) { return &ResolverBuild{ Schema: s, PackageName: s.Config.Resolver.Package, diff --git a/codegen/server.go b/server.go similarity index 92% rename from codegen/server.go rename to server.go index 3a42f8baf1..39b19262e5 100644 --- a/codegen/server.go +++ b/server.go @@ -1,17 +1,17 @@ -package codegen +package gqlgen import ( "log" "os" + "github.com/99designs/gqlgen/codegen" "github.com/99designs/gqlgen/codegen/config" "github.com/99designs/gqlgen/codegen/templates" - "github.com/99designs/gqlgen/codegen/unified" "github.com/pkg/errors" ) type ServerBuild struct { - unified.Schema + codegen.Schema PackageName string ExecPackageName string diff --git a/codegen/testdata/generateserver.graphqls b/testdata/generateserver.graphqls similarity index 100% rename from codegen/testdata/generateserver.graphqls rename to testdata/generateserver.graphqls diff --git a/testdata/gqlgen.go b/testdata/gqlgen.go new file mode 100644 index 0000000000..6b37275a0d --- /dev/null +++ b/testdata/gqlgen.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "time" + + "github.com/99designs/gqlgen" + "github.com/99designs/gqlgen/codegen/config" +) + +func main() { + log.SetOutput(ioutil.Discard) + + start := time.Now() + + cfg, err := config.LoadConfigFromDefaultLocations() + if err != nil { + fmt.Fprintln(os.Stderr, "failed to load config", err.Error()) + os.Exit(2) + } + + err = gqlgen.Generate(cfg) + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + os.Exit(3) + } + + fmt.Printf("Generated %s in %4.2fs\n", cfg.Exec.ImportPath(), time.Since(start).Seconds()) +}