Ejemplo n.º 1
0
func parseType(parser *Parser) (ast.Type, error) {
	start := parser.Token.Start
	var ttype ast.Type
	if skip(parser, lexer.TokenKind[lexer.BRACE_L]) {
		t, err := parseType(parser)
		if err != nil {
			return t, err
		}
		ttype = t
		_, err = expect(parser, lexer.TokenKind[lexer.BRACKET_R])
		if err != nil {
			return ttype, err
		}
		ttype = ast.NewListType(&ast.ListType{
			Type: ttype,
			Loc:  loc(parser, start),
		})
	} else {
		name, err := parseNamedType(parser)
		if err != nil {
			return ttype, err
		}
		ttype = name
	}
	if skip(parser, lexer.TokenKind[lexer.BANG]) {
		ttype = ast.NewNonNullType(&ast.NonNullType{
			Type: ttype,
			Loc:  loc(parser, start),
		})
		return ttype, nil
	}
	return ttype, nil
}
func TestSchemaParser_SimpleNonNullType(t *testing.T) {

	body := `
type Hello {
  world: String!
}`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: loc(1, 32),
		Definitions: []ast.Node{
			ast.NewObjectTypeDefinition(&ast.ObjectTypeDefinition{
				Loc: loc(1, 32),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   loc(6, 11),
				}),
				Interfaces: []*ast.NamedType{},
				Fields: []*ast.FieldDefinition{
					ast.NewFieldDefinition(&ast.FieldDefinition{
						Loc: loc(16, 30),
						Name: ast.NewName(&ast.Name{
							Value: "world",
							Loc:   loc(16, 21),
						}),
						Arguments: []*ast.InputValueDefinition{},
						Type: ast.NewNonNullType(&ast.NonNullType{
							Kind: "NonNullType",
							Loc:  loc(23, 30),
							Type: ast.NewNamedType(&ast.NamedType{
								Loc: loc(23, 29),
								Name: ast.NewName(&ast.Name{
									Value: "String",
									Loc:   loc(23, 29),
								}),
							}),
						}),
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}