From 9a5321317b96de2404c56d0a4dcae12f4beff78f Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Thu, 29 Mar 2018 12:31:40 +1100 Subject: [PATCH] Allow generated models to go into their own package --- main.go | 30 ++++++++++--------- test/generated.go | 33 +++++++++++---------- test/{models_gen.go => models/generated.go} | 2 +- test/resolvers_test.go | 13 ++++---- 4 files changed, 41 insertions(+), 37 deletions(-) rename test/{models_gen.go => models/generated.go} (93%) diff --git a/main.go b/main.go index 28fbe4c63d6..95fe3866268 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,10 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" "syscall" + "strings" + "github.com/vektah/gqlgen/codegen" "github.com/vektah/gqlgen/codegen/templates" "github.com/vektah/gqlgen/neelance/schema" @@ -22,6 +23,7 @@ var models = flag.String("models", "models_gen.go", "the file to write the model var schemaFilename = flag.String("schema", "schema.graphql", "the graphql schema to generate types from") var typemap = flag.String("typemap", "", "a json map going from graphql to golang types") var packageName = flag.String("package", "", "the package name") +var modelPackageName = flag.String("modelpackage", "", "the package name to use for models") var help = flag.Bool("h", false, "this usage text") func main() { @@ -54,7 +56,7 @@ func main() { types := loadTypeMap() - modelsBuild := codegen.Models(schema, types, dirName()) + modelsBuild := codegen.Models(schema, types, dirName(*models)) if len(modelsBuild.Models) > 0 { if *packageName != "" { modelsBuild.PackageName = *packageName @@ -67,7 +69,7 @@ func main() { } write(*models, buf.Bytes()) - pkgName := fullPackageName() + pkgName := fullPackageName(*models, *modelPackageName) for _, model := range modelsBuild.Models { types[model.GQLType] = pkgName + "." + model.GoType @@ -78,7 +80,7 @@ func main() { } } - build, err := codegen.Bind(schema, types, dirName()) + build, err := codegen.Bind(schema, types, dirName(*output)) if err != nil { fmt.Fprintln(os.Stderr, "failed to generate code: "+err.Error()) os.Exit(1) @@ -121,26 +123,22 @@ func write(filename string, b []byte) { } } -func absOutput() string { - absPath, err := filepath.Abs(*output) +func abs(path string) string { + absPath, err := filepath.Abs(path) if err != nil { panic(err) } return absPath } -func dirName() string { - return filepath.Dir(absOutput()) -} - -func fullPackageName() string { - absPath, err := filepath.Abs(*output) +func fullPackageName(file string, override string) string { + absPath, err := filepath.Abs(file) if err != nil { panic(err) } pkgName := filepath.Dir(absPath) - if *packageName != "" { - pkgName = filepath.Join(filepath.Dir(pkgName), *packageName) + if override != "" { + pkgName = filepath.Join(filepath.Dir(pkgName), override) } for _, gopath := range strings.Split(build.Default.GOPATH, ":") { @@ -152,6 +150,10 @@ func fullPackageName() string { return pkgName } +func dirName(path string) string { + return filepath.Dir(abs(path)) +} + func loadTypeMap() map[string]string { goTypes := map[string]string{ "__Directive": "github.com/vektah/gqlgen/neelance/introspection.Directive", diff --git a/test/generated.go b/test/generated.go index 7537947700e..eff50f80572 100644 --- a/test/generated.go +++ b/test/generated.go @@ -14,6 +14,7 @@ import ( query "github.com/vektah/gqlgen/neelance/query" schema "github.com/vektah/gqlgen/neelance/schema" introspection1 "github.com/vektah/gqlgen/test/introspection" + models "github.com/vektah/gqlgen/test/models" ) func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema { @@ -21,9 +22,9 @@ func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema { } type Resolvers interface { - OuterObject_inner(ctx context.Context, obj *OuterObject) (InnerObject, error) - Query_nestedInputs(ctx context.Context, input [][]OuterInput) (*bool, error) - Query_nestedOutputs(ctx context.Context) ([][]OuterObject, error) + OuterObject_inner(ctx context.Context, obj *models.OuterObject) (models.InnerObject, error) + Query_nestedInputs(ctx context.Context, input [][]models.OuterInput) (*bool, error) + Query_nestedOutputs(ctx context.Context) ([][]models.OuterObject, error) Query_shapes(ctx context.Context) ([]Shape, error) Query_recursive(ctx context.Context, input *RecursiveInputSlice) (*bool, error) Query_mapInput(ctx context.Context, input *map[string]interface{}) (*bool, error) @@ -102,7 +103,7 @@ func (ec *executionContext) _Circle_area(ctx context.Context, field graphql.Coll var innerObjectImplementors = []string{"InnerObject"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _InnerObject(ctx context.Context, sel []query.Selection, obj *InnerObject) graphql.Marshaler { +func (ec *executionContext) _InnerObject(ctx context.Context, sel []query.Selection, obj *models.InnerObject) graphql.Marshaler { fields := graphql.CollectFields(ec.Doc, sel, innerObjectImplementors, ec.Variables) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -121,7 +122,7 @@ func (ec *executionContext) _InnerObject(ctx context.Context, sel []query.Select return out } -func (ec *executionContext) _InnerObject_id(ctx context.Context, field graphql.CollectedField, obj *InnerObject) graphql.Marshaler { +func (ec *executionContext) _InnerObject_id(ctx context.Context, field graphql.CollectedField, obj *models.InnerObject) graphql.Marshaler { res := obj.ID return graphql.MarshalInt(res) } @@ -156,7 +157,7 @@ func (ec *executionContext) _It_id(ctx context.Context, field graphql.CollectedF var outerObjectImplementors = []string{"OuterObject"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _OuterObject(ctx context.Context, sel []query.Selection, obj *OuterObject) graphql.Marshaler { +func (ec *executionContext) _OuterObject(ctx context.Context, sel []query.Selection, obj *models.OuterObject) graphql.Marshaler { fields := graphql.CollectFields(ec.Doc, sel, outerObjectImplementors, ec.Variables) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -175,7 +176,7 @@ func (ec *executionContext) _OuterObject(ctx context.Context, sel []query.Select return out } -func (ec *executionContext) _OuterObject_inner(ctx context.Context, field graphql.CollectedField, obj *OuterObject) graphql.Marshaler { +func (ec *executionContext) _OuterObject_inner(ctx context.Context, field graphql.CollectedField, obj *models.OuterObject) graphql.Marshaler { return graphql.Defer(func() (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -231,14 +232,14 @@ func (ec *executionContext) _Query(ctx context.Context, sel []query.Selection) g } func (ec *executionContext) _Query_nestedInputs(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { - var arg0 [][]OuterInput + var arg0 [][]models.OuterInput if tmp, ok := field.Args["input"]; ok { var err error rawIf1 := tmp.([]interface{}) - arg0 = make([][]OuterInput, len(rawIf1)) + arg0 = make([][]models.OuterInput, len(rawIf1)) for idx1 := range rawIf1 { rawIf2 := rawIf1[idx1].([]interface{}) - arg0[idx1] = make([]OuterInput, len(rawIf2)) + arg0[idx1] = make([]models.OuterInput, len(rawIf2)) for idx2 := range rawIf2 { arg0[idx1][idx2], err = UnmarshalOuterInput(rawIf2[idx2]) } @@ -251,10 +252,10 @@ func (ec *executionContext) _Query_nestedInputs(ctx context.Context, field graph var tmp interface{} = []interface{}{[]interface{}{map[string]interface{}{"inner": map[string]interface{}{"id": 1}}}} var err error rawIf1 := tmp.([]interface{}) - arg0 = make([][]OuterInput, len(rawIf1)) + arg0 = make([][]models.OuterInput, len(rawIf1)) for idx1 := range rawIf1 { rawIf2 := rawIf1[idx1].([]interface{}) - arg0[idx1] = make([]OuterInput, len(rawIf2)) + arg0[idx1] = make([]models.OuterInput, len(rawIf2)) for idx2 := range rawIf2 { arg0[idx1][idx2], err = UnmarshalOuterInput(rawIf2[idx2]) } @@ -1016,8 +1017,8 @@ func (ec *executionContext) _ShapeUnion(ctx context.Context, sel []query.Selecti } } -func UnmarshalInnerInput(v interface{}) (InnerInput, error) { - var it InnerInput +func UnmarshalInnerInput(v interface{}) (models.InnerInput, error) { + var it models.InnerInput for k, v := range v.(map[string]interface{}) { switch k { @@ -1033,8 +1034,8 @@ func UnmarshalInnerInput(v interface{}) (InnerInput, error) { return it, nil } -func UnmarshalOuterInput(v interface{}) (OuterInput, error) { - var it OuterInput +func UnmarshalOuterInput(v interface{}) (models.OuterInput, error) { + var it models.OuterInput for k, v := range v.(map[string]interface{}) { switch k { diff --git a/test/models_gen.go b/test/models/generated.go similarity index 93% rename from test/models_gen.go rename to test/models/generated.go index 202227c0401..c3de8caa5a9 100644 --- a/test/models_gen.go +++ b/test/models/generated.go @@ -1,6 +1,6 @@ // This file was generated by github.com/vektah/gqlgen, DO NOT EDIT -package test +package models type InnerInput struct { ID int diff --git a/test/resolvers_test.go b/test/resolvers_test.go index 2f8ccec83be..39cdfcca38e 100644 --- a/test/resolvers_test.go +++ b/test/resolvers_test.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/vektah/gqlgen -out generated.go -typemap types.json +//go:generate gorunpkg github.com/vektah/gqlgen -out generated.go -typemap types.json -models models/generated.go package test @@ -14,6 +14,7 @@ import ( gqlerrors "github.com/vektah/gqlgen/neelance/errors" "github.com/vektah/gqlgen/neelance/query" "github.com/vektah/gqlgen/test/introspection" + "github.com/vektah/gqlgen/test/models" ) func TestCompiles(t *testing.T) {} @@ -76,11 +77,11 @@ func mkctx(doc *query.Document, errFn func(e error) string) context.Context { } type testResolvers struct { - inner InnerObject + inner models.InnerObject innerErr error nestedInputs *bool nestedInputsErr error - nestedOutputs [][]OuterObject + nestedOutputs [][]models.OuterObject nestedOutputsErr error } @@ -100,15 +101,15 @@ func (r *testResolvers) Query_collision(ctx context.Context) (*introspection.It, panic("implement me") } -func (r *testResolvers) OuterObject_inner(ctx context.Context, obj *OuterObject) (InnerObject, error) { +func (r *testResolvers) OuterObject_inner(ctx context.Context, obj *models.OuterObject) (models.InnerObject, error) { return r.inner, r.innerErr } -func (r *testResolvers) Query_nestedInputs(ctx context.Context, input [][]OuterInput) (*bool, error) { +func (r *testResolvers) Query_nestedInputs(ctx context.Context, input [][]models.OuterInput) (*bool, error) { return r.nestedInputs, r.nestedInputsErr } -func (r *testResolvers) Query_nestedOutputs(ctx context.Context) ([][]OuterObject, error) { +func (r *testResolvers) Query_nestedOutputs(ctx context.Context) ([][]models.OuterObject, error) { return r.nestedOutputs, r.nestedOutputsErr }