Skip to content

Commit

Permalink
Merge pull request #204 from vektah/gqlparser-prelude
Browse files Browse the repository at this point in the history
Use shared prelude
  • Loading branch information
vektah authored Jul 19, 2018
2 parents 2692d3e + 548aed1 commit c2f1570
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 162 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Build struct {
MutationRoot *Object
SubscriptionRoot *Object
SchemaRaw string
SchemaFilename string
}

type ModelBuild struct {
Expand Down Expand Up @@ -76,12 +77,13 @@ func (cfg *Config) bind() (*Build, error) {
}

b := &Build{
PackageName: cfg.Exec.Package,
Objects: objects,
Interfaces: cfg.buildInterfaces(namedTypes, prog),
Inputs: inputs,
Imports: imports.finalize(),
SchemaRaw: cfg.SchemaStr,
PackageName: cfg.Exec.Package,
Objects: objects,
Interfaces: cfg.buildInterfaces(namedTypes, prog),
Inputs: inputs,
Imports: imports.finalize(),
SchemaRaw: cfg.SchemaStr,
SchemaFilename: cfg.SchemaFilename,
}

if cfg.schema.Query != nil {
Expand Down
12 changes: 8 additions & 4 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (

"github.com/pkg/errors"
"github.com/vektah/gqlgen/codegen/templates"
"github.com/vektah/gqlgen/graphql/introspection"
"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/gqlerror"
"golang.org/x/tools/imports"
)

Expand Down Expand Up @@ -107,9 +108,12 @@ func (cfg *Config) normalize() error {
}
}

var err error
cfg.schema, err = gqlparser.LoadSchema(introspection.Prelude + cfg.SchemaStr)
return err
var err *gqlerror.Error
cfg.schema, err = gqlparser.LoadSchema(&ast.Source{Name: cfg.SchemaFilename, Input: cfg.SchemaStr})
if err != nil {
return err
}
return nil
}

var invalidPackageNameChar = regexp.MustCompile(`[^\w]`)
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/data.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,6 @@ func (ec *executionContext) introspectType(name string) *introspection.Type {
return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name])
}

var parsedSchema = gqlparser.MustLoadSchema(introspection.Prelude + {{.SchemaRaw|rawQuote}})
var parsedSchema = gqlparser.MustLoadSchema(
&ast.Source{Name: {{.SchemaFilename|quote}}, Input: {{.SchemaRaw|rawQuote}}},
)
3 changes: 3 additions & 0 deletions codegen/type_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func (n NamedTypes) getType(t *ast.Type) *Type {
if !t.NonNull {
modifiers = append(modifiers, modPtr)
}
if n[t.NamedType] == nil {
panic("missing type " + t.NamedType)
}
res := &Type{
NamedType: n[t.NamedType],
Modifiers: modifiers,
Expand Down
6 changes: 4 additions & 2 deletions example/chat/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions example/dataloader/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions example/scalars/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions example/selection/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions example/starwars/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions example/todo/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/todo/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ input TodoInput {
text: String!
done: Boolean
}

scalar Map
31 changes: 0 additions & 31 deletions graphql/introspection/inliner/inliner.go

This file was deleted.

2 changes: 0 additions & 2 deletions graphql/introspection/introspection.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:generate go run ./inliner/inliner.go

// introspection implements the spec defined in https://github.com/facebook/graphql/blob/master/spec/Section%204%20--%20Introspection.md#schema-introspection
package introspection

Expand Down
3 changes: 0 additions & 3 deletions graphql/introspection/prelude.go

This file was deleted.

96 changes: 0 additions & 96 deletions graphql/introspection/prelude.graphql

This file was deleted.

4 changes: 2 additions & 2 deletions handler/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type executableSchemaStub struct {
var _ graphql.ExecutableSchema = &executableSchemaStub{}

func (e *executableSchemaStub) Schema() *ast.Schema {
return gqlparser.MustLoadSchema(`
return gqlparser.MustLoadSchema(&ast.Source{Input: `
schema { query: Query }
type Query { me: User! }
type User { name: String! }
`)
`})
}

func (e *executableSchemaStub) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
Expand Down
6 changes: 4 additions & 2 deletions test/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c2f1570

Please sign in to comment.