Skip to content

Commit

Permalink
Updating schema parser to more closely match current state of RFC
Browse files Browse the repository at this point in the history
graphql/graphql-spec#90

Commit:
b0885a038ec0e654962d69fb910ac86659279579 [b0885a0]
Parents:
fdafe32724
Author:
Lee Byron <[email protected]>
Date:
23 March 2016 at 6:35:37 AM SGT
Commit Date:
23 March 2016 at 6:35:39 AM SGT
  • Loading branch information
Brian Thompson committed May 30, 2016
1 parent 6a21281 commit 889ff24
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 93 deletions.
4 changes: 1 addition & 3 deletions language/ast/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ type Definition interface {
// Ensure that all definition types implements Definition interface
var _ Definition = (*OperationDefinition)(nil)
var _ Definition = (*FragmentDefinition)(nil)
var _ Definition = (*TypeExtensionDefinition)(nil)
var _ Definition = (*DirectiveDefinition)(nil)
var _ Definition = (Definition)(nil)
var _ Definition = (TypeSystemDefinition)(nil)

// OperationDefinition implements Node, Definition
type OperationDefinition struct {
Expand Down
117 changes: 68 additions & 49 deletions language/ast/type_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,74 @@ import (
"github.com/graphql-go/graphql/language/kinds"
)

// Ensure that all typeDefinition types implements Definition interface
var _ Definition = (*ObjectDefinition)(nil)
var _ Definition = (*InterfaceDefinition)(nil)
var _ Definition = (*UnionDefinition)(nil)
var _ Definition = (*ScalarDefinition)(nil)
var _ Definition = (*EnumDefinition)(nil)
var _ Definition = (*InputObjectDefinition)(nil)
type TypeDefinition interface {
GetOperation() string
GetVariableDefinitions() []*VariableDefinition
GetSelectionSet() *SelectionSet
GetKind() string
GetLoc() *Location
}

var _ TypeDefinition = (*ScalarDefinition)(nil)
var _ TypeDefinition = (*ObjectDefinition)(nil)
var _ TypeDefinition = (*InterfaceDefinition)(nil)
var _ TypeDefinition = (*UnionDefinition)(nil)
var _ TypeDefinition = (*EnumDefinition)(nil)
var _ TypeDefinition = (*InputObjectDefinition)(nil)

type TypeSystemDefinition interface {
GetOperation() string
GetVariableDefinitions() []*VariableDefinition
GetSelectionSet() *SelectionSet
GetKind() string
GetLoc() *Location
}

var _ TypeSystemDefinition = (TypeDefinition)(nil)
var _ TypeSystemDefinition = (*TypeExtensionDefinition)(nil)
var _ TypeSystemDefinition = (*DirectiveDefinition)(nil)

// ScalarDefinition implements Node, Definition
type ScalarDefinition struct {
Kind string
Loc *Location
Name *Name
}

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

func (def *ScalarDefinition) GetKind() string {
return def.Kind
}

func (def *ScalarDefinition) GetLoc() *Location {
return def.Loc
}

func (def *ScalarDefinition) GetName() *Name {
return def.Name
}

func (def *ScalarDefinition) GetVariableDefinitions() []*VariableDefinition {
return []*VariableDefinition{}
}

func (def *ScalarDefinition) GetSelectionSet() *SelectionSet {
return &SelectionSet{}
}

func (def *ScalarDefinition) GetOperation() string {
return ""
}

// ObjectDefinition implements Node, Definition
type ObjectDefinition struct {
Expand Down Expand Up @@ -206,48 +267,6 @@ func (def *UnionDefinition) GetOperation() string {
return ""
}

// ScalarDefinition implements Node, Definition
type ScalarDefinition struct {
Kind string
Loc *Location
Name *Name
}

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

func (def *ScalarDefinition) GetKind() string {
return def.Kind
}

func (def *ScalarDefinition) GetLoc() *Location {
return def.Loc
}

func (def *ScalarDefinition) GetName() *Name {
return def.Name
}

func (def *ScalarDefinition) GetVariableDefinitions() []*VariableDefinition {
return []*VariableDefinition{}
}

func (def *ScalarDefinition) GetSelectionSet() *SelectionSet {
return &SelectionSet{}
}

func (def *ScalarDefinition) GetOperation() string {
return ""
}

// EnumDefinition implements Node, Definition
type EnumDefinition struct {
Kind string
Expand Down
4 changes: 3 additions & 1 deletion language/kinds/kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ const (
List = "List" // previously ListType
NonNull = "NonNull" // previously NonNull

// Type System Definitions

// Types Definitions
ScalarDefinition = "ScalarDefinition" // previously ScalarTypeDefinition
ObjectDefinition = "ObjectDefinition" // previously ObjectTypeDefinition
FieldDefinition = "FieldDefinition"
InputValueDefinition = "InputValueDefinition"
InterfaceDefinition = "InterfaceDefinition" // previously InterfaceTypeDefinition
UnionDefinition = "UnionDefinition" // previously UnionTypeDefinition
ScalarDefinition = "ScalarDefinition" // previously ScalarTypeDefinition
EnumDefinition = "EnumDefinition" // previously EnumTypeDefinition
EnumValueDefinition = "EnumValueDefinition"
InputObjectDefinition = "InputObjectDefinition" // previously InputObjectTypeDefinition
Expand Down
53 changes: 26 additions & 27 deletions language/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ func parseDocument(parser *Parser) (*ast.Document, error) {
return nil, err
}
nodes = append(nodes, node)
case "scalar":
node, err := parseScalarTypeDefinition(parser)
if err != nil {
return nil, err
}
nodes = append(nodes, node)
case "type":
node, err := parseObjectTypeDefinition(parser)
if err != nil {
Expand All @@ -151,12 +157,6 @@ func parseDocument(parser *Parser) (*ast.Document, error) {
return nil, err
}
nodes = append(nodes, node)
case "scalar":
node, err := parseScalarTypeDefinition(parser)
if err != nil {
return nil, err
}
nodes = append(nodes, node)
case "enum":
node, err := parseEnumTypeDefinition(parser)
if err != nil {
Expand Down Expand Up @@ -860,6 +860,26 @@ func parseNamed(parser *Parser) (*ast.Named, error) {

/* Implements the parsing rules in the Type Definition section. */

/**
* ScalarTypeDefinition : scalar Name
*/
func parseScalarTypeDefinition(parser *Parser) (*ast.ScalarDefinition, error) {
start := parser.Token.Start
_, err := expectKeyWord(parser, "scalar")
if err != nil {
return nil, err
}
name, err := parseName(parser)
if err != nil {
return nil, err
}
def := ast.NewScalarDefinition(&ast.ScalarDefinition{
Name: name,
Loc: loc(parser, start),
})
return def, nil
}

/**
* ObjectTypeDefinition : type Name ImplementsInterfaces? { FieldDefinition+ }
*/
Expand Down Expand Up @@ -1085,26 +1105,6 @@ func parseUnionMembers(parser *Parser) ([]*ast.Named, error) {
return members, nil
}

/**
* ScalarTypeDefinition : scalar Name
*/
func parseScalarTypeDefinition(parser *Parser) (*ast.ScalarDefinition, error) {
start := parser.Token.Start
_, err := expectKeyWord(parser, "scalar")
if err != nil {
return nil, err
}
name, err := parseName(parser)
if err != nil {
return nil, err
}
def := ast.NewScalarDefinition(&ast.ScalarDefinition{
Name: name,
Loc: loc(parser, start),
})
return def, nil
}

/**
* EnumTypeDefinition : enum Name { EnumValueDefinition+ }
*/
Expand Down Expand Up @@ -1246,7 +1246,6 @@ func parseDirectiveDefinition(parser *Parser) (*ast.DirectiveDefinition, error)
* - Name
* - DirectiveLocations | Name
*/

func parseDirectiveLocations(parser *Parser) ([]*ast.Name, error) {
locations := []*ast.Name{}
for {
Expand Down
34 changes: 21 additions & 13 deletions language/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
}
return visitor.ActionNoChange, nil
},

// Document
"Document": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.Document:
Expand Down Expand Up @@ -258,6 +260,8 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
}
return visitor.ActionNoChange, nil
},

// Fragments
"FragmentSpread": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.InlineFragment:
Expand Down Expand Up @@ -306,6 +310,7 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
return visitor.ActionNoChange, nil
},

// Value
"IntValue": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.IntValue:
Expand Down Expand Up @@ -383,6 +388,7 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
return visitor.ActionNoChange, nil
},

// Directive
"Directive": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.Directive:
Expand All @@ -397,6 +403,7 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
return visitor.ActionNoChange, nil
},

// Type
"Named": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.Named:
Expand Down Expand Up @@ -425,6 +432,20 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
return visitor.ActionNoChange, nil
},

// Type System Definitions
"ScalarDefinition": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.ScalarDefinition:
name := fmt.Sprintf("%v", node.Name)
str := "scalar " + name
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
str := "scalar " + name
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
},
"ObjectDefinition": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.ObjectDefinition:
Expand Down Expand Up @@ -506,19 +527,6 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
}
return visitor.ActionNoChange, nil
},
"ScalarDefinition": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.ScalarDefinition:
name := fmt.Sprintf("%v", node.Name)
str := "scalar " + name
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
str := "scalar " + name
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
},
"EnumDefinition": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.EnumDefinition:
Expand Down

0 comments on commit 889ff24

Please sign in to comment.