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

support .gqlgen.yml #163

Merged
merged 4 commits into from
Jul 5, 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
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ required = ["github.com/vektah/dataloaden"]
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "gopkg.in/yaml.v2"
version = "2.2.1"
106 changes: 75 additions & 31 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,60 @@ import (
)

type Config struct {
SchemaStr string
Typemap map[string]string
SchemaFilename string `yaml:"schema,omitempty"`
SchemaStr string `yaml:"-"`
Typemap TypeMap `yaml:"models,omitempty"`

schema *schema.Schema
schema *schema.Schema `yaml:"-"`

ExecFilename string
ExecPackageName string
execPackagePath string
execDir string
ExecFilename string `yaml:"output,omitempty"`
ExecPackageName string `yaml:"package,omitempty"`
execPackagePath string `yaml:"-"`
execDir string `yaml:"-"`

ModelFilename string
ModelPackageName string
modelPackagePath string
modelDir string
ModelFilename string `yaml:"modeloutput,omitempty"`
ModelPackageName string `yaml:"modelpackage,omitempty"`
modelPackagePath string `yaml:"-"`
modelDir string `yaml:"-"`
}

func (cfg *Config) Check() error {
err := cfg.Typemap.Check()
if err != nil {
return fmt.Errorf("config: %s", err.Error())
}
return nil
}

type TypeMap map[string]TypeMapEntry

func (tm TypeMap) Exists(typeName string) bool {
return tm.Get(typeName) != nil
}

func (tm TypeMap) Get(typeName string) *TypeMapEntry {
entry, ok := tm[typeName]
if !ok {
return nil
}
return &entry
}

func (tm TypeMap) Check() error {
for typeName, entry := range tm {
if entry.Model == "" {
return fmt.Errorf("model %s: entityPath is not defined", typeName)
}
}
return nil
}

type TypeMapEntry struct {
Model string `yaml:"model"`
Fields map[string]TypeMapField `yaml:"fields,omitempty"`
}

type TypeMapField struct {
}

func Generate(cfg Config) error {
Expand All @@ -58,11 +98,15 @@ func Generate(cfg Config) error {
return err
}
for _, model := range modelsBuild.Models {
cfg.Typemap[model.GQLType] = cfg.modelPackagePath + "." + model.GoType
cfg.Typemap[model.GQLType] = TypeMapEntry{
Model: cfg.modelPackagePath + "." + model.GoType,
}
}

for _, enum := range modelsBuild.Enums {
cfg.Typemap[enum.GQLType] = cfg.modelPackagePath + "." + enum.GoType
cfg.Typemap[enum.GQLType] = TypeMapEntry{
Model: cfg.modelPackagePath + "." + enum.GoType,
}
}
}

Expand Down Expand Up @@ -108,28 +152,28 @@ func (cfg *Config) normalize() error {
cfg.ExecPackageName = sanitizePackageName(cfg.ExecPackageName)
cfg.execPackagePath = fullPackageName(cfg.execDir, cfg.ExecPackageName)

builtins := map[string]string{
"__Directive": "github.com/vektah/gqlgen/neelance/introspection.Directive",
"__Type": "github.com/vektah/gqlgen/neelance/introspection.Type",
"__Field": "github.com/vektah/gqlgen/neelance/introspection.Field",
"__EnumValue": "github.com/vektah/gqlgen/neelance/introspection.EnumValue",
"__InputValue": "github.com/vektah/gqlgen/neelance/introspection.InputValue",
"__Schema": "github.com/vektah/gqlgen/neelance/introspection.Schema",
"Int": "github.com/vektah/gqlgen/graphql.Int",
"Float": "github.com/vektah/gqlgen/graphql.Float",
"String": "github.com/vektah/gqlgen/graphql.String",
"Boolean": "github.com/vektah/gqlgen/graphql.Boolean",
"ID": "github.com/vektah/gqlgen/graphql.ID",
"Time": "github.com/vektah/gqlgen/graphql.Time",
"Map": "github.com/vektah/gqlgen/graphql.Map",
builtins := TypeMap{
"__Directive": {Model: "github.com/vektah/gqlgen/neelance/introspection.Directive"},
"__Type": {Model: "github.com/vektah/gqlgen/neelance/introspection.Type"},
"__Field": {Model: "github.com/vektah/gqlgen/neelance/introspection.Field"},
"__EnumValue": {Model: "github.com/vektah/gqlgen/neelance/introspection.EnumValue"},
"__InputValue": {Model: "github.com/vektah/gqlgen/neelance/introspection.InputValue"},
"__Schema": {Model: "github.com/vektah/gqlgen/neelance/introspection.Schema"},
"Int": {Model: "github.com/vektah/gqlgen/graphql.Int"},
"Float": {Model: "github.com/vektah/gqlgen/graphql.Float"},
"String": {Model: "github.com/vektah/gqlgen/graphql.String"},
"Boolean": {Model: "github.com/vektah/gqlgen/graphql.Boolean"},
"ID": {Model: "github.com/vektah/gqlgen/graphql.ID"},
"Time": {Model: "github.com/vektah/gqlgen/graphql.Time"},
"Map": {Model: "github.com/vektah/gqlgen/graphql.Map"},
}

if cfg.Typemap == nil {
cfg.Typemap = map[string]string{}
cfg.Typemap = TypeMap{}
}
for k, v := range builtins {
if _, ok := cfg.Typemap[k]; !ok {
cfg.Typemap[k] = v
for typeName, entry := range builtins {
if !cfg.Typemap.Exists(typeName) {
cfg.Typemap[typeName] = entry
}
}

Expand Down
8 changes: 4 additions & 4 deletions codegen/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func TestInvalidPackagenames(t *testing.T) {
type InvalidIdentifier {
id: Int!
}
`, map[string]string{
"InvalidIdentifier": "github.com/vektah/gqlgen/codegen/testdata/invalid-packagename.InvalidIdentifier",
`, TypeMap{
"InvalidIdentifier": {Model: "github.com/vektah/gqlgen/codegen/testdata/invalid-packagename.InvalidIdentifier"},
})

require.NoError(t, err)
Expand All @@ -30,8 +30,8 @@ func TestImportCollisions(t *testing.T) {
id: ID!
}

`, map[string]string{
"It": "github.com/vektah/gqlgen/codegen/testdata/introspection.It",
`, TypeMap{
"It": {Model: "github.com/vektah/gqlgen/codegen/testdata/introspection.It"},
})

require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions codegen/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestRawMapInputs(t *testing.T) {
a: Int
b: Int
}
`, map[string]string{
"Changes": "map[string]interface{}",
`, TypeMap{
"Changes": {Model: "map[string]interface{}"},
})

require.NoError(t, err)
Expand All @@ -56,8 +56,8 @@ func TestRecursiveInputType(t *testing.T) {
input RecursiveInputSlice {
self: [RecursiveInputSlice!]
}
`, map[string]string{
"RecursiveInputSlice": "github.com/vektah/gqlgen/codegen/testdata.RecursiveInputSlice",
`, TypeMap{
"RecursiveInputSlice": {Model: "github.com/vektah/gqlgen/codegen/testdata.RecursiveInputSlice"},
})

require.NoError(t, err)
Expand All @@ -84,8 +84,8 @@ func TestComplexInputTypes(t *testing.T) {
type InnerObject {
id: Int!
}
`, map[string]string{
"Changes": "map[string]interface{}",
`, TypeMap{
"Changes": {Model: "map[string]interface{}"},
})

require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions codegen/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ func TestShapes(t *testing.T) {
area: Float
}
union ShapeUnion = Circle | Rectangle
`, map[string]string{
"Shape": "github.com/vektah/gqlgen/codegen/testdata.Shape",
"ShapeUnion": "github.com/vektah/gqlgen/codegen/testdata.ShapeUnion",
"Circle": "github.com/vektah/gqlgen/codegen/testdata.Circle",
"Rectangle": "github.com/vektah/gqlgen/codegen/testdata.Rectangle",
`, TypeMap{
"Shape": {Model: "github.com/vektah/gqlgen/codegen/testdata.Shape"},
"ShapeUnion": {Model: "github.com/vektah/gqlgen/codegen/testdata.ShapeUnion"},
"Circle": {Model: "github.com/vektah/gqlgen/codegen/testdata.Circle"},
"Rectangle": {Model: "github.com/vektah/gqlgen/codegen/testdata.Rectangle"},
})

require.NoError(t, err)

}

func generate(name string, schema string, typemap ...map[string]string) error {
func generate(name string, schema string, typemap ...TypeMap) error {
cfg := Config{
SchemaStr: schema,
ExecFilename: "testdata/gen/" + name + "/exec.go",
Expand Down
4 changes: 2 additions & 2 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package templates
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
"text/template"
"unicode"
"sort"
)

func Run(name string, tpldata interface{}) (*bytes.Buffer, error) {
Expand Down Expand Up @@ -120,7 +120,7 @@ func dump(val interface{}) string {

for _, key := range keys {
data := val[key]

buf.WriteString(strconv.Quote(key))
buf.WriteString(":")
buf.WriteString(dump(data))
Expand Down
5 changes: 4 additions & 1 deletion codegen/type_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ func (cfg *Config) buildNamedTypes() NamedTypes {
for _, schemaType := range cfg.schema.Types {
t := namedTypeFromSchema(schemaType)

userType := cfg.Typemap[t.GQLType]
userType := ""
if userEntry := cfg.Typemap.Get(t.GQLType); userEntry != nil {
userType = userEntry.Model
}
t.IsUserDefined = userType != ""
if userType == "" && t.IsScalar {
userType = "github.com/vektah/gqlgen/graphql.String"
Expand Down
Loading