ballingt
ballingt2y ago

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
allen
allen2y ago
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.
ian
ian2y ago
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
allen
allen2y ago
I see. I'll take a closer look, but at my intial pass it didnt like the query response type, which looks like:
export interface UserInterface extends Document<'users'> {
cause: Document<'causes'> | null;
nonprofit: Document<'nonprofits'>;
}
export interface UserInterface extends Document<'users'> {
cause: Document<'causes'> | null;
nonprofit: Document<'nonprofits'>;
}
ian
ian2y ago
You will have to cast back to specific types after jsonToConvex, iirc
allen
allen2y ago
This is what I'm seeing:
No description
allen
allen2y ago
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.
ian
ian2y ago
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?
ballingt
ballingtOP2y ago
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 alright
allen
allen2y ago
It does appear to like this:
export type UserInterface = Document<'users'> & {
cause: Document<'causes'> | null;
nonprofit: Document<'nonprofits'>;
}
export type UserInterface = Document<'users'> & {
cause: Document<'causes'> | null;
nonprofit: Document<'nonprofits'>;
}
>casting to any here would be alright Understood -- was more of an issue of me thinking it was an unsupported input for convexToJson
ballingt
ballingtOP2y ago
that's a good point, yeah casting would turn this into a runtime error instead
allen
allen2y ago
I can redefine my interfaces as types, but it would be nice if convexToJson wasn't so sensitive to this difference.
ballingt
ballingtOP2y ago
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
allen
allen2y ago
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
ballingt
ballingtOP2y ago
Cohesive server-side solutions are coming where we do the serialization for you but in the meantime this makes a lot of sense
allen
allen2y ago
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 error
ballingt
ballingtOP2y ago
we gotta bring you back that typesafety!

Did you find this page helpful?