Deserializing specific scalar type #3777
Replies: 2 comments
-
This is probably not the answer you're looking for but it sounds like the operation you're doing is inherently "unsafe" or rather not a sane type derivation in a type system. Basically, given that you have several inputs that serialise to JSON, some of them strings, technically speaking, only the schema can tell you if a given field's scalar is a However, shipping a schema to the client-side (while possible and we do have a minification tool for that and someone has made an exchange for scalar deserialisation that uses this) is expensive inherently. This question is similar to this btw: 0no-co/gql.tada#439 (comment) This also contains my preferred and recommended solution: tagged types and deserialisers. Basically, add a "shadow type" to your Date types and let your type generator take care of the problem. Basically, these are called branded types in TS most of the time Then have a helper that deserialises this branded type in your UI/view/consumer code outside of the GraphQL client. The branded/shadow type enforces that you must use your helper and it solves this problems with a zero-cost abstraction |
Beta Was this translation helpful? Give feedback.
-
Understood! I appreciate the response. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to create a scalar (implemented as an ISO 8601 string) called Instant that will be automatically deserialized to a TypeScript Date object from responses.
I've added an exchange that serializes correctly and deserializes correctly when I use regexp to identify ISO 8601 strings, but sometimes I want date strings to stay strings and sometimes I want them to be converted to Date objects.
How can I get my deserializer to know only to convert Instants and not Strings?
Beta Was this translation helpful? Give feedback.
All reactions