Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow generated models to go into their own package #70

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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, ":") {
Expand All @@ -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",
Expand Down
33 changes: 17 additions & 16 deletions test/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ 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 {
return &executableSchema{resolvers: resolvers}
}

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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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])
}
Expand All @@ -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])
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/models_gen.go → test/models/generated.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 7 additions & 6 deletions test/resolvers_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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) {}
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down