diff --git a/language/ast/definitions.go b/language/ast/definitions.go index 52539e7..7796df6 100644 --- a/language/ast/definitions.go +++ b/language/ast/definitions.go @@ -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 { diff --git a/language/ast/type_definitions.go b/language/ast/type_definitions.go index 9581007..f06970f 100644 --- a/language/ast/type_definitions.go +++ b/language/ast/type_definitions.go @@ -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 { @@ -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 diff --git a/language/kinds/kinds.go b/language/kinds/kinds.go index 36e6d3d..c4ca662 100644 --- a/language/kinds/kinds.go +++ b/language/kinds/kinds.go @@ -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 diff --git a/language/parser/parser.go b/language/parser/parser.go index 8a7bb07..db482ec 100644 --- a/language/parser/parser.go +++ b/language/parser/parser.go @@ -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 { @@ -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 { @@ -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+ } */ @@ -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+ } */ @@ -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 { diff --git a/language/printer/printer.go b/language/printer/printer.go index ae7ca78..9bfa451 100644 --- a/language/printer/printer.go +++ b/language/printer/printer.go @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: