scalarType

GraphQL Docs for Scalar Types

Nexus allows you to provide an asNexusMethod property which will make the scalar available as a builtin on the definition block object. We automatically generate and merge the types so you get type-safety just like the scalar types specified in the spec:

1const DateScalar = scalarType({
2 name: 'Date',
3 asNexusMethod: 'date',
4 description: 'Date custom scalar type',
5 parseValue(value) {
6 return new Date(value)
7 },
8 serialize(value) {
9 return value.getTime()
10 },
11 parseLiteral(ast) {
12 if (ast.kind === Kind.INT) {
13 return new Date(ast.value)
14 }
15 return null
16 },
17})

Example of Upload scalar

1import { GraphQLUpload } from 'graphql-upload'
2
3export const Upload = GraphQLUpload

Example of DateTime scalar

1import { GraphQLDate } from 'graphql-iso-date'
2
3export const DateTime = GraphQLDate

Exposing scalar as method

If you have an existing GraphQL scalar and you'd like to expose it as a method on the builder, call asNexusMethod:

1import { GraphQLDate } from 'graphql-iso-date'
2
3export const GQLDate = asNexusMethod(GraphQLDate, 'date')
1const SomeObject = objectType({
2 name: 'SomeObject',
3 definition(t) {
4 t.date('createdAt') // t.date() is now available (with types!) because of `asNexusMethod`
5 },
6})

Check the type-definitions or the examples for a full illustration of the various options for scalarType, or feel free to open a PR on the docs to help document!

Edit this page on Github