Skip to content

Commit

Permalink
fixed imports and gofmted files
Browse files Browse the repository at this point in the history
  • Loading branch information
Антон Вершинин committed Sep 20, 2023
1 parent 8123191 commit 72d3d75
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 27 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/jkrajniak/graphql-codegen-go)](https://goreportcard.com/report/github.com/jkrajniak/graphql-codegen-go)
[![Go Report Card](https://goreportcard.com/badge/github.com/VerTox/graphql-codegen-go)](https://goreportcard.com/report/github.com/VerTox/graphql-codegen-go)

# graphql-codegen-go
Generate Go structs from your GraphQL schema.
Expand All @@ -9,7 +9,7 @@ can be located either locally or can be fetched from GIT repository.
Install it using `go get`:

```bash
$ go get -u github.com/jkrajniak/graphql-codegen-go
$ go get -u github.com/VerTox/graphql-codegen-go
```
## Quick start

Expand Down Expand Up @@ -116,7 +116,7 @@ generates: A key-value map, where key is the name of the output Go file
schema:
- ./schema.graphql
- ./types.graphql
- https://github.com/jkrajniak/sc.git/schema1.gql
- https://github.com/VerTox/sc.git/schema1.gql
generates:
internal/models.go:
config:
Expand Down
4 changes: 2 additions & 2 deletions examples/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"github.com/jkrajniak/graphql-codegen-go/examples/config/internal"
"github.com/VerTox/graphql-codegen-go/examples/config/internal"
)

func main() {
Expand All @@ -14,4 +14,4 @@ func main() {
Address: nil,
}
fmt.Println(person)
}
}
2 changes: 1 addition & 1 deletion examples/git-schema/internal/appsync/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package appsync

//go:generate go run ../../../../. -schemas https://github.com/jkrajniak/sc.git/schema1.gql -out models.go -entities Entity1
//go:generate go run ../../../../. -schemas https://github.com/VerTox/sc.git/schema1.gql -out models.go -entities Entity1

type RawEntity struct {
ID string
Expand Down
2 changes: 1 addition & 1 deletion examples/local-schema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"github.com/jkrajniak/graphql-codegen-go/examples/local-schema/internal/appsync"
"github.com/VerTox/graphql-codegen-go/examples/local-schema/internal/appsync"
)

func main() {
Expand Down
8 changes: 4 additions & 4 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type GOGenerate struct {
}

type Config struct {
Schemas []string
Outputs []OutputItem
Schemas []string
Outputs []OutputItem

InPlace bool
GoGenConfig *GOGenerate
Expand All @@ -54,8 +54,8 @@ func ReadConfigFromFile(f io.Reader) (Config, error) {
}

c := Config{
Schemas: yamlConfig.Schema,
Outputs: []OutputItem{},
Schemas: yamlConfig.Schema,
Outputs: []OutputItem{},
}
for outPath, item := range yamlConfig.Generates {
c.Outputs = append(c.Outputs, OutputItem{
Expand Down
27 changes: 18 additions & 9 deletions internal/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ func NewGoGenerator(output Outputer, entities []string, packageName string) *GoG
return &GoGenerator{output: output, entities: entities, packageName: packageName}
}

func (g *GoGenerator) Generate(doc *ast.SchemaDocument) error {
declaredKeywords := keywordMap{}

//remap enums
enumMap := buildEnumMap(doc)
reqEntities := resolveEntityDependencies(doc, g.entities, enumMap)

// Write enum const
func (g *GoGenerator) writeEnums(enumMap map[string]enum, reqEntities []string, declaredKeywords *keywordMap) error {
for _, e := range enumMap {
if len(reqEntities) > 0 && !inArray(e.TypeName, reqEntities) {
continue
Expand All @@ -87,6 +80,22 @@ func (g *GoGenerator) Generate(doc *ast.SchemaDocument) error {
}
}

return nil
}

func (g *GoGenerator) Generate(doc *ast.SchemaDocument) error {
declaredKeywords := keywordMap{}

//remap enums
enumMap := buildEnumMap(doc)
reqEntities := resolveEntityDependencies(doc, g.entities, enumMap)

// Write enum const
err := g.writeEnums(enumMap, reqEntities, &declaredKeywords)
if err != nil {
return err
}

imports := make(map[string]bool, 0)

for _, i := range doc.Definitions {
Expand Down Expand Up @@ -144,7 +153,7 @@ func (g *GoGenerator) Generate(doc *ast.SchemaDocument) error {

importsStr += "\n"

err := g.output.WriteToStart(fmt.Sprintf(Header, g.packageName, importsStr))
err = g.output.WriteToStart(fmt.Sprintf(Header, g.packageName, importsStr))
if err != nil {
return err
}
Expand Down
2 changes: 0 additions & 2 deletions internal/readers/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ func DiscoverReader(schemaPath string) SchemaReader {
return NewLocalReader(schemaPath)
}
}


4 changes: 2 additions & 2 deletions internal/readers/vsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func TestNewGitReader(t *testing.T) {
}

func TestNewGitReader_SSHPath(t *testing.T) {
s := NewGitReader("git@github.com:jkrajniak/sc-priv.git/schema.sql")
s := NewGitReader("git@github.com:VerTox/sc-priv.git/schema.sql")
assert.Equal(t, "master", s.branchName)
assert.Equal(t, "/schema.sql", s.path)
assert.Equal(t, "git@github.com:jkrajniak/sc-priv.git", s.repoURL)
assert.Equal(t, "git@github.com:VerTox/sc-priv.git", s.repoURL)
}

func TestNewGitReader_BranchName(t *testing.T) {
Expand Down
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import (
"strings"
)

func main() {
func initConfig() internal.Config {
var config internal.Config
configYaml := flag.String("config", "", "config yaml")
schemasFile := flag.String("schemas", "", "schema file (comma separated list)")
entitiesString := flag.String("entities", "", "comma separated list of entities (optional)")
packageNameString := flag.String("packageName", "", "package name")
outFile := flag.String("out", "", "file output name (optional, default: stdout)")
flag.Parse()

var config internal.Config
if configYaml != nil && *configYaml != "" {
configFile, err := os.Open(*configYaml)
if err != nil {
Expand Down Expand Up @@ -57,6 +56,12 @@ func main() {
}
}

return config
}

func main() {
config := initConfig()

// Combine all schemas.
inputSchemas, err := internal.ReadSchemas(config.Schemas)
if err != nil {
Expand Down

0 comments on commit 72d3d75

Please sign in to comment.