Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove source reprinting #1019

Merged
merged 2 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 23 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,18 @@ 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 {
return nil, errors.Wrap(err, "unable to open schema")
}

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

return config, nil
}

Expand Down Expand Up @@ -569,23 +581,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