$ show graphql-hive/graphql-scalars
This repository is a comprehensive library of custom GraphQL scalar types, intended to supplement the base GraphQL specification with precise, type-safe scalars for common data formats (e.g., UUID, DateTime, EmailAddress, PhoneNumber, IBAN, GeoJSON, ISBN, etc.). It exports over 80 scalar definitions as `GraphQLScalarType` instances, along with corresponding SDL type definitions, mock generators, and JSON Schema extensions. The architecture is clean and uniform: each scalar is implemented as a self-contained module with `serialize`, `parseValue`, and `parseLiteral` methods, leveraging a shared validation pattern via `createGraphQLError`. The library integrates seamlessly with GraphQL codegen via `extensions.codegenScalarType` and provides a `RegularExpression` factory for creating custom regex-based scalars. Production usage is as a dependency for GraphQL servers (Apollo, Yoga, etc.) where precise input validation and output formatting are required. The codebase is well-tested with per-scalar unit tests, integration tests for date/time scalars, and bundle-size checks. While the majority of scalars are simple regex or range validations, several modules (GeoJSON, IBAN, iso-date) demonstrate non-trivial validation logic and are safely reusable. The repository also includes a Next.js documentation website and automated release tooling via Changesets.
The LLM identified 4 files worth highlighting for reuse:
A factory class for generating custom `GraphQLScalarType` instances based on a regular expression. It accepts a regex, a description, and optional configuration (e.g., `stringOnly` to reject AST inputs that are strings). This is a self-contained, highly reusable component that allows consumers to define their own regex-validated scalars without duplicating the scalar boilerplate.
A coherent module implementing a GeoJSON scalar for geometry, feature, feature collection, and combination types. It includes TypeScript type definitions, a JSON Schema generator, validation functions for coordinates and geometry types, and a custom `GraphQLScalarType`. The `validators.ts` file provides reusable coordinate validation (bounding checks, nesting depth, linear ring closure) that could be extracted and applied to other geographic data handling projects.
A comprehensive implementation of RFC 3339 date, time, date-time, and duration scalars. It includes a `formatter.ts` for parsing and formatting ISO 8601 strings, and a `validator.ts` with regex validation and bounds checking. The modular design separates date/time parsing logic from GraphQL scalar binding, making the validator and formatter reusable in non-GraphQL contexts (e.g., backend API validation libraries).
An IBAN validator implementing ISO 13616 with country-specific length and structure validation, including a MOD-97 checksum algorithm. The file contains a map of country codes to valid IBAN lengths and format patterns, making it a self-contained reference implementation for IBAN validation that could be extracted into a standalone utility library for financial applications.