Dr. Marawan Shahin, Professor of Hadith and its Sciences, Faculty of Usul Ad-Din (Theology),Al-Azhar Univ. states the following: “There are two points that should be clear in tackling this issue:
1. We are forbidden to take graves as mosques. So, from the very beginning Muslims should never turn graves into mosques.
2. As for the issue of offering Prayer in mosques including graves or there are graves attached to them, the surface meaning of the textual proofs does not forbid offering prayer in such mosques. What is important is that praying Muslims should not offer prayer while the grave is in front of them.
Focusing more on your issue, as there are walls separating between the mosque and the graves, there is nothing wrong at all to offer prayer at this mosque.”