Scalars
Baeta provides support for custom GraphQL scalars, allowing you to define any data type beyond the built-in primitives.
Schema Definition
Define your scalars in a GraphQL schema file, for example src/modules/scalars/scalars.gql:
scalar DateTime
scalar UUID
Implementation
Register the scalar resolvers in your module using graphql-scalars or your own implementation:
// src/modules/scalars/index.ts
import { DateTimeResolver, UUIDResolver } from "graphql-scalars";
import { ScalarsModule } from "./typedef.ts";
export default ScalarsModule.$schema({
DateTime: DateTimeResolver,
UUID: UUIDResolver,
});
Custom TypeScript Types
To map a scalar to a custom TypeScript type, add it to the Scalars interface in src/modules/types.ts:
export interface Scalars extends BaseScalars {
UUID: `${string}-${string}-${string}-${string}-${string}`;
DateTime: Date;
}
For more details on the types file, see Custom Types.
If you don't add a custom type mapping, the scalar will default to any. Adding entries to the Scalars interface ensures full type safety in resolvers.
Usage in Resolvers
The scalars are automatically typed in your resolvers:
const { Query, User } = UserModule;
const userQuery = Query.user.resolve(() => {
return {
id: randomUUID(), // typed as UUID
birthday: new Date("1990-01-01"), // typed as Date
};
});
Built-in Scalars
GraphQL includes several built-in scalar types that don't require any additional configuration:
IntFloatStringBooleanID