Astronomers have caught a star in the act of swallowing a planet whole - the first time such an event has been witnessed.