Skip to content

Commit

Permalink
Merge pull request #163 from vvakame/feat-types-json
Browse files Browse the repository at this point in the history
support .gqlgen.yml
  • Loading branch information
vektah authored Jul 5, 2018
2 parents faf095f + 8577cea commit f0a0861
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 82 deletions.
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 @@ -113,28 +157,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

0 comments on commit f0a0861

Please sign in to comment.