fixes while integrating into webapp
This commit is contained in:
+10
-9
@@ -27,6 +27,7 @@ type Config struct {
|
||||
// how to convert that to SDL).
|
||||
Schema string `yaml:"schema"`
|
||||
// The filename with the queries; defaults to queries.graphql
|
||||
// TODO: allow multiple files?
|
||||
Queries string `yaml:"queries"`
|
||||
// The filename to which to write the generated code; defaults to
|
||||
// generated.go
|
||||
@@ -45,14 +46,20 @@ type Config struct {
|
||||
ClientGetter string `yaml:"client_getter"`
|
||||
}
|
||||
|
||||
func (c *Config) ValidateAndFillDefaults() error {
|
||||
func (c *Config) ValidateAndFillDefaults(configFilename string) error {
|
||||
// Make paths relative to config dir
|
||||
configDir := filepath.Dir(configFilename)
|
||||
c.Schema = filepath.Join(configDir, c.Schema)
|
||||
c.Queries = filepath.Join(configDir, c.Queries)
|
||||
c.Generated = filepath.Join(configDir, c.Generated)
|
||||
|
||||
if c.Package == "" {
|
||||
abs, err := filepath.Abs(c.Generated)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to guess package-name: %v", err)
|
||||
}
|
||||
|
||||
base := filepath.Base(abs)
|
||||
base := filepath.Base(filepath.Dir(abs))
|
||||
if !token.IsIdentifier(base) {
|
||||
return fmt.Errorf("unable to guess package-name: %v is not a valid identifier", base)
|
||||
}
|
||||
@@ -86,16 +93,10 @@ func ReadAndValidateConfig(filename string) (*Config, error) {
|
||||
}
|
||||
}
|
||||
|
||||
err := config.ValidateAndFillDefaults()
|
||||
err := config.ValidateAndFillDefaults(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid config file %v: %v", filename, err)
|
||||
}
|
||||
|
||||
// Make paths relative to config dir
|
||||
basename := filepath.Dir(filename)
|
||||
config.Schema = filepath.Join(basename, config.Schema)
|
||||
config.Queries = filepath.Join(basename, config.Queries)
|
||||
config.Generated = filepath.Join(basename, config.Generated)
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/vektah/gqlparser/ast"
|
||||
"github.com/vektah/gqlparser/formatter"
|
||||
"github.com/vektah/gqlparser/v2/ast"
|
||||
"github.com/vektah/gqlparser/v2/formatter"
|
||||
)
|
||||
|
||||
var fileTemplate = mustTemplate("operation.go.tmpl")
|
||||
@@ -154,6 +154,13 @@ func Generate(config *Config) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: we could also allow this, and generate an empty file with just the
|
||||
// package-name, if it turns out to be more convenient that way. (As-is,
|
||||
// we generate a broken file, with just (unused) imports.)
|
||||
if len(document.Operations) == 0 {
|
||||
return nil, fmt.Errorf("no queries found in %v", config.Queries)
|
||||
}
|
||||
|
||||
g := newGenerator(config, schema)
|
||||
for _, op := range document.Operations {
|
||||
if err = g.addOperation(op); err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func readConfigGenerateAndWrite(configFilename string) error {
|
||||
@@ -17,6 +18,13 @@ func readConfigGenerateAndWrite(configFilename string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(config.Generated), 0o755)
|
||||
if err != nil {
|
||||
return fmt.Errorf(
|
||||
"could not create parent directory for generated file %v: %v",
|
||||
config.Generated, err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(config.Generated, code, 0o644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write generated file %v: %v",
|
||||
|
||||
+4
-4
@@ -4,10 +4,10 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/vektah/gqlparser"
|
||||
"github.com/vektah/gqlparser/ast"
|
||||
"github.com/vektah/gqlparser/parser"
|
||||
"github.com/vektah/gqlparser/validator"
|
||||
"github.com/vektah/gqlparser/v2"
|
||||
"github.com/vektah/gqlparser/v2/ast"
|
||||
"github.com/vektah/gqlparser/v2/parser"
|
||||
"github.com/vektah/gqlparser/v2/validator"
|
||||
)
|
||||
|
||||
func getSchema(filename string) (*ast.Schema, error) {
|
||||
|
||||
+3
-2
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/vektah/gqlparser/ast"
|
||||
"github.com/vektah/gqlparser/v2/ast"
|
||||
)
|
||||
|
||||
type typeBuilder struct {
|
||||
@@ -120,7 +120,7 @@ type outputField struct{ field *ast.Field }
|
||||
|
||||
func (s outputField) Alias() string {
|
||||
// gqlparser sets Alias even if the field is not aliased, see e.g.
|
||||
// https://github.com/vektah/gqlparser/blob/c06d8e0d135f285e37e7f1ff397f10e049733eb3/parser/query.go#L150
|
||||
// https://github.com/vektah/gqlparser/v2/blob/c06d8e0d135f285e37e7f1ff397f10e049733eb3/parser/query.go#L150
|
||||
return s.field.Alias
|
||||
}
|
||||
|
||||
@@ -201,6 +201,7 @@ func (builder *typeBuilder) writeField(field field) error {
|
||||
// `query q { a: f { b }, c: f { d } }` we need separate types for a
|
||||
// and c, even though they are the same type in GraphQL, because they
|
||||
// have different fields.
|
||||
// TODO: if this is an input type, we should skip the prefixing!
|
||||
builder.typeNamePrefix+upperFirst(field.Alias()), "", typ, fields)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user