Custom Scalar Types
Description:
Currently, GraphQL supports basic scalar types like Int, Float, String, etc. However, certain applications require custom scalar types for specific use cases, such as handling BigInt, Decimal, or Money values. This proposal suggests adding support for defining and using custom scalars in our GraphQL schema.
Proposed Implementation:
export const graphql = {
Query: {
getBigNumber: () => BigInt('12345678901234567890')
},
scalars: {
// (Note that the key must be the same as the type you return above)
BigInt: GraphQLBigInt
}
};
Open Questions:
- How should non-primitive scalars (e.g.,
Money) be defined? Should developers useGraphQLScalarType, or should we introduce a base class for scalars that can be extended? What best practices should be followed for defining and registering these scalars? - Since
Moneyisn’t a built-in type, what should be returned for aMoneyquery? Should it be an instance of aMoneyclass (new Money(...)), a plain object, or another representation? How should this type be structured to ensure compatibility with GraphQL and JavaScript? - Is there an easier way to define custom scalars than using
new GraphQLScalarTypewhere you must implementserialize,parseValue, andparseLiteral? Could a utility or abstraction simplify this process?
Potential Benefits:
- Improves type safety in GraphQL schemas
- Allows handling of complex numeric types like
BigIntandDecimal - Ensures better interoperability with backend services handling monetary values
- Enables support for non-primitive custom scalars like
Money,DateTime, orUUID
Hello @schettn did you decide something about custom scalar types integrations ? currently we are forced to transform our bigints to text in the API, this is non-blocking, but on other frameworks we use to integrate some scalars like https://www.npmjs.com/package/graphql-scalars
Thanks in advance 🤟