From b8ccc25c26247d3005c97727271e6b06459a53ff Mon Sep 17 00:00:00 2001 From: Camden Cheek Date: Sat, 19 Oct 2024 16:48:40 -0600 Subject: [PATCH] support leading pipe in union definition --- internal/schema/schema.go | 3 +++ internal/schema/schema_test.go | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/internal/schema/schema.go b/internal/schema/schema.go index 84bb6fe1..03bdfb83 100644 --- a/internal/schema/schema.go +++ b/internal/schema/schema.go @@ -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('|') diff --git a/internal/schema/schema_test.go b/internal/schema/schema_test.go index 73cc6d41..594afb46 100644 --- a/internal/schema/schema_test.go +++ b/internal/schema/schema_test.go @@ -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)