Is there a "non-trivial" function $f(x,y)$ such that
$$f(x,y) \in \mathbb{Q} \iff x,y\in \mathbb{Q}?$$
An example of a "trivial" function would be
$$f(x,y) = \begin{cases} 0 & x,y\in \mathbb{Q}\\ \pi & \text{otherwise} \end{cases}$$ or any other $f$ which effectively uses a cases function.
The motivation is just my curiosity. Obviously, operations which preserve one direction of the $\iff$ are plentiful and well-studied. I was wondering how onerous the condition of the additional direction is on the choice of $f$. This question on mathoverflow seems related.

Perhaps a slightly less trivial example would be the function $f$ that interleaves the decimal digits of $x$ and $y$: that is, if \begin{align} x&=0.x_1x_2x_3\dots \\ y&=0.y_1y_2y_3\dots \end{align} are decimal expansions of $x$ and $y$, then $$ f(x,y)=0.x_1y_1x_2y_2x_3y_3\dots $$
You can make this definition unambiguous by deciding to always (or never) take finite decimal expansions when they are available.
Then $f(x,y)$ has a repeating decimal expansion if and only if both $x$ and $y$ do, and so it satisfies your condition.