Example #1
0
func parseInterfaceTypeDefinition(parser *Parser) (*ast.InterfaceTypeDefinition, error) {
	start := parser.Token.Start
	_, err := expectKeyWord(parser, "interface")
	if err != nil {
		return nil, err
	}
	name, err := parseName(parser)
	if err != nil {
		return nil, err
	}
	iFields, err := any(parser, lexer.TokenKind[lexer.BRACE_L], parseFieldDefinition, lexer.TokenKind[lexer.BRACE_R])
	if err != nil {
		return nil, err
	}
	fields := []*ast.FieldDefinition{}
	for _, iField := range iFields {
		if iField != nil {
			fields = append(fields, iField.(*ast.FieldDefinition))
		}
	}
	return ast.NewInterfaceTypeDefinition(&ast.InterfaceTypeDefinition{
		Name:   name,
		Loc:    loc(parser, start),
		Fields: fields,
	}), nil
}
func TestSchemaParser_SimpleInterface(t *testing.T) {
	body := `
interface Hello {
  world: String
}`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: loc(1, 36),
		Definitions: []ast.Node{
			ast.NewInterfaceTypeDefinition(&ast.InterfaceTypeDefinition{
				Loc: loc(1, 36),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   loc(11, 16),
				}),
				Fields: []*ast.FieldDefinition{
					ast.NewFieldDefinition(&ast.FieldDefinition{
						Loc: loc(21, 34),
						Name: ast.NewName(&ast.Name{
							Value: "world",
							Loc:   loc(21, 26),
						}),
						Arguments: []*ast.InputValueDefinition{},
						Type: ast.NewNamedType(&ast.NamedType{
							Loc: loc(28, 34),
							Name: ast.NewName(&ast.Name{
								Value: "String",
								Loc:   loc(28, 34),
							}),
						}),
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}