Skip to main content
Version: Next (2.x)

Scalars

GraphQL ships with Int, Float, String, Boolean, and ID. For anything else like dates, UUIDs, or money, declare a custom scalar in your schema and register a resolver that handles serialization, parsing, and AST validation.

Schema definition

src/modules/scalars/scalars.gql:

scalar DateTime
scalar UUID

Implementation

Register the resolver in your module — graphql-scalars provides battle-tested implementations for most common types:

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

Without a TypeScript mapping, a custom scalar resolves to any in resolvers. Add an entry to the Scalars interface in src/modules/types.ts so every resolver gets the right type:

export interface Scalars extends BaseScalars {
UUID: `${string}-${string}-${string}-${string}-${string}`;
DateTime: Date;
}

For everything else in that file, see Custom Types.

Using scalars in resolvers

Scalar fields are typed automatically:

const { Query, User } = UserModule;

const userQuery = Query.user.resolve(() => {
return {
id: randomUUID(), // typed as UUID
birthday: new Date("1990-01-01"), // typed as Date
};
});