GraphQL Scalars 1.0 is out!

Explore our services and get in touch.
The GraphQL Specification has theInt
, Float
, String
, Boolean
and ID
Scalar types by
default. Those scalar types help you identify the data and validate it before transferring it
between client and server. But you might need more specific scalars for your GraphQL application, to
help you better describe and validate your app's data.
Validation Using Scalars
For example, you have a String
field, but you need to validate upcoming or ongoing string data
using regular expressions. So you should have this validation on each end; one in the client, the
other one in the server and maybe there is another on a source. Instead of duplicating the same
logic in different parts of the project, you can use EmailAddress
scalar type that does the
validation inside GraphQL for you.
Serialization and Parsing
The other benefit of using GraphQL scalar types is parsing and serializing while transferring data.
For example, you have DateTime
data, but it is transferred as String
due to restrictions of
JSON, and each time you receive and pass the data, you have to parse the string and create a
JavaScript Date
instance while also serializing it to string before passing it to the client.
Instead of having that logic in your implementation, you can just use DateTime
scalar and you
would work with native JavaScriptDate
instances directly like it is one of primitive types such as
string, number and boolean.
What's New?
We've recently taken over the maintenance of GraphQL-Scalars (opens in a new tab) library from the amazing team of OK-Grow!
Since then, we completely rewrote the library using TypeScript, upgraded all dependencies, closed
all the issues and PRs and increased the number of scalars in the package with new scalars like:
BigInt(Long)
, GUID
, HexColorCode
, Hexadecimal
, IPv4
, IPv6
, ISBN
, MAC
, JSON
and more. You can see all scalars in the
README (opens in a new tab).
Mocking
Apollo Server provides mocks built-in scalars such as Int
, String
, Float
, ID
and
Boolean
. What if you need same thing for our scalars? So, we provide you mocking functions for
each scalar in this package. You can add those easily in your server for mocking the schema.
import { ApolloServer } from 'apollo-server'
import { makeExecutableSchema } from 'graphql-tools'
// import all scalars and resolvers
import { typeDefs, resolvers, mocks } from 'graphql-scalars'
// Alternatively, import individual scalars and resolvers
// import { DateTimeResolver, DateTimeTypeDefinition, DateTimeMock, ... } from "graphql-scalars"
const server = new ApolloServer({
typeDefs: [
// use spread syntax to add scalar definitions to your schema
...typeDefs
// DateTimeDefinition,
// ...
// ... other type definitions ...
],
resolvers: {
// use spread syntax to add scalar resolvers to your resolver map
...resolvers
// DateTimeResolver,
// ...
// ... remainder of resolver map ...
},
mocks: {
// use spread syntax to add scalar resolvers to your resolver map
...mocks
// DateTimeMock,
// ...
// ... other mocks ...
}
})
Special Thanks
Thanks to OK-Grow for creating this package,
adriano-di-giovanni (opens in a new tab) for being generous and giving us the
graphql-scalars
(opens in a new tab) name on npm, to
Saeris (opens in a new tab) for letting us take other scalar implementations from his fork,
stems (opens in a new tab) for their
graphql-bigint
(opens in a new tab) package,
abhiaiyer91 (opens in a new tab) for his
graphql-currency-scalars
(opens in a new tab) package and
taion (opens in a new tab) for his
graphql-type-json
(opens in a new tab).
Join our newsletter
Want to hear from us when there's something new? Sign up and stay up to date!
By subscribing, you agree with Beehiiv’s Terms of Service and Privacy Policy.
Recent issues of our newsletterSimilar articles

Scalable APIs with GraphQL Server Codegen Preset
Structuring GraphQL server the right way enables many teams to work in harmony while minimising runtime risks.

GraphQL over Internet
HTTP, WebSockets, Server-Sent Events, undergoing standardization and libraries.

Using @defer Directive with GraphQL Code Generator
Learn how to boost GraphQL performance using the @defer directive and GraphQL Code Generator for deferred fragment field resolution.

Introducing Schema Policy in Hive
New GraphQL-Hive feature for enfocring best-practices and schema-design styles.