Custom scalars and types on the client (Typescript) #9370
-
I've been trying to get this to work all day yesterday and it sounds like it's possible but only with resolvers, they seem to be server side and I need to configure the scalars & types as they get parsed on the client side. Here is my scenario. Our schema and server is handled by a different team. And we have multiple graphql clients so it's sort of fixed. Codegen is working really well, but I'd like to tweak the parsed versions of what the client sees to make it more runtime-friendly. In our schema we have a Looks like this.
What I'd like to be able to do it have switch out codegen type to be a custom type. Preferably with some constructor so I can split the string into a decimal/number and currency type. something like this:
I have tried the following config
And its sort of working in that the generated code knows about it and imports it import { Money } from '../lib/money';
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
Date: any;
DateTime: any;
Decimal: any;
Money: Money; <---- This used to be `any` and this is what I want to see
}; But it's still just a Just still a string Where am I going wrong? I'm missing the bit where I can hook into the parsing and do some manipulation and possibly construct a different object where I pass in a string and split it into its decimal value and currency. I also plan to do a similar this with Date & DateTime scalars where I look at the contents of the date string and construct js Date objects based on if there is a time Hopefully, this made some sense. Just to reiterate I need to do this all client side if that's possible. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi @craigrushforth 👋
You GraphQL client may have ways to change a scalar value to match the type. For example, Apollo Client allows changing an object's field behaviour in the fetched data: https://www.apollographql.com/docs/react/caching/cache-field-behavior/ For example, if you were using Apollo Client, you may have to add it into const cache = new InMemoryCache({
typePolicies: {
Holding: { // Object type name
fields: {
marketValue: { // Field name
read(value) { // `value` is the value that comes from the Graph e.g. "0.00 USD"
return {
amount: getAmountFromString(value), // Just an example
currency: getCurrencyFromString(value) // Just an example
}
}
}
},
},
},
}); |
Beta Was this translation helpful? Give feedback.
Hi @craigrushforth 👋
scalars
option only changes the type of a Scalar but not the implementation, which seems to be what you are looking for.You GraphQL client may have ways to change a scalar value to match the type. For example, Apollo Client allows changing an object's field behaviour in the fetched data: https://www.apollographql.com/docs/react/caching/cache-field-behavior/
(I'm unclear if it can target all Scalars? 🤔 )
For example, if you were using Apollo Client, you may have to add it into
InMemoryCache
like this: