File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -488,6 +488,9 @@ func parseUnionDef(l *common.Lexer) *ast.Union {
488488
489489 union .Directives = common .ParseDirectives (l )
490490 l .ConsumeToken ('=' )
491+ if l .Peek () == '|' {
492+ l .ConsumeToken ('|' )
493+ }
491494 union .TypeNames = []string {l .ConsumeIdent ()}
492495 for l .Peek () == '|' {
493496 l .ConsumeToken ('|' )
Original file line number Diff line number Diff line change @@ -1124,6 +1124,42 @@ func TestInterfaceImplementsInterface(t *testing.T) {
11241124 return nil
11251125 },
11261126 },
1127+ {
1128+ name : "Unions can be defined with a leading pipe" ,
1129+ sdl : `
1130+ type Named {
1131+ name: String!
1132+ }
1133+ type Numbered {
1134+ num: Int!
1135+ }
1136+ union Item1 =
1137+ | Named
1138+ | Numbered
1139+ union Item2 = | Named | Numbered
1140+ ` ,
1141+ validateSchema : func (s * ast.Schema ) error {
1142+ for _ , itemName := range []string {"Item1" , "Item2" } {
1143+ typ , ok := s .Types [itemName ].(* ast.Union )
1144+ if ! ok {
1145+ return fmt .Errorf ("type %q not found" , "Item" )
1146+ }
1147+ if len (typ .UnionMemberTypes ) != 2 {
1148+ return fmt .Errorf ("Expected 2 possible types, but instead got %d types" , len (typ .UnionMemberTypes ))
1149+ }
1150+ posible := map [string ]struct {}{
1151+ "Named" : {},
1152+ "Numbered" : {},
1153+ }
1154+ for _ , pt := range typ .UnionMemberTypes {
1155+ if _ , ok := posible [pt .Name ]; ! ok {
1156+ return fmt .Errorf ("Unexpected possible type %q" , pt .Name )
1157+ }
1158+ }
1159+ }
1160+ return nil
1161+ },
1162+ },
11271163 } {
11281164 t .Run (tt .name , func (t * testing.T ) {
11291165 s , err := schema .ParseSchema (tt .sdl , tt .useStringDescriptions )
You can’t perform that action at this time.
0 commit comments