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

use error interface instead of gqlerror as return type #100

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions gqlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
_ "github.com/vektah/gqlparser/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 @@ -20,10 +20,10 @@ func MustLoadSchema(str ...*ast.Source) *ast.Schema {
return s
}

func LoadQuery(schema *ast.Schema, str string) (*ast.QueryDocument, gqlerror.List) {
func LoadQuery(schema *ast.Schema, str string) (*ast.QueryDocument, error) {
query, err := parser.ParseQuery(&ast.Source{Input: str})
if err != nil {
return nil, gqlerror.List{err}
return nil, gqlerror.List{err.(*gqlerror.Error)}
}
errs := validator.Validate(schema, query)
if errs != nil {
Expand Down
15 changes: 10 additions & 5 deletions parser/query.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package parser

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

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

func ParseQuery(source *Source) (*QueryDocument, *gqlerror.Error) {
func ParseQuery(source *Source) (*QueryDocument, error) {
p := parser{
lexer: lexer.New(source),
}
return p.parseQueryDocument(), p.err

doc := p.parseQueryDocument()

if p.err != nil {
return nil, p.err
}

return doc, nil
}

func (p *parser) parseQueryDocument() *QueryDocument {
Expand Down
12 changes: 11 additions & 1 deletion parser/query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package parser

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

"github.com/vektah/gqlparser/ast"
Expand All @@ -10,8 +12,16 @@ 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"})

log.Printf("got error %#v", err)

var e *gqlerror.Error
robojones marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
e = err.(*gqlerror.Error)
}

return testrunner.Spec{
Error: err,
Error: e,
AST: ast.Dump(doc),
}
})
Expand Down
11 changes: 9 additions & 2 deletions validator/imported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ 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)
Expand All @@ -98,8 +98,15 @@ func runSpec(t *testing.T, schemas []*ast.Schema, deviations []*Deviation, filen
schema = schemas[idx]
}
_, err := gqlparser.LoadQuery(schema, spec.Query)
var gqlErrors gqlerror.List
if err != nil {
gqlErrors = err.(gqlerror.List)
}

var finalErrors gqlerror.List
for _, err := range err {


for _, err := range gqlErrors {
// 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 @@ -11,15 +11,15 @@ import (
"github.com/vektah/gqlparser/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
9 changes: 8 additions & 1 deletion 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/gqlerror"
"io/ioutil"
"testing"

Expand Down Expand Up @@ -58,8 +59,14 @@ 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})

var e *gqlerror.Error
if err != nil {
e = err.(*gqlerror.Error)
}

return testrunner.Spec{
Error: err,
Error: e,
}
})
}
8 changes: 6 additions & 2 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func AddRule(name string, f ruleFunc) {
rules = append(rules, rule{name: name, rule: f})
}

func Validate(schema *Schema, doc *QueryDocument) gqlerror.List {
func Validate(schema *Schema, doc *QueryDocument) error {
var errs gqlerror.List

observers := &Events{}
Expand All @@ -40,5 +40,9 @@ func Validate(schema *Schema, doc *QueryDocument) gqlerror.List {
}

Walk(schema, doc, observers)
return errs

if errs != nil {
return errs
}
return nil
}