func TestSchemaParser_UnionWithTwoTypes(t *testing.T) {
	body := `union Hello = Wo | Rld`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: loc(0, 22),
		Definitions: []ast.Node{
			ast.NewUnionTypeDefinition(&ast.UnionTypeDefinition{
				Loc: loc(0, 22),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   loc(6, 11),
				}),
				Types: []*ast.NamedType{
					ast.NewNamedType(&ast.NamedType{
						Loc: loc(14, 16),
						Name: ast.NewName(&ast.Name{
							Value: "Wo",
							Loc:   loc(14, 16),
						}),
					}),
					ast.NewNamedType(&ast.NamedType{
						Loc: loc(19, 22),
						Name: ast.NewName(&ast.Name{
							Value: "Rld",
							Loc:   loc(19, 22),
						}),
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}
Esempio n. 2
0
func parseUnionTypeDefinition(parser *Parser) (*ast.UnionTypeDefinition, error) {
	start := parser.Token.Start
	_, err := expectKeyWord(parser, "union")
	if err != nil {
		return nil, err
	}
	name, err := parseName(parser)
	if err != nil {
		return nil, err
	}
	_, err = expect(parser, lexer.TokenKind[lexer.EQUALS])
	if err != nil {
		return nil, err
	}
	types, err := parseUnionMembers(parser)
	if err != nil {
		return nil, err
	}
	return ast.NewUnionTypeDefinition(&ast.UnionTypeDefinition{
		Name:  name,
		Loc:   loc(parser, start),
		Types: types,
	}), nil
}