liontariai / samarium

https://liontari.ai/
Apache License 2.0
14 stars 0 forks source link

Implement Custom Clientside Scalars #3

Closed liontariai closed 4 months ago

liontariai commented 4 months ago

This PR enables custom scalar parsing on the client side. Values are parsed lazily, only when accessed.

As default, the following scalars are implement as follows:

DateTime: (value: string) => new Date(value),
Date: (value: string) => new Date(value),
Time: (value: string) => new Date(value),
JSON: (value: string) => JSON.parse(value),

Arrays and nested arrays of custom scalars are supported.

Custom parsing implementations can be configured with the .init function:

import dates from "./dates";

dates.init({
    scalars: {
        DateTime: (v: string) => new Date(new Date(v).getTime() * 1.2),
    },
});
liontariai commented 4 months ago

This feature is similar to what is being discussed at Apollo Client here: https://github.com/apollographql/apollo-feature-requests/issues/368

Right now only the canonical examples Date and JSON are supported, because not only the custom parse function is needed, but also a type mapping.

For this, the question is if it has to be either provided on codegen-time or it can be configured in runtime with a config-factory approach, where a type generic is passed and thus assigns the right type to the fields in Typescript.