You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Custom Scalar support is important for many projects. GraphQL only natively supports ID, String, Int, Float, and Boolean. Significantly missing is Date/DateTime. We are also using BigInt, Decimal.js and DateOnly (without a time). Apollo Server has good support for serializing and deserializing custom scalars, but that feature is missing from the client.
The key to implementation in the client is knowing which fields are of the custom type. One workaround is to serialize the field on the server as a more complex type with a __typename attribute. We can then use ApolloLink to deserialize on the client. While a little verbose over the wire, this works quite well, except in systems that don't use ApolloLink (eg, we're using Retool which appears to have no support for a global transformer). We often want to serialize the field as a simple string, and then somehow know which fields should be deserialized.
A documented solution to this problem is to use type policies in the Apollo cache configuration. For each field, we can specify a "read" method which could deserialize. However, there is no way to programmatically know which fields we should specify, and doing this by hand is brittle.
I propose a plugin for codegen which outputs a list of fields and types for our custom scalars.
Custom Scalar support is important for many projects. GraphQL only natively supports ID, String, Int, Float, and Boolean. Significantly missing is Date/DateTime. We are also using BigInt, Decimal.js and DateOnly (without a time). Apollo Server has good support for serializing and deserializing custom scalars, but that feature is missing from the client.
The key to implementation in the client is knowing which fields are of the custom type. One workaround is to serialize the field on the server as a more complex type with a __typename attribute. We can then use ApolloLink to deserialize on the client. While a little verbose over the wire, this works quite well, except in systems that don't use ApolloLink (eg, we're using Retool which appears to have no support for a global transformer). We often want to serialize the field as a simple string, and then somehow know which fields should be deserialized.
A documented solution to this problem is to use type policies in the Apollo cache configuration. For each field, we can specify a "read" method which could deserialize. However, there is no way to programmatically know which fields we should specify, and doing this by hand is brittle.
I propose a plugin for codegen which outputs a list of fields and types for our custom scalars.
A possible implementation is on my fork at:
https://github.com/frec-kenneth/graphql-code-generator-community/tree/kenneth/custom-scalar-usage/packages/plugins/typescript/custom-scalar-usage
As shown in the spec, for a given schema:
this will output a const
and then we can use the
scalarUsage
in our cache config to output all the typepolicies that are needed to deserialize the fields.Thanks for your consideration.
The text was updated successfully, but these errors were encountered: