func TestSchemaParser_UnionWithTwoTypes(t *testing.T) {
	body := `union Hello = Wo | Rld`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: testLoc(0, 22),
		Definitions: []ast.Node{
			ast.NewUnionDefinition(&ast.UnionDefinition{
				Loc: testLoc(0, 22),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   testLoc(6, 11),
				}),
				Types: []*ast.Named{
					ast.NewNamed(&ast.Named{
						Loc: testLoc(14, 16),
						Name: ast.NewName(&ast.Name{
							Value: "Wo",
							Loc:   testLoc(14, 16),
						}),
					}),
					ast.NewNamed(&ast.Named{
						Loc: testLoc(19, 22),
						Name: ast.NewName(&ast.Name{
							Value: "Rld",
							Loc:   testLoc(19, 22),
						}),
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}
示例#2
0
func parseUnionTypeDefinition(parser *Parser) (*ast.UnionDefinition, 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.NewUnionDefinition(&ast.UnionDefinition{
		Name:  name,
		Loc:   loc(parser, start),
		Types: types,
	}), nil
}