Scalars
Adding Custom GraphQL Scalars
To add a custom scalar that has been implemented as GraphQLScalar from graphql-js you need to provide some type information in SchemaTypes generic parameter of the builder:
The Input type is the type that will be used when the type is used in an argument or InputObject
.
The Output type is used to validate the resolvers return the correct value when using the scalar in
their return type.
For many scalars Input
and Outout
will be the same, but they do not always need to match. The
Scalars generic can be used to change types for the built-in scalars.
For example, the defaults for the ID scalar might not be exactly what you want, you can customize the values like so:
Adding Scalars from graphql-scalars
Similarly to adding your own custom scalars, you can utilize scalars from the graphql-scalars library by also providing the types through the SchemaTypes generic parameter.
Note that when implementing the graphql-scalars library, the best types to use for Input
and
Output
types are not always intuitive. For example, you might assume that the JSON
type from
graphql-scalars would utilize the global JSON
type, or another JSON type imported from a library
that tries to enumerate potential JSON values, but it is usually better to just use unknown
. A
good place to start if you are unsure what type to use it the check the codegenScalarType
inside
file where the scalar is defined by graphql-scalars
(BigInt scalar definition, for reference).
This isn't defined for all scalars, and some scalars use any
in which case unknown
might be a
better option.