GraphQL Scalars is a comprehensive collection of custom GraphQL scalar types that provide precise type-safe validation for GraphQL schemas. It includes over 69 scalar types covering dates, numbers, strings, networking, geographic data, colors, financial data, identifiers, and many other specialized data formats.
npm install graphql-scalarsgraphql ^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0Individual Scalar Import:
import { GraphQLEmailAddress, GraphQLURL, GraphQLDate } from "graphql-scalars";Resolver Collection Import:
import { resolvers } from "graphql-scalars";Type Definitions Import:
import { typeDefs } from "graphql-scalars";Mock Data Import:
import { EmailAddressMock, URLMock, DateMock } from "graphql-scalars";For CommonJS:
const { GraphQLEmailAddress, resolvers, typeDefs } = require("graphql-scalars");import { GraphQLObjectType, GraphQLSchema, buildSchema } from "graphql";
import { GraphQLEmailAddress, GraphQLURL, resolvers, typeDefs } from "graphql-scalars";
// Method 1: Individual scalars in schema building
const UserType = new GraphQLObjectType({
name: "User",
fields: {
email: { type: GraphQLEmailAddress },
website: { type: GraphQLURL },
},
});
// Method 2: Using resolver collection
const schema = buildSchema(`
type User {
email: EmailAddress
website: URL
}
${typeDefs.join('\n')}
`);
// Apply resolvers
Object.assign(schema.getTypeMap(), resolvers);
// Method 3: Using individual type definitions
const schemaSDL = `
scalar EmailAddress
scalar URL
type User {
email: EmailAddress
website: URL
}
`;GraphQL Scalars is built around several key components:
Each scalar type implements the complete GraphQL scalar interface with three core methods:
serialize: Convert internal value to transport formatparseValue: Parse value from variablesparseLiteral: Parse value from AST literalsDate, time, and timestamp validation with ISO 8601 compliance and timezone support. Includes local time variants and duration types.
// Core date/time scalars
const GraphQLDate: GraphQLScalarType; // ISO 8601 date (YYYY-MM-DD)
const GraphQLTime: GraphQLScalarType; // ISO 8601 time (HH:MM:SS[.sss])
const GraphQLDateTime: GraphQLScalarType; // ISO 8601 date-time
const GraphQLDateTimeISO: GraphQLScalarType; // Strict ISO 8601 with timezone
const GraphQLTimestamp: GraphQLScalarType; // Unix timestamp
const GraphQLTimeZone: GraphQLScalarType; // IANA timezone identifiers
const GraphQLUtcOffset: GraphQLScalarType; // UTC offset (+/-HH:MM)
const GraphQLDuration: GraphQLScalarType; // Duration strings
const GraphQLISO8601Duration: GraphQLScalarType; // ISO 8601 duration formatNumeric validation including constrained integers, floating point numbers, and specialized numeric types like BigInt.
// Integer scalars
const GraphQLPositiveInt: GraphQLScalarType; // Positive integers (> 0)
const GraphQLNegativeInt: GraphQLScalarType; // Negative integers (< 0)
const GraphQLNonNegativeInt: GraphQLScalarType; // Non-negative integers (>= 0)
const GraphQLNonPositiveInt: GraphQLScalarType; // Non-positive integers (<= 0)
const GraphQLUnsignedInt: GraphQLScalarType; // Unsigned integers
const GraphQLSafeInt: GraphQLScalarType; // Safe JavaScript integer range
const GraphQLByte: GraphQLScalarType; // 8-bit unsigned integer (0-255)
// Floating point scalars
const GraphQLPositiveFloat: GraphQLScalarType; // Positive floats (> 0)
const GraphQLNegativeFloat: GraphQLScalarType; // Negative floats (< 0)
const GraphQLNonNegativeFloat: GraphQLScalarType; // Non-negative floats (>= 0)
const GraphQLNonPositiveFloat: GraphQLScalarType; // Non-positive floats (<= 0)
const GraphQLUnsignedFloat: GraphQLScalarType; // Unsigned floats
// Specialized numeric types
const GraphQLBigInt: GraphQLScalarType; // JavaScript BigInt values
const GraphQLLong: GraphQLScalarType; // 64-bit signed integerString validation for common text formats including emails, URLs, and text with constraints.
const GraphQLEmailAddress: GraphQLScalarType; // Valid email format
const GraphQLURL: GraphQLScalarType; // Valid URL format
const GraphQLNonEmptyString: GraphQLScalarType; // String with at least one character
const GraphQLPhoneNumber: GraphQLScalarType; // Phone number validationUnique identifier validation including UUIDs, ObjectIDs, JWTs, and other ID formats.
const GraphQLUUID: GraphQLScalarType; // UUID format validation
const GraphQLGUID: GraphQLScalarType; // GUID format (alias for UUID)
const GraphQLObjectID: GraphQLScalarType; // MongoDB ObjectID format
const GraphQLCuid: GraphQLScalarType; // Collision-resistant unique identifier
const GraphQLJWT: GraphQLScalarType; // JSON Web Token format
const GraphQLDID: GraphQLScalarType; // Decentralized Identifier formatNetwork-related validation including IP addresses, MAC addresses, and port numbers.
const GraphQLIP: GraphQLScalarType; // IP address (v4 or v6)
const GraphQLIPv4: GraphQLScalarType; // IPv4 address format
const GraphQLIPv6: GraphQLScalarType; // IPv6 address format
const GraphQLMAC: GraphQLScalarType; // MAC address format
const GraphQLPort: GraphQLScalarType; // Network port number (1-65535)Geographic coordinate validation and postal address components.
const GraphQLCountryCode: GraphQLScalarType; // ISO 3166-1 country codes
const GraphQLCountryName: GraphQLScalarType; // Country name validation
const GraphQLPostalCode: GraphQLScalarType; // Postal/ZIP code validation
const GraphQLLatitude: GraphQLScalarType; // Geographic latitude (-90 to 90)
const GraphQLLongitude: GraphQLScalarType; // Geographic longitude (-180 to 180)
const GraphQLLocale: GraphQLScalarType; // IETF BCP 47 locale identifiersColor format validation for various color representation systems.
const GraphQLRGB: GraphQLScalarType; // RGB color format
const GraphQLRGBA: GraphQLScalarType; // RGBA color format with alpha
const GraphQLHSL: GraphQLScalarType; // HSL color format
const GraphQLHSLA: GraphQLScalarType; // HSLA color format with alpha
const GraphQLHexColorCode: GraphQLScalarType; // Hexadecimal color code (#RRGGBB)
const GraphQLHexadecimal: GraphQLScalarType; // Hexadecimal string formatFinancial data validation including currencies, bank account numbers, and routing information.
const GraphQLCurrency: GraphQLScalarType; // ISO 4217 currency codes
const GraphQLUSCurrency: GraphQLScalarType; // US currency validation
const GraphQLIBAN: GraphQLScalarType; // International Bank Account Number
const GraphQLRoutingNumber: GraphQLScalarType; // US bank routing number
const GraphQLAccountNumber: GraphQLScalarType; // Bank account numberJSON and structured data format validation.
const GraphQLJSON: GraphQLScalarType; // JSON data validation
const GraphQLJSONObject: GraphQLScalarType; // JSON object validation
const GraphQLGeoJSON: GraphQLScalarType; // GeoJSON format validationSpecialized validation for books, classifications, versions, and other domain-specific formats.
// Library and classification
const GraphQLISBN: GraphQLScalarType; // International Standard Book Number
const GraphQLDeweyDecimal: GraphQLScalarType; // Dewey Decimal Classification
const GraphQLLCCSubclass: GraphQLScalarType; // Library of Congress Classification
const GraphQLIPCPatent: GraphQLScalarType; // International Patent Classification
// Version and identification
const GraphQLSemVer: GraphQLScalarType; // Semantic version format
const GraphQLSESSN: GraphQLScalarType; // Swedish Social Security Number
const GraphQLVoid: GraphQLScalarType; // Void/null typeCustom scalar creation utilities for advanced use cases.
class RegularExpression extends GraphQLScalarType {
constructor(name: string, regex: RegExp, options?: RegularExpressionOptions);
}
interface RegularExpressionOptions {
errorMessage?: RegularExpressionErrorMessageFn;
description?: string;
stringOnly?: boolean;
}
type RegularExpressionErrorMessageFn = (r: RegExp, v: any) => string;const resolvers: Record<string, GraphQLScalarType>;Object mapping scalar names to their resolver instances for easy schema integration.
const typeDefs: string[];Array of GraphQL scalar type definition strings for schema building.
const mocks: Record<string, () => any>;Collection of mock data generators for testing, with each scalar providing a corresponding mock function.