Skip to content

Commit

Permalink
Implement types.yaml parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Jul 2, 2018
1 parent ecf5600 commit 5debbc6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 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"
6 changes: 3 additions & 3 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func (tm TypeMap) Get(typeName string) *TypeMapEntry {
}

type TypeMapEntry struct {
TypeName string
EntityPath string
TypeName string `yaml:"typeName"`
EntityPath string `yaml:"entityPath"`
Fields []TypeMapField
}

type TypeMapField struct {
FieldName string
FieldName string `yaml:"fieldName"`
}

func Generate(cfg Config) error {
Expand Down
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/vektah/gqlgen/codegen"
"gopkg.in/yaml.v2"
)

var output = flag.String("out", "generated.go", "the file to write to")
Expand Down Expand Up @@ -58,6 +60,19 @@ func loadTypeMap() codegen.TypeMap {
if *typemap == "" {
return nil
}

if strings.HasSuffix(*typemap, ".json") {
return loadTypeMapJSON()
}

return loadTypeMapYAML()
}

func loadTypeMapJSON() codegen.TypeMap {
if *typemap == "" {
return nil
}

b, err := ioutil.ReadFile(*typemap)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open typemap: "+err.Error())
Expand Down Expand Up @@ -89,3 +104,23 @@ func loadTypeMap() codegen.TypeMap {

return typeMap
}

func loadTypeMapYAML() codegen.TypeMap {
if *typemap == "" {
return nil
}

b, err := ioutil.ReadFile(*typemap)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open typemap: "+err.Error())
return nil
}

var typeMap codegen.TypeMap
if err = yaml.Unmarshal(b, &typeMap); err != nil {
fmt.Fprintln(os.Stderr, "unable to parse typemap: "+err.Error())
os.Exit(1)
}

return typeMap
}
2 changes: 1 addition & 1 deletion 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 -models models/generated.go
//go:generate gorunpkg github.com/vektah/gqlgen -out generated.go -typemap types.yaml -models models/generated.go

package test

Expand Down
11 changes: 0 additions & 11 deletions test/types.json

This file was deleted.

8 changes: 8 additions & 0 deletions test/types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- typeName: Element
entityPath: github.com/vektah/gqlgen/test.Element

- typeName: Viewer
entityPath: github.com/vektah/gqlgen/test.Viewer

- typeName: User
entityPath: remote_api.User

0 comments on commit 5debbc6

Please sign in to comment.