Skip to content

Commit

Permalink
Remove source reprinting
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 8, 2020
1 parent e289aaa commit aacc9b1
Show file tree
Hide file tree
Showing 30 changed files with 1,163 additions and 982 deletions.
21 changes: 13 additions & 8 deletions api/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,30 @@ func Generate(cfg *config.Config, option ...Option) error {
}

for _, p := range plugins {
if inj, ok := p.(plugin.SourcesInjector); ok {
inj.InjectSources(cfg)
if inj, ok := p.(plugin.EarlySourceInjector); ok {
if s := inj.InjectSourceEarly(); s != nil {
cfg.Sources = append(cfg.Sources, s)
}
}
}

err := cfg.LoadSchema()
if err != nil {
if err := cfg.LoadSchema(); err != nil {
return errors.Wrap(err, "failed to load schema")
}

for _, p := range plugins {
if mut, ok := p.(plugin.SchemaMutator); ok {
err := mut.MutateSchema(cfg.Schema)
if err != nil {
return errors.Wrap(err, p.Name())
if inj, ok := p.(plugin.LateSourceInjector); ok {
if s := inj.InjectSourceLate(cfg.Schema); s != nil {
cfg.Sources = append(cfg.Sources, s)
}
}
}

// LoadSchema again now we have everything
if err := cfg.LoadSchema(); err != nil {
return errors.Wrap(err, "failed to load schema")
}

if err := cfg.Init(); err != nil {
return errors.Wrap(err, "generating core failed")
}
Expand Down
39 changes: 24 additions & 15 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Config struct {
Directives map[string]DirectiveConfig `yaml:"directives,omitempty"`
OmitSliceElementPointers bool `yaml:"omit_slice_element_pointers,omitempty"`
SkipValidation bool `yaml:"skip_validation,omitempty"`
AdditionalSources []*ast.Source `yaml:"-"`
Sources []*ast.Source `yaml:"-"`
Packages *code.Packages `yaml:"-"`
Schema *ast.Schema `yaml:"-"`

Expand Down Expand Up @@ -138,6 +138,19 @@ func LoadConfig(filename string) (*Config, error) {
}
}

for _, filename := range config.SchemaFilename {
filename = filepath.ToSlash(filename)
var err error
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error())
os.Exit(1)
}

config.Sources = append(config.Sources, &ast.Source{Name: filename, Input: string(schemaRaw)})
}

return config, nil
}

Expand Down Expand Up @@ -569,23 +582,19 @@ func (c *Config) LoadSchema() error {
return err
}

sources := append([]*ast.Source{}, c.AdditionalSources...)
for _, filename := range c.SchemaFilename {
filename = filepath.ToSlash(filename)
var err error
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error())
os.Exit(1)
}
sources = append(sources, &ast.Source{Name: filename, Input: string(schemaRaw)})
}

schema, err := gqlparser.LoadSchema(sources...)
schema, err := gqlparser.LoadSchema(c.Sources...)
if err != nil {
return err
}

if schema.Query == nil {
schema.Query = &ast.Definition{
Kind: ast.Object,
Name: "Query",
}
schema.Types["Query"] = schema.Query
}

c.Schema = schema
return nil
}
Expand Down
12 changes: 2 additions & 10 deletions codegen/data.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package codegen

import (
"bytes"
"fmt"
"sort"

"github.com/99designs/gqlgen/codegen/config"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/formatter"

"github.com/99designs/gqlgen/codegen/config"
)

// Data is a unified model of the code to be generated. Plugins may modify this structure to do things like implement
// resolvers or directives automatically (eg grpc, validation)
type Data struct {
Config *config.Config
Schema *ast.Schema
SchemaStr map[string]string
Directives DirectiveList
Objects Objects
Inputs Objects
Expand All @@ -32,7 +30,6 @@ type Data struct {
type builder struct {
Config *config.Config
Schema *ast.Schema
SchemaStr map[string]string
Binder *config.Binder
Directives map[string]*Directive
}
Expand Down Expand Up @@ -62,7 +59,6 @@ func BuildData(cfg *config.Config) (*Data, error) {
Config: cfg,
Directives: dataDirectives,
Schema: b.Schema,
SchemaStr: b.SchemaStr,
Interfaces: map[string]*Interface{},
}

Expand Down Expand Up @@ -127,10 +123,6 @@ func BuildData(cfg *config.Config) (*Data, error) {
return nil, fmt.Errorf("invalid types were encountered while traversing the go source code, this probably means the invalid code generated isnt correct. add try adding -v to debug")
}

var buf bytes.Buffer
formatter.NewFormatter(&buf).FormatSchema(b.Schema)
s.SchemaStr = map[string]string{"schema.graphql": buf.String()}

return &s, nil
}

Expand Down
11 changes: 6 additions & 5 deletions codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ func (ec *executionContext) introspectType(name string) (*introspection.Type, er
return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name]), nil
}

var parsedSchema = gqlparser.MustLoadSchema(
{{- range $filename, $schema := .SchemaStr }}
&ast.Source{Name: {{$filename|quote}}, Input: {{$schema|rawQuote}}},
{{- end }}
)
var sources = []*ast.Source{
{{- range $source := .Config.Sources }}
&ast.Source{Name: {{$source.Name|quote}}, Input: {{$source.Input|rawQuote}}, BuiltIn: {{$source.BuiltIn}}},
{{- end }}
}
var parsedSchema = gqlparser.MustLoadSchema(sources...)
Loading

0 comments on commit aacc9b1

Please sign in to comment.