You are asked to interpret $g$ as a function of $x$ and $q$ but $y$ is still present in the defining equation $g=f-qy$. So you need the additional equation
\begin{align*}
q = \frac{\partial f}{\partial y}(x,y)
\end{align*}
to express $y$ as function of $q$ (and $x$). For that purpose the function $y\mapsto \frac{\partial f}{\partial y}(x,y)$ must be bijective (for every $x$). For ensuring bijectivity it is often assumed that $f$ is strongly convex.
The partial derivative $\frac{\partial g}{\partial x}$ actually already follows from the total differential
\begin{align*}
dg = p\cdot dx - y\cdot dq
\end{align*}
But also your hard way to calculate $\frac{\partial g}{\partial x}$ works if you carefully consider the dependencies:
\begin{align*}
g(x,q) &= f(x,y(x,q)) - q\cdot y(x,q)\\
\frac{\partial g}{\partial x} &= \frac{\partial f}{\partial x} + \frac{\partial f}{\partial y}\cdot\frac{\partial y}{\partial x} - q\cdot\frac{\partial y}{\partial x}\\
&= p + q\cdot \frac{\partial y}{\partial x} - q\cdot\frac{\partial y}{\partial x}\\
&= p
\end{align*}
Your example $f(x,y) = xy$ is inappropriate since $q=\frac{\partial f}{\partial y} = x$ is not a bijective mapping from $y$ to $q$.
We can fix that by modifying it to $f(x,y)=xy+\frac12y^2$ giving $q=x+y$ which has the inverse $y=q-x$. The Legendre-transformed is then
\begin{align*}
g(x,q) &= x\cdot(q-x) + \frac12\left(q-x\right)^2 - q\cdot(q-x)\\
&= -\frac12 x^2 +qx -\frac12 q^2\\
&= -\frac12(x-q)^2
\end{align*}
and
\begin{align*}
\frac{\partial g}{\partial x} &= q-x
\end{align*}
For comparison: $p=\frac{\partial f}{\partial x} = y = q-x$//.
Note, that you find some stuff about the background of the Legendre transformation in Arnold's book on Mathematical Methods of Classical Mechanics.