pylon icon indicating copy to clipboard operation
pylon copied to clipboard

Custom Scalar Types

Open schettn opened this issue 10 months ago • 1 comments

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:

  1. How should non-primitive scalars (e.g., Money) be defined? Should developers use GraphQLScalarType, 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?
  2. Since Money isn’t a built-in type, what should be returned for a Money query? Should it be an instance of a Money class (new Money(...)), a plain object, or another representation? How should this type be structured to ensure compatibility with GraphQL and JavaScript?
  3. Is there an easier way to define custom scalars than using new GraphQLScalarType where you must implement serialize, parseValue, and parseLiteral? Could a utility or abstraction simplify this process?

Potential Benefits:

  • Improves type safety in GraphQL schemas
  • Allows handling of complex numeric types like BigInt and Decimal
  • Ensures better interoperability with backend services handling monetary values
  • Enables support for non-primitive custom scalars like Money, DateTime, or UUID

schettn avatar Mar 18 '25 06:03 schettn

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 🤟

MarcDanjou avatar Jul 03 '25 13:07 MarcDanjou