Skip to content

Commit

Permalink
Merge pull request #197 from vektah/new-parser
Browse files Browse the repository at this point in the history
Integrate gqlparser
  • Loading branch information
vektah authored Jul 18, 2018
2 parents 55179a6 + 5796d47 commit 2692d3e
Show file tree
Hide file tree
Showing 89 changed files with 1,254 additions and 9,024 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
- run: go install -v . && go get -u github.com/vektah/dataloaden github.com/pkg/errors
- run: go generate ./... && if [[ $(git --no-pager diff) ]] ; then echo "you need to run go generate" ; git --no-pager diff ; exit 1 ; fi
- run: go vet ./...
- run: go test -race ./...
- run: go test ./...
- run: gometalinter --vendor ./...

3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/internal/tests/testdata/graphql-js
/vendor
/docs/public
/example/chat/node_modules
/example/chat/package-lock.json
/codegen/testdata/gen
/codegen/tests/gen

.idea/
27 changes: 27 additions & 0 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ environment:
PATH: '%PATH%;c:\gopath\bin'

branches:
only: ["master"]
only: ["master", "next"]

init:
- git config --global core.autocrlf input
Expand Down
4 changes: 2 additions & 2 deletions client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/gorilla/websocket"
"github.com/vektah/gqlgen/neelance/errors"
"github.com/vektah/gqlparser/gqlerror"
)

const (
Expand Down Expand Up @@ -83,7 +83,7 @@ func (p *Client) Websocket(query string, options ...Option) *Subscription {
}

if respDataRaw["errors"] != nil {
var errs []*errors.QueryError
var errs []*gqlerror.Error
if err = unpack(respDataRaw["errors"], errs); err != nil {
return err
}
Expand Down
39 changes: 8 additions & 31 deletions codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,42 +84,19 @@ func (cfg *Config) bind() (*Build, error) {
SchemaRaw: cfg.SchemaStr,
}

if qr, ok := cfg.schema.EntryPoints["query"]; ok {
b.QueryRoot = b.Objects.ByName(qr.TypeName())
}

if mr, ok := cfg.schema.EntryPoints["mutation"]; ok {
b.MutationRoot = b.Objects.ByName(mr.TypeName())
if cfg.schema.Query != nil {
b.QueryRoot = b.Objects.ByName(cfg.schema.Query.Name)
} else {
return b, fmt.Errorf("query entry point missing")
}

if sr, ok := cfg.schema.EntryPoints["subscription"]; ok {
b.SubscriptionRoot = b.Objects.ByName(sr.TypeName())
if cfg.schema.Mutation != nil {
b.MutationRoot = b.Objects.ByName(cfg.schema.Mutation.Name)
}

if b.QueryRoot == nil {
return b, fmt.Errorf("query entry point missing")
if cfg.schema.Subscription != nil {
b.SubscriptionRoot = b.Objects.ByName(cfg.schema.Subscription.Name)
}

// Poke a few magic methods into query
q := b.Objects.ByName(b.QueryRoot.GQLType)
q.Fields = append(q.Fields, Field{
Type: &Type{namedTypes["__Schema"], []string{modPtr}, nil},
GQLName: "__schema",
NoErr: true,
GoMethodName: "ec.introspectSchema",
Object: q,
})
q.Fields = append(q.Fields, Field{
Type: &Type{namedTypes["__Type"], []string{modPtr}, nil},
GQLName: "__type",
NoErr: true,
GoMethodName: "ec.introspectType",
Args: []FieldArgument{
{GQLName: "name", Type: &Type{namedTypes["String"], []string{}, nil}, Object: &Object{}},
},
Object: q,
})

return b, nil
}

Expand Down
20 changes: 11 additions & 9 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

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

Expand Down Expand Up @@ -82,12 +83,12 @@ func (cfg *Config) normalize() error {
}

builtins := TypeMap{
"__Directive": {Model: "github.com/vektah/gqlgen/neelance/introspection.Directive"},
"__Type": {Model: "github.com/vektah/gqlgen/neelance/introspection.Type"},
"__Field": {Model: "github.com/vektah/gqlgen/neelance/introspection.Field"},
"__EnumValue": {Model: "github.com/vektah/gqlgen/neelance/introspection.EnumValue"},
"__InputValue": {Model: "github.com/vektah/gqlgen/neelance/introspection.InputValue"},
"__Schema": {Model: "github.com/vektah/gqlgen/neelance/introspection.Schema"},
"__Directive": {Model: "github.com/vektah/gqlgen/graphql/introspection.Directive"},
"__Type": {Model: "github.com/vektah/gqlgen/graphql/introspection.Type"},
"__Field": {Model: "github.com/vektah/gqlgen/graphql/introspection.Field"},
"__EnumValue": {Model: "github.com/vektah/gqlgen/graphql/introspection.EnumValue"},
"__InputValue": {Model: "github.com/vektah/gqlgen/graphql/introspection.InputValue"},
"__Schema": {Model: "github.com/vektah/gqlgen/graphql/introspection.Schema"},
"Int": {Model: "github.com/vektah/gqlgen/graphql.Int"},
"Float": {Model: "github.com/vektah/gqlgen/graphql.Float"},
"String": {Model: "github.com/vektah/gqlgen/graphql.String"},
Expand All @@ -106,8 +107,9 @@ func (cfg *Config) normalize() error {
}
}

cfg.schema = schema.New()
return cfg.schema.Parse(cfg.SchemaStr)
var err error
cfg.schema, err = gqlparser.LoadSchema(introspection.Prelude + cfg.SchemaStr)
return err
}

var invalidPackageNameChar = regexp.MustCompile(`[^\w]`)
Expand Down
4 changes: 2 additions & 2 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/vektah/gqlgen/neelance/schema"
"github.com/vektah/gqlparser/ast"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ type Config struct {
Model PackageConfig `yaml:"model"`
Models TypeMap `yaml:"models,omitempty"`

schema *schema.Schema `yaml:"-"`
schema *ast.Schema `yaml:"-"`
}

type PackageConfig struct {
Expand Down
8 changes: 4 additions & 4 deletions codegen/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func TestLoadConfig(t *testing.T) {
})

t.Run("malformed config", func(t *testing.T) {
_, err := LoadConfig("testdata/cfg/malformedconfig.yml")
_, err := LoadConfig("tests/cfg/malformedconfig.yml")
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `asdf` into codegen.Config")
})

t.Run("unknown keys", func(t *testing.T) {
_, err := LoadConfig("testdata/cfg/unknownkeys.yml")
_, err := LoadConfig("tests/cfg/unknownkeys.yml")
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 2: field unknown not found in type codegen.Config")
})
}
Expand All @@ -33,7 +33,7 @@ func TestLoadDefaultConfig(t *testing.T) {
var cfg *Config

t.Run("will find closest match", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "subdir"))
err = os.Chdir(filepath.Join(testDir, "tests", "cfg", "subdir"))
require.NoError(t, err)

cfg, err = LoadDefaultConfig()
Expand All @@ -42,7 +42,7 @@ func TestLoadDefaultConfig(t *testing.T) {
})

t.Run("will find config in parent dirs", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "otherdir"))
err = os.Chdir(filepath.Join(testDir, "tests", "cfg", "otherdir"))
require.NoError(t, err)

cfg, err = LoadDefaultConfig()
Expand Down
11 changes: 5 additions & 6 deletions codegen/enum_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import (
"strings"

"github.com/vektah/gqlgen/codegen/templates"
"github.com/vektah/gqlgen/neelance/schema"
"github.com/vektah/gqlparser/ast"
)

func (cfg *Config) buildEnums(types NamedTypes) []Enum {
var enums []Enum

for _, typ := range cfg.schema.Types {
namedType := types[typ.TypeName()]
e, isEnum := typ.(*schema.Enum)
if !isEnum || strings.HasPrefix(typ.TypeName(), "__") || namedType.IsUserDefined {
namedType := types[typ.Name]
if typ.Kind != ast.Enum || strings.HasPrefix(typ.Name, "__") || namedType.IsUserDefined {
continue
}

var values []EnumValue
for _, v := range e.Values {
values = append(values, EnumValue{v.Name, v.Desc})
for _, v := range typ.EnumValues {
values = append(values, EnumValue{v.Name, v.Description})
}

enum := Enum{
Expand Down
10 changes: 5 additions & 5 deletions codegen/import_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ var ambientImports = []string{
"strconv",
"time",
"sync",
"github.com/vektah/gqlgen/neelance/introspection",
"github.com/vektah/gqlgen/neelance/errors",
"github.com/vektah/gqlgen/neelance/query",
"github.com/vektah/gqlgen/neelance/schema",
"github.com/vektah/gqlgen/neelance/validation",
"errors",

"github.com/vektah/gqlparser",
"github.com/vektah/gqlparser/ast",
"github.com/vektah/gqlgen/graphql",
"github.com/vektah/gqlgen/graphql/introspection",
}

func buildImports(types NamedTypes, destDir string) *Imports {
Expand Down
4 changes: 2 additions & 2 deletions codegen/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestInvalidPackagenames(t *testing.T) {
id: Int!
}
`, TypeMap{
"InvalidIdentifier": {Model: "github.com/vektah/gqlgen/codegen/testdata/invalid-packagename.InvalidIdentifier"},
"InvalidIdentifier": {Model: "github.com/vektah/gqlgen/codegen/tests/invalid-packagename.InvalidIdentifier"},
})

require.NoError(t, err)
Expand All @@ -31,7 +31,7 @@ func TestImportCollisions(t *testing.T) {
}
`, TypeMap{
"It": {Model: "github.com/vektah/gqlgen/codegen/testdata/introspection.It"},
"It": {Model: "github.com/vektah/gqlgen/codegen/tests/introspection.It"},
})

require.NoError(t, err)
Expand Down
24 changes: 14 additions & 10 deletions codegen/input_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/vektah/gqlgen/neelance/schema"
"github.com/vektah/gqlparser/ast"
"golang.org/x/tools/go/loader"
)

func (cfg *Config) buildInputs(namedTypes NamedTypes, prog *loader.Program, imports *Imports) (Objects, error) {
var inputs Objects

for _, typ := range cfg.schema.Types {
switch typ := typ.(type) {
case *schema.InputObject:
switch typ.Kind {
case ast.InputObject:
input, err := buildInput(namedTypes, typ)
if err != nil {
return nil, err
Expand Down Expand Up @@ -44,18 +44,22 @@ func (cfg *Config) buildInputs(namedTypes NamedTypes, prog *loader.Program, impo
return inputs, nil
}

func buildInput(types NamedTypes, typ *schema.InputObject) (*Object, error) {
obj := &Object{NamedType: types[typ.TypeName()]}
func buildInput(types NamedTypes, typ *ast.Definition) (*Object, error) {
obj := &Object{NamedType: types[typ.Name]}

for _, field := range typ.Values {
for _, field := range typ.Fields {
newField := Field{
GQLName: field.Name.Name,
GQLName: field.Name,
Type: types.getType(field.Type),
Object: obj,
}

if field.Default != nil {
newField.Default = field.Default.Value(nil)
if field.DefaultValue != nil {
var err error
newField.Default, err = field.DefaultValue.Value(nil)
if err != nil {
return nil, errors.Errorf("default value for %s.%s is not valid: %s", typ.Name, field.Name, err.Error())
}
}

if !newField.Type.IsInput && !newField.Type.IsScalar {
Expand All @@ -70,7 +74,7 @@ func buildInput(types NamedTypes, typ *schema.InputObject) (*Object, error) {

// if user has implemented an UnmarshalGQL method on the input type manually, use it
// otherwise we will generate one.
func buildInputMarshaler(typ *schema.InputObject, def types.Object) *Ref {
func buildInputMarshaler(typ *ast.Definition, def types.Object) *Ref {
switch def := def.(type) {
case *types.TypeName:
namedType := def.Type().(*types.Named)
Expand Down
2 changes: 1 addition & 1 deletion codegen/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestRecursiveInputType(t *testing.T) {
self: [RecursiveInputSlice!]
}
`, TypeMap{
"RecursiveInputSlice": {Model: "github.com/vektah/gqlgen/codegen/testdata.RecursiveInputSlice"},
"RecursiveInputSlice": {Model: "github.com/vektah/gqlgen/codegen/tests.RecursiveInputSlice"},
})

require.NoError(t, err)
Expand Down
Loading

0 comments on commit 2692d3e

Please sign in to comment.