Skip to content

Commit

Permalink
Update scalar definition parser to capture description
Browse files Browse the repository at this point in the history
Signed-off-by: James Phillips <jamesdphillips@gmail.com>
  • Loading branch information
jamesdphillips committed Dec 13, 2017
1 parent 523d27c commit bbfa6a2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
20 changes: 11 additions & 9 deletions language/ast/type_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (def *SchemaDefinition) GetOperation() string {
return ""
}

// ScalarDefinition implements Node, Definition
// OperationTypeDefinition implements Node, Definition
type OperationTypeDefinition struct {
Kind string
Loc *Location
Expand Down Expand Up @@ -102,21 +102,23 @@ func (def *OperationTypeDefinition) GetLoc() *Location {

// ScalarDefinition implements Node, Definition
type ScalarDefinition struct {
Kind string
Loc *Location
Name *Name
Directives []*Directive
Kind string
Loc *Location
Description *StringValue
Name *Name
Directives []*Directive
}

func NewScalarDefinition(def *ScalarDefinition) *ScalarDefinition {
if def == nil {
def = &ScalarDefinition{}
}
return &ScalarDefinition{
Kind: kinds.ScalarDefinition,
Loc: def.Loc,
Name: def.Name,
Directives: def.Directives,
Kind: kinds.ScalarDefinition,
Loc: def.Loc,
Description: def.Description,
Name: def.Name,
Directives: def.Directives,
}
}

Expand Down
15 changes: 10 additions & 5 deletions language/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,11 +958,15 @@ func parseOperationTypeDefinition(parser *Parser) (interface{}, error) {
}

/**
* ScalarTypeDefinition : scalar Name Directives?
* ScalarTypeDefinition : Description? scalar Name Directives?
*/
func parseScalarTypeDefinition(parser *Parser) (*ast.ScalarDefinition, error) {
start := parser.Token.Start
_, err := expectKeyWord(parser, "scalar")
description, err := parseDescription(parser)
if err != nil {
return nil, err
}
_, err = expectKeyWord(parser, "scalar")
if err != nil {
return nil, err
}
Expand All @@ -975,9 +979,10 @@ func parseScalarTypeDefinition(parser *Parser) (*ast.ScalarDefinition, error) {
return nil, err
}
def := ast.NewScalarDefinition(&ast.ScalarDefinition{
Name: name,
Directives: directives,
Loc: loc(parser, start),
Name: name,
Description: description,
Directives: directives,
Loc: loc(parser, start),
})
return def, nil
}
Expand Down
13 changes: 13 additions & 0 deletions language/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,19 @@ func TestParsesNamedSubscriptionOperations(t *testing.T) {
}
}

func TestParsesScalarWithDescription(t *testing.T) {
source := `
"""
Returns RFC666; includes timezone offset.
"""
scalar TimeWithZone
`
_, err := Parse(ParseParams{Source: source})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

func TestParseCreatesAst(t *testing.T) {
body := `{
node(id: 4) {
Expand Down

0 comments on commit bbfa6a2

Please sign in to comment.