There are indeed things in the works for
There are indeed things in the works for Next.js and server-side rendering, but for right now calling
convexToJson()
(imported from 'convex/values'
) on the query result to get something JSON-serializable is the ticket.16 Replies
I did take a look at that api, but it wasnt clear how to utilize it. I'm guessing unless you are passing it an instance of
Document
it doesnt like it.
Plus its not the greatest solution, as Next abstracts the serialization/deserialization away and prefers you pass in a typed object so that the server and client can work together more seamlessly.Agreed. We’re working on a better solution. For now, it will accept any value- document or nested object / value. With 0.9.2 there’s Infer for typing nested values.
It doesn’t serialize to a string, just to a json-serializable thing that can re-hydrate Map and Id and such
I see. I'll take a closer look, but at my intial pass it didnt like the query response type, which looks like:
You will have to cast back to specific types after jsonToConvex, iirc
This is what I'm seeing:

Am I miss understanding that this doesnt iterate over an object and make it a POJO? Do I need to convert individual properties?
With 0.9.2 there’s Infer for typing nested values.Perhaps I'm misunderstanding what this should do.
No I think your assumptions are right. Im not a ts expert but my hunch is that it’s concerned that UserInterface might have non-string indexes beyond what is declared. Does it get any better if you declare it as a type rather than an interface?
Interested in what Ian's suggesting, but also pragmatically you're going to lose type information once you run
convexToJson
anyway and need to cast it once you run jsonToConvex
on the client side, so casting to any here would be alrightIt does appear to like this:
>casting to any here would be alright
Understood -- was more of an issue of me thinking it was an unsupported input for
convexToJson
that's a good point, yeah casting would turn this into a runtime error instead
I can redefine my interfaces as types, but it would be nice if
convexToJson
wasn't so sensitive to this difference.I think Ian's right that this is an issue of convincing TypeScript that the extended interface is limited to this value, and it's possible we could do something here to fix
Also, a way to have type safety on the returned
JSONValue
would be nice
eg: having my getServerSideProps
return something like JSONValue<UserInterface>
, the client side it understanding that jsonToConvex
of that value would return UserInterface
Cohesive server-side solutions are coming where we do the serialization for you but in the meantime this makes a lot of sense
Hmm, so
convexToJson
is working on turning everything, including nested documents, into POJO, however, jsonToConvex
doesnt rehydrate the nested documents. They are undefined.
disregard. lack of typesafety introduce human errorwe gotta bring you back that typesafety!