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

Fail to parse empty field lists for Object, Interface, and Input Object types #127

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ func TestIncludeDirective(t *testing.T) {

type testDeprecatedDirectiveResolver struct{}

func (r *testDeprecatedDirectiveResolver) Test() string {
return ""
}

func (r *testDeprecatedDirectiveResolver) A() int32 {
return 0
}
Expand Down Expand Up @@ -643,6 +647,7 @@ func TestDeprecatedDirective(t *testing.T) {
}

type Query {
test: String!
}

enum Test {
Expand Down
38 changes: 38 additions & 0 deletions internal/common/arguments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package common

type Argument struct {
Name Ident
Value Literal
}

type ArgumentList []Argument

func (l ArgumentList) Get(name string) (Literal, bool) {
for _, arg := range l {
if arg.Name.Name == name {
return arg.Value, true
}
}
return nil, false
}

func (l ArgumentList) MustGet(name string) Literal {
value, ok := l.Get(name)
if !ok {
panic("argument not found")
}
return value
}

func ParseArguments(l *Lexer) ArgumentList {
var args ArgumentList
l.ConsumeToken('(')
for l.Peek() != ')' {
name := l.ConsumeIdentWithLoc()
l.ConsumeToken(':')
value := ParseLiteral(l, false)
args = append(args, Argument{Name: name, Value: value})
}
l.ConsumeToken(')')
return args
}
48 changes: 19 additions & 29 deletions internal/common/values.go → internal/common/input_values.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common

import (
"fmt"

"github.com/neelance/graphql-go/errors"
)

Expand Down Expand Up @@ -39,39 +41,27 @@ func ParseInputValue(l *Lexer) *InputValue {
return p
}

type Argument struct {
Name Ident
Value Literal
}

type ArgumentList []Argument

func (l ArgumentList) Get(name string) (Literal, bool) {
for _, arg := range l {
if arg.Name.Name == name {
return arg.Value, true
func ParseArgumentDeclList(l *Lexer) InputValueList {
var args InputValueList
if l.Peek() == '(' {
l.ConsumeToken('(')
for l.Peek() != ')' {
args = append(args, ParseInputValue(l))
}
l.ConsumeToken(')')
}
return nil, false
return args
}

func (l ArgumentList) MustGet(name string) Literal {
value, ok := l.Get(name)
if !ok {
panic("argument not found")
func ParseInputFieldList(typeName string, l *Lexer) InputValueList {
l.ConsumeToken('{')
var list InputValueList
for l.Peek() != '}' {
list = append(list, ParseInputValue(l))
}
return value
}

func ParseArguments(l *Lexer) ArgumentList {
var args ArgumentList
l.ConsumeToken('(')
for l.Peek() != ')' {
name := l.ConsumeIdentWithLoc()
l.ConsumeToken(':')
value := ParseLiteral(l, false)
args = append(args, Argument{Name: name, Value: value})
if len(list) == 0 {
l.SyntaxError(fmt.Sprintf(`input type %q must define one or more fields`, typeName))
}
l.ConsumeToken(')')
return args
l.ConsumeToken('}')
return list
}
38 changes: 11 additions & 27 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,14 @@ func parseObjectDecl(l *common.Lexer) *Object {
}
}
}
l.ConsumeToken('{')
o.Fields = parseFields(l)
l.ConsumeToken('}')
o.Fields = parseFields("object", o.Name, l)
return o
}

func parseInterfaceDecl(l *common.Lexer) *Interface {
i := &Interface{}
i.Name = l.ConsumeIdent()
l.ConsumeToken('{')
i.Fields = parseFields(l)
l.ConsumeToken('}')
i.Fields = parseFields("interface", i.Name, l)
return i
}

Expand All @@ -393,11 +389,7 @@ func parseUnionDecl(l *common.Lexer) *Union {
func parseInputDecl(l *common.Lexer) *InputObject {
i := &InputObject{}
i.Name = l.ConsumeIdent()
l.ConsumeToken('{')
for l.Peek() != '}' {
i.Values = append(i.Values, common.ParseInputValue(l))
}
l.ConsumeToken('}')
i.Values = common.ParseInputFieldList(i.Name, l)
return i
}

Expand All @@ -420,14 +412,7 @@ func parseDirectiveDecl(l *common.Lexer) *DirectiveDecl {
d := &DirectiveDecl{}
l.ConsumeToken('@')
d.Name = l.ConsumeIdent()
if l.Peek() == '(' {
l.ConsumeToken('(')
for l.Peek() != ')' {
v := common.ParseInputValue(l)
d.Args = append(d.Args, v)
}
l.ConsumeToken(')')
}
d.Args = common.ParseArgumentDeclList(l)
l.ConsumeKeyword("on")
for {
loc := l.ConsumeIdent()
Expand All @@ -440,23 +425,22 @@ func parseDirectiveDecl(l *common.Lexer) *DirectiveDecl {
return d
}

func parseFields(l *common.Lexer) FieldList {
func parseFields(declType, typeName string, l *common.Lexer) FieldList {
var fields FieldList
l.ConsumeToken('{')
for l.Peek() != '}' {
f := &Field{}
f.Desc = l.DescComment()
f.Name = l.ConsumeIdent()
if l.Peek() == '(' {
l.ConsumeToken('(')
for l.Peek() != ')' {
f.Args = append(f.Args, common.ParseInputValue(l))
}
l.ConsumeToken(')')
}
f.Args = common.ParseArgumentDeclList(l)
l.ConsumeToken(':')
f.Type = common.ParseType(l)
f.Directives = common.ParseDirectives(l)
fields = append(fields, f)
}
if len(fields) == 0 {
l.SyntaxError(fmt.Sprintf(`%s type %q must define one or more fields`, declType, typeName))
}
l.ConsumeToken('}')
return fields
}