Prayer in the graveyard
All of the earth is a Masjid except for the graveyards and the Hammaam [washrooms].
[Jaami' al-Tirmidhi]
Based on this Hadiith, you cannot pray in the graveyard. To pray in a graveyard is not permissible because the Prophet (sallallaahu alayhi wa sallam) said it is not a place to pray.
You cannot pray in a graveyard or towards a grave.
Praying in a graveyard opens the doors to shirk. Similarly, praying towards a grave opens doors to shirk. Islamic legislation has forbidden anything which leads to shirk.
The devil flows inside of you and me like our blood. First, he opens up the doors leading to shirk. Then, he pushes us into shirk. Hence, Islamic legislation has deemed impermissible anything which acts as a means to shirk.
If you pray in a graveyard your prayer is invalid. This includes the obligatory and voluntary prayers.
You can offer the funeral prayer over a grave in the graveyard. This is established in a Sahih Hadiith.
Narrated Abu Huraira:
A black man or a black woman used to sweep the mosque and he or she died. The Prophet (ﷺ) asked about her (or him). He was told that she (or he) had died. He said, "Why did you not inform me? Show me his grave (or her grave)." So he went to her (his) grave and offered her (his) funeral prayer."
[Sahih al-Bukhaari Hadiith No. 458]
The funeral prayer is an exception to the general rule. You cannot pray any prayer in the graveyard except the funeral prayer.
The first hadiith was general. The second one specifies it. You cannot pray obligatory or voluntary prayers in the graveyard. Nor can you pray towards a grave.
However, you can pray the funeral prayer over a grave in a graveyard. The above mentioned hadith makes an exception for the funeral prayer.