Skip to content

Commit

Permalink
fix : returning error instead of *gqlError.Error (#234)
Browse files Browse the repository at this point in the history
Co-authored-by: Suraj Chafle <[email protected]>
  • Loading branch information
schafle and Suraj Chafle authored Aug 26, 2022
1 parent 6d97050 commit fe91596
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 59 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ require (
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
github.com/sergi/go-diff v1.1.0 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/tools v0.1.9
gopkg.in/yaml.v2 v2.2.8
)
28 changes: 0 additions & 28 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,6 @@ github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8=
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
5 changes: 3 additions & 2 deletions gqlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
_ "github.com/vektah/gqlparser/v2/validator/rules"
)

func LoadSchema(str ...*ast.Source) (*ast.Schema, *gqlerror.Error) {
func LoadSchema(str ...*ast.Source) (*ast.Schema, error) {
return validator.LoadSchema(append([]*ast.Source{validator.Prelude}, str...)...)
}

Expand All @@ -23,7 +23,8 @@ func MustLoadSchema(str ...*ast.Source) *ast.Schema {
func LoadQuery(schema *ast.Schema, str string) (*ast.QueryDocument, gqlerror.List) {
query, err := parser.ParseQuery(&ast.Source{Input: str})
if err != nil {
return nil, gqlerror.List{err}
gqlErr := err.(*gqlerror.Error)
return nil, gqlerror.List{gqlErr}
}
errs := validator.Validate(schema, query)
if errs != nil {
Expand Down
18 changes: 9 additions & 9 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func (s *Lexer) peek() (rune, int) {
return utf8.DecodeRuneInString(s.Input[s.end:])
}

func (s *Lexer) makeToken(kind Type) (Token, *gqlerror.Error) {
func (s *Lexer) makeToken(kind Type) (Token, error) {
return s.makeValueToken(kind, s.Input[s.start:s.end])
}

func (s *Lexer) makeValueToken(kind Type, value string) (Token, *gqlerror.Error) {
func (s *Lexer) makeValueToken(kind Type, value string) (Token, error) {
return Token{
Kind: kind,
Value: value,
Expand All @@ -55,7 +55,7 @@ func (s *Lexer) makeValueToken(kind Type, value string) (Token, *gqlerror.Error)
}, nil
}

func (s *Lexer) makeError(format string, args ...interface{}) (Token, *gqlerror.Error) {
func (s *Lexer) makeError(format string, args ...interface{}) (Token, error) {
column := s.endRunes - s.lineStartRunes + 1
return Token{
Kind: Invalid,
Expand All @@ -74,7 +74,7 @@ func (s *Lexer) makeError(format string, args ...interface{}) (Token, *gqlerror.
// This skips over whitespace and comments until it finds the next lexable
// token, then lexes punctuators immediately or calls the appropriate helper
// function for more complicated tokens.
func (s *Lexer) ReadToken() (token Token, err *gqlerror.Error) {
func (s *Lexer) ReadToken() (token Token, err error) {

s.ws()
s.start = s.end
Expand Down Expand Up @@ -192,7 +192,7 @@ func (s *Lexer) ws() {
// readComment from the input
//
// #[\u0009\u0020-\uFFFF]*
func (s *Lexer) readComment() (Token, *gqlerror.Error) {
func (s *Lexer) readComment() (Token, error) {
for s.end < len(s.Input) {
r, w := s.peek()

Expand All @@ -213,7 +213,7 @@ func (s *Lexer) readComment() (Token, *gqlerror.Error) {
//
// Int: -?(0|[1-9][0-9]*)
// Float: -?(0|[1-9][0-9]*)(\.[0-9]+)?((E|e)(+|-)?[0-9]+)?
func (s *Lexer) readNumber() (Token, *gqlerror.Error) {
func (s *Lexer) readNumber() (Token, error) {
float := false

// backup to the first digit
Expand Down Expand Up @@ -299,7 +299,7 @@ func (s *Lexer) describeNext() string {
// readString from the input
//
// "([^"\\\u000A\u000D]|(\\(u[0-9a-fA-F]{4}|["\\/bfnrt])))*"
func (s *Lexer) readString() (Token, *gqlerror.Error) {
func (s *Lexer) readString() (Token, error) {
inputLen := len(s.Input)

// this buffer is lazily created only if there are escape characters.
Expand Down Expand Up @@ -409,7 +409,7 @@ func (s *Lexer) readString() (Token, *gqlerror.Error) {
// readBlockString from the input
//
// """("?"?(\\"""|\\(?!=""")|[^"\\]))*"""
func (s *Lexer) readBlockString() (Token, *gqlerror.Error) {
func (s *Lexer) readBlockString() (Token, error) {
inputLen := len(s.Input)

var buf bytes.Buffer
Expand Down Expand Up @@ -499,7 +499,7 @@ func unhex(b string) (v rune, ok bool) {
// readName from the input
//
// [_A-Za-z][_0-9A-Za-z]*
func (s *Lexer) readName() (Token, *gqlerror.Error) {
func (s *Lexer) readName() (Token, error) {
for s.end < len(s.Input) {
r, w := s.peek()

Expand Down
3 changes: 2 additions & 1 deletion lexer/lexer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lexer

import (
"github.com/vektah/gqlparser/v2/gqlerror"
"testing"

"github.com/vektah/gqlparser/v2/ast"
Expand All @@ -16,7 +17,7 @@ func TestLexer(t *testing.T) {
tok, err := l.ReadToken()

if err != nil {
ret.Error = err
ret.Error = err.(*gqlerror.Error)
break
}

Expand Down
4 changes: 2 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

type parser struct {
lexer lexer.Lexer
err *gqlerror.Error
err error

peeked bool
peekToken lexer.Token
peekError *gqlerror.Error
peekError error

prev lexer.Token
}
Expand Down
3 changes: 1 addition & 2 deletions parser/query.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package parser

import (
"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/vektah/gqlparser/v2/lexer"

. "github.com/vektah/gqlparser/v2/ast"
)

func ParseQuery(source *Source) (*QueryDocument, *gqlerror.Error) {
func ParseQuery(source *Source) (*QueryDocument, error) {
p := parser{
lexer: lexer.New(source),
}
Expand Down
9 changes: 8 additions & 1 deletion parser/query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"github.com/vektah/gqlparser/v2/gqlerror"
"testing"

"github.com/vektah/gqlparser/v2/ast"
Expand All @@ -10,8 +11,14 @@ import (
func TestQueryDocument(t *testing.T) {
testrunner.Test(t, "query_test.yml", func(t *testing.T, input string) testrunner.Spec {
doc, err := ParseQuery(&ast.Source{Input: input, Name: "spec"})
if err != nil {
gqlErr := err.(*gqlerror.Error)
return testrunner.Spec{
Error: gqlErr,
AST: ast.Dump(doc),
}
}
return testrunner.Spec{
Error: err,
AST: ast.Dump(doc),
}
})
Expand Down
5 changes: 2 additions & 3 deletions parser/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package parser

import (
. "github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/vektah/gqlparser/v2/lexer"
)

func ParseSchema(source *Source) (*SchemaDocument, *gqlerror.Error) {
func ParseSchema(source *Source) (*SchemaDocument, error) {
p := parser{
lexer: lexer.New(source),
}
Expand All @@ -25,7 +24,7 @@ func ParseSchema(source *Source) (*SchemaDocument, *gqlerror.Error) {
return ast, nil
}

func ParseSchemas(inputs ...*Source) (*SchemaDocument, *gqlerror.Error) {
func ParseSchemas(inputs ...*Source) (*SchemaDocument, error) {
ast := &SchemaDocument{}
for _, input := range inputs {
inputAst, err := ParseSchema(input)
Expand Down
8 changes: 7 additions & 1 deletion parser/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"github.com/vektah/gqlparser/v2/gqlerror"
"testing"

"github.com/vektah/gqlparser/v2/ast"
Expand All @@ -10,8 +11,13 @@ import (
func TestSchemaDocument(t *testing.T) {
testrunner.Test(t, "schema_test.yml", func(t *testing.T, input string) testrunner.Spec {
doc, err := ParseSchema(&ast.Source{Input: input, Name: "spec"})
if err != nil {
return testrunner.Spec{
Error: err.(*gqlerror.Error),
AST: ast.Dump(doc),
}
}
return testrunner.Spec{
Error: err,
AST: ast.Dump(doc),
}
})
Expand Down
3 changes: 2 additions & 1 deletion parser/testrunner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func Test(t *testing.T, filename string, f func(t *testing.T, input string) Spec

if spec.Error == nil {
if result.Error != nil {
t.Errorf("unexpected error %s", result.Error.Message)
gqlErr := err.(*gqlerror.Error)
t.Errorf("unexpected error %s", gqlErr.Message)
}
} else if result.Error == nil {
t.Errorf("expected error but got none")
Expand Down
6 changes: 3 additions & 3 deletions validator/imported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ func runSpec(t *testing.T, schemas []*ast.Schema, deviations []*Deviation, filen
// idx := spec.Schema
var schema *ast.Schema
if idx, err := strconv.Atoi(spec.Schema); err != nil {
var gqlErr *gqlerror.Error
var gqlErr error
schema, gqlErr = gqlparser.LoadSchema(&ast.Source{Input: spec.Schema, Name: spec.Name})
if gqlErr != nil {
t.Fatal(err)
}
} else {
schema = schemas[idx]
}
_, err := gqlparser.LoadQuery(schema, spec.Query)
_, errList := gqlparser.LoadQuery(schema, spec.Query)
var finalErrors gqlerror.List
for _, err := range err {
for _, err := range errList {
// ignore errors from other rules
if spec.Rule != "" && err.Rule != spec.Rule {
continue
Expand Down
4 changes: 2 additions & 2 deletions validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"github.com/vektah/gqlparser/v2/parser"
)

func LoadSchema(inputs ...*Source) (*Schema, *gqlerror.Error) {
func LoadSchema(inputs ...*Source) (*Schema, error) {
ast, err := parser.ParseSchemas(inputs...)
if err != nil {
return nil, err
}
return ValidateSchemaDocument(ast)
}

func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, *gqlerror.Error) {
func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, error) {
schema := Schema{
Types: map[string]*Definition{},
Directives: map[string]*DirectiveDefinition{},
Expand Down
8 changes: 6 additions & 2 deletions validator/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"github.com/vektah/gqlparser/v2/gqlerror"
"os"
"testing"

Expand Down Expand Up @@ -97,9 +98,12 @@ func TestLoadSchema(t *testing.T) {

testrunner.Test(t, "./schema_test.yml", func(t *testing.T, input string) testrunner.Spec {
_, err := LoadSchema(Prelude, &ast.Source{Input: input})
return testrunner.Spec{
Error: err,
if err != nil {
return testrunner.Spec{
Error: err.(*gqlerror.Error),
}
}
return testrunner.Spec{}
})
}

Expand Down
2 changes: 1 addition & 1 deletion validator/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var UnexpectedType = fmt.Errorf("Unexpected Type")

// VariableValues coerces and validates variable values
func VariableValues(schema *ast.Schema, op *ast.OperationDefinition, variables map[string]interface{}) (map[string]interface{}, *gqlerror.Error) {
func VariableValues(schema *ast.Schema, op *ast.OperationDefinition, variables map[string]interface{}) (map[string]interface{}, error) {
coercedVars := map[string]interface{}{}

validator := varValidator{
Expand Down

0 comments on commit fe91596

Please sign in to comment.