Docblock tags
Scalars
On this page
To define a GraphQL custom scalar, put a @gqlScalar docblock directly before a type alias declaration. As with other tags, the name after the tag, if any, is the scalar's name in the schema, when it differs from the type's name.
/**
* A description of my custom scalar.
* @gqlScalar
*/
export type MyCustomString = string;"""A description of my custom scalar."""
scalar MyCustomStringBuilt-in scalars
Note
For the built-in GraphQL scalars that have no TypeScript type of their own, gratos exports type aliases to import. Gratos may prompt you to use one of them when you use number in a position where it must infer a GraphQL type.
import type { Float, Int, ID } from "gratos";
/** @gqlType */
class Calculator {
/** @gqlField */
id: ID;
constructor(id: ID) {
this.id = id;
}
/** @gqlField */
round(args: { float: Float }): Int {
return Math.round(args.float);
}
}type Calculator {
id: ID
round(float: Float!): Int
}The @specifiedBy directive
The GraphQL specification defines the @specifiedBy directive for custom scalar definitions. It provides a "scalar specification URL for specifying the behavior of custom scalar types". With directive annotations, you can add it to your custom scalars:
/**
* @gqlScalar
* @gqlAnnotate specifiedBy(url: "https://tools.ietf.org/html/rfc4122")
*/
export type UUID = string;scalar UUID @specifiedBy(url: "https://tools.ietf.org/html/rfc4122")Serialization and parsing of custom scalars
When you define a custom scalar, the generated getSchema takes a configuration object for the scalar, which can define how values of that type are serialized and parsed.
For example, with a Date scalar:
/** @gqlScalar Date */
export type GqlDate = Date;scalar DateThe generated getSchema requires a config object, in which you supply serialize, parseValue and parseLiteral for this type. If Date is serialized as a Unix timestamp:
import { getSchema } from "./generated/schema.ts"; // Generated by Gratos
import type { SchemaConfig } from "./generated/schema.ts";
// `new Date` takes any number, and out of range it returns an invalid Date, whose time is NaN.
function validDate(date: Date, where: string): Date {
if (!Number.isFinite(date.getTime())) {
throw new Error(`${where}: value is not a valid date`);
}
return date;
}
const config: SchemaConfig = {
scalars: {
Date: {
serialize(value): number {
if (!(value instanceof Date)) {
throw new Error("Date.serialize: value is not a Date object");
}
return validDate(value, "Date.serialize").getTime();
},
parseValue(value): Date {
if (typeof value !== "number") {
throw new Error("Date.parseValue: value is not a number");
}
return validDate(new Date(value), "Date.parseValue");
},
parseLiteral(ast): Date {
if (!(ast.kind === "IntValue" || ast.kind === "StringValue")) {
throw new Error("Date.parseLiteral: ast.kind is not IntValue or StringValue");
}
return validDate(new Date(Number(ast.value)), "Date.parseLiteral");
},
},
},
};
export const schema = getSchema(config);
// ... Continue on, passing the schema to a GraphQL server