De Bruijn-Erd\H{o}s type theorems for graphs and posets