Initial work on moving the query to the server

/cc @hdgarrood 

This isn't ready yet. I still need to work on the client-side changes, which will probably be considerable, but this implements the bare-bones server-side functionality needed for the new architecture.