Skip to content

SDL - Separate multiple inherited interfaces with & #324

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

Merged
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 language/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
FLOAT
STRING
BLOCK_STRING
AMP
)

// NAME -> keyword relationship
Expand Down Expand Up @@ -97,6 +98,7 @@ func init() {
tokenDescription[TokenKind[FLOAT]] = "Float"
tokenDescription[TokenKind[STRING]] = "String"
tokenDescription[TokenKind[BLOCK_STRING]] = "BlockString"
tokenDescription[TokenKind[AMP]] = "&"
}
}

Expand Down Expand Up @@ -526,6 +528,9 @@ func readToken(s *source.Source, fromPosition int) (Token, error) {
// $
case '$':
return makeToken(TokenKind[DOLLAR], position, position+1, ""), nil
// &
case '&':
return makeToken(TokenKind[AMP], position, position+1, ""), nil
// (
case '(':
return makeToken(TokenKind[PAREN_L], position, position+1, ""), nil
Expand Down
10 changes: 8 additions & 2 deletions language/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,22 +1013,28 @@ func parseObjectTypeDefinition(parser *Parser) (ast.Node, error) {
}

/**
* ImplementsInterfaces : implements NamedType+
* ImplementsInterfaces :
* - implements `&`? NamedType
* - ImplementsInterfaces & NamedType
*/
func parseImplementsInterfaces(parser *Parser) ([]*ast.Named, error) {
types := []*ast.Named{}
if parser.Token.Value == "implements" {
if err := advance(parser); err != nil {
return nil, err
}
// optional leading ampersand
skip(parser, lexer.TokenKind[lexer.AMP])
for {
ttype, err := parseNamed(parser)
if err != nil {
return types, err
}
types = append(types, ttype)
if !peek(parser, lexer.TokenKind[lexer.NAME]) {
if skipped, err := skip(parser, lexer.TokenKind[lexer.AMP]); !skipped {
break
} else if err != nil {
return types, err
}
}
}
Expand Down
48 changes: 43 additions & 5 deletions language/parser/schema_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ func TestSchemaParser_SimpleTypeInheritingInterface(t *testing.T) {
}

func TestSchemaParser_SimpleTypeInheritingMultipleInterfaces(t *testing.T) {
body := `type Hello implements Wo, rld { }`
body := `type Hello implements Wo & rld { }`
astDoc := parse(t, body)
expected := ast.NewDocument(&ast.Document{
Loc: testLoc(0, 33),
Loc: testLoc(0, 34),
Definitions: []ast.Node{
ast.NewObjectDefinition(&ast.ObjectDefinition{
Loc: testLoc(0, 33),
Loc: testLoc(0, 34),
Name: ast.NewName(&ast.Name{
Value: "Hello",
Loc: testLoc(5, 10),
Expand All @@ -224,9 +224,47 @@ func TestSchemaParser_SimpleTypeInheritingMultipleInterfaces(t *testing.T) {
ast.NewNamed(&ast.Named{
Name: ast.NewName(&ast.Name{
Value: "rld",
Loc: testLoc(26, 29),
Loc: testLoc(27, 30),
}),
Loc: testLoc(26, 29),
Loc: testLoc(27, 30),
}),
},
Fields: []*ast.FieldDefinition{},
}),
},
})
if !reflect.DeepEqual(astDoc, expected) {
t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
}
}

func TestSchemaParser_SimpleTypeInheritingMultipleInterfacesWithLeadingAmpersand(t *testing.T) {
body := `type Hello implements & Wo & rld { }`
astDoc := parse(t, body)
expected := ast.NewDocument(&ast.Document{
Loc: testLoc(0, 36),
Definitions: []ast.Node{
ast.NewObjectDefinition(&ast.ObjectDefinition{
Loc: testLoc(0, 36),
Name: ast.NewName(&ast.Name{
Value: "Hello",
Loc: testLoc(5, 10),
}),
Directives: []*ast.Directive{},
Interfaces: []*ast.Named{
ast.NewNamed(&ast.Named{
Name: ast.NewName(&ast.Name{
Value: "Wo",
Loc: testLoc(24, 26),
}),
Loc: testLoc(24, 26),
}),
ast.NewNamed(&ast.Named{
Name: ast.NewName(&ast.Name{
Value: "rld",
Loc: testLoc(29, 32),
}),
Loc: testLoc(29, 32),
}),
},
Fields: []*ast.FieldDefinition{},
Expand Down
4 changes: 2 additions & 2 deletions language/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
str := join([]string{
"type",
name,
wrap("implements ", join(interfaces, ", "), ""),
wrap("implements ", join(interfaces, " & "), ""),
join(directives, " "),
block(fields),
}, " ")
Expand All @@ -551,7 +551,7 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
str := join([]string{
"type",
name,
wrap("implements ", join(interfaces, ", "), ""),
wrap("implements ", join(interfaces, " & "), ""),
join(directives, " "),
block(fields),
}, " ")
Expand Down
2 changes: 1 addition & 1 deletion language/printer/schema_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestSchemaPrinter_PrintsKitchenSink(t *testing.T) {
mutation: MutationType
}

type Foo implements Bar {
type Foo implements Bar & Baz {
one: Type
two(argument: InputType!): Type
three(argument: InputType, other: String): Int
Expand Down
2 changes: 1 addition & 1 deletion schema-kitchen-sink.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ schema {
mutation: MutationType
}

type Foo implements Bar {
type Foo implements Bar & Baz {
one: Type
two(argument: InputType!): Type
three(argument: InputType, other: String): Int
Expand Down