Skip to content

Commit

Permalink
Merge pull request #654 from camdencheek/master
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelnikolov authored Feb 21, 2025
2 parents 8fb4624 + b8ccc25 commit 4eae033
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,9 @@ func parseUnionDef(l *common.Lexer) *ast.Union {

union.Directives = common.ParseDirectives(l)
l.ConsumeToken('=')
if l.Peek() == '|' {
l.ConsumeToken('|')
}
union.TypeNames = []string{l.ConsumeIdent()}
for l.Peek() == '|' {
l.ConsumeToken('|')
Expand Down
36 changes: 36 additions & 0 deletions internal/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,42 @@ func TestInterfaceImplementsInterface(t *testing.T) {
return nil
},
},
{
name: "Unions can be defined with a leading pipe",
sdl: `
type Named {
name: String!
}
type Numbered {
num: Int!
}
union Item1 =
| Named
| Numbered
union Item2 = | Named | Numbered
`,
validateSchema: func(s *ast.Schema) error {
for _, itemName := range []string{"Item1", "Item2"} {
typ, ok := s.Types[itemName].(*ast.Union)
if !ok {
return fmt.Errorf("type %q not found", "Item")
}
if len(typ.UnionMemberTypes) != 2 {
return fmt.Errorf("Expected 2 possible types, but instead got %d types", len(typ.UnionMemberTypes))
}
posible := map[string]struct{}{
"Named": {},
"Numbered": {},
}
for _, pt := range typ.UnionMemberTypes {
if _, ok := posible[pt.Name]; !ok {
return fmt.Errorf("Unexpected possible type %q", pt.Name)
}
}
}
return nil
},
},
} {
t.Run(tt.name, func(t *testing.T) {
s, err := schema.ParseSchema(tt.sdl, tt.useStringDescriptions)
Expand Down

0 comments on commit 4eae033

Please sign in to comment.