Esempio n. 1
0
func parseInputObjectTypeDefinition(parser *Parser) (*ast.InputObjectTypeDefinition, error) {
	start := parser.Token.Start
	_, err := expectKeyWord(parser, "input")
	if err != nil {
		return nil, err
	}
	name, err := parseName(parser)
	if err != nil {
		return nil, err
	}
	iInputValueDefinitions, err := any(parser, lexer.TokenKind[lexer.BRACE_L], parseInputValueDef, lexer.TokenKind[lexer.BRACE_R])
	if err != nil {
		return nil, err
	}
	fields := []*ast.InputValueDefinition{}
	for _, iInputValueDefinition := range iInputValueDefinitions {
		if iInputValueDefinition != nil {
			fields = append(fields, iInputValueDefinition.(*ast.InputValueDefinition))
		}
	}
	return ast.NewInputObjectTypeDefinition(&ast.InputObjectTypeDefinition{
		Name:   name,
		Loc:    loc(parser, start),
		Fields: fields,
	}), nil
}
func TestSchemaParser_SimpleInputObject(t *testing.T) {
	body := `
input Hello {
  world: String
}`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: loc(1, 32),
		Definitions: []ast.Node{
			ast.NewInputObjectTypeDefinition(&ast.InputObjectTypeDefinition{
				Loc: loc(1, 32),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   loc(7, 12),
				}),
				Fields: []*ast.InputValueDefinition{
					ast.NewInputValueDefinition(&ast.InputValueDefinition{
						Loc: loc(17, 30),
						Name: ast.NewName(&ast.Name{
							Value: "world",
							Loc:   loc(17, 22),
						}),
						Type: ast.NewNamedType(&ast.NamedType{
							Loc: loc(24, 30),
							Name: ast.NewName(&ast.Name{
								Value: "String",
								Loc:   loc(24, 30),
							}),
						}),
						DefaultValue: nil,
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}