Example #1
0
/**
 * InterfaceTypeDefinition : interface Name { FieldDefinition+ }
 */
func parseInterfaceTypeDefinition(parser *Parser) (*ast.InterfaceDefinition, 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.NewInterfaceDefinition(&ast.InterfaceDefinition{
		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: testLoc(1, 36),
		Definitions: []ast.Node{
			ast.NewInterfaceDefinition(&ast.InterfaceDefinition{
				Loc: testLoc(1, 36),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   testLoc(11, 16),
				}),
				Fields: []*ast.FieldDefinition{
					ast.NewFieldDefinition(&ast.FieldDefinition{
						Loc: testLoc(21, 34),
						Name: ast.NewName(&ast.Name{
							Value: "world",
							Loc:   testLoc(21, 26),
						}),
						Arguments: []*ast.InputValueDefinition{},
						Type: ast.NewNamed(&ast.Named{
							Loc: testLoc(28, 34),
							Name: ast.NewName(&ast.Name{
								Value: "String",
								Loc:   testLoc(28, 34),
							}),
						}),
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}