-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typesafe serialization and deserialization of scalars #5949
Comments
Another sidenote: if we agree that this is in-scope for the library and if we agree on a design for it, i would be willing to contribute this too. |
Set in the config the following:
Then you can add a global.d.ts in your project, where u can define your custom types. This types will be accessible in your project without any import. |
@mastorm this is in interesting area we have been thinking about. Some possibly related issue from GraphQL Scalars: |
As @balazssoltesz suggested, you can use |
If I understand correctly, the thing @mastorm pointing is serialization/deserialization and type safety. To work around, I'm using Branded Type: // externs.ts
type Brand<T, B extends string> = T &
{
readonly [k in B]: symbol;
};
export type MyDatetime = Brand<string, '__MyDatetime__'>;
/*
const dt: MyDatetime = '2011-10-05T14:48:00.000Z'; // error!: Type 'string' is not assignable to type 'MyDatetime'.
*/ and declare scalars:
DateTime: "externs#MyDateTime" then write a serializer from Serializer issue still remains but type safety could be assured. |
Codegen doesn't aim this kind of deserialization. You need to implement it as part of client library. |
I'm facing the same issue. My only idea is to serialize & deserialize date fields wherever they are read from / written to in the code which leads to a lot of boilerplate code. @ardatan you wrote
What do you mean with 'as part of the client library'? How could I write a central de-serializer that codegen uses for |
Facing this issue as well. Have had to write boilerplate scalar conversion code to handle this. It feels like a natural extension of the |
Is your feature request related to a problem? Please describe.
We use a lot of custom scalars in our schema. Following are three small examples:
Currently, the only option we have been able to find is to use the
ScalarsMap
to map them to strings respectively:I am aware of the fact that you can map scalars to custom types aside from string too, but serializing to Date obviously doesnt work since graphql-codegen has no concept(that im aware of) that allows specifying how a specific scalar is deserialized.
That leaves me with what we have currently, which is telling graphql-codegen that the scalar should be mapped to string and then deserialize them at every occurence in my code which results in noisy and repetitive code for both mutations and queries.
Describe the solution you'd like
I would like to have some way to specify scalar types on the client safely. Here is my general idea:
scalarMappers
Describe alternatives you've considered
The current workaround we use is outlined in the first bullet point. Another option i considered was writing a custom plugin, but from the docs it seemed like that would require me to fork the typescript plugin since i want to modify the output of that. If theres a way to modify the output of the typescript plugin, let me know please. I sadly wasnt able to find one.
The text was updated successfully, but these errors were encountered: