remorses / genql

Type safe TypeScript client for any GraphQL API
https://genql.dev
MIT License
881 stars 37 forks source link

Allow custom serializer/deserializer for custom scalars #57

Open DanielRose opened 3 years ago

DanielRose commented 3 years ago

Using the CLI option --scalar, it is possible to set the Typescript type of custom scalars. However, that only modifies the generated interfaces. The actual value of the scalar is unchanged.

For easier working with the scalar values after a query/subscription, they may need to be deserialized differently than the default. The same with a mutation: the scalars may need to be serialized differently than the default. While it is possible to do this manually, it would be much easier and less error-prone if this could be done by passing custom serializer/deserializer functions.

For example, a custom scalar could be "Date". The actual values will be transferred as strings. For easier usage, it would be much better if they were deserialized after a query into a Javascript Date or Luxon DateTime. For a mutation, the Date/DateTime would probably need to be serialized as string again (instead of hoping the default serialization will work).

DanielRose commented 3 years ago

graphql-typed-client has something like this: https://github.com/helios1138/graphql-typed-client#custom-scalar-type-mapping

jasonkuhrt commented 3 years ago

Was looking for this tonight for https://github.com/prisma/nexus-prisma/pull/56#discussion_r656678366.

ayoung19 commented 1 year ago

@remorses also, is this feature on the roadmap?

bennyk8y commented 7 months ago

@remorses it would be really really great to have it

jasonkuhrt commented 7 months ago

fwiw I am working on making graphql-request type safe and it will have custom scalar support. The PR for it is almost done.