Skip to content
Graffle is a work in progress. Learn more.

Custom Scalar

This example shows how to add a client-side custom scalar codec to have arguments and data automatically encoded and decoded respectively.

ts
// Our website uses Vitepress+Twoslash. Twoslash does not discover the generated Graffle modules.
// Perhaps we can configure Twoslash to include them. Until we figure that out, we have to
// explicitly import them like this.
import './graffle/modules/global.js'
// ---cut---

import { Graffle } from './graffle/_.js'

const graffle = Graffle
  .create()
  .scalar(`Date`, {
    decode: (value: string) => new Date(value),
    encode: (value: Date) => value.toISOString(),
  })

const pokemons = await graffle.query.pokemons({
  $: { filter: { birthday: { lte: new Date(`1987-01-13`) } } },
  //                              ^^^^^^^^^^^^^^^^^^^^^^
  name: true,
  birthday: true,
})

console.log(pokemons?.[0]?.birthday instanceof Date)
console.log(pokemons)

Outputs

txt
true
txt
[
  { name: 'Pikachu', birthday: 1850-01-01T00:00:00.000Z },
  { name: 'Squirtle', birthday: 1910-01-01T00:00:00.000Z }
]

Released under the MIT License.