Gql Document Node Typed
This example shows how to use the TypeScript type "TypedQueryDocumentNode" from the package graphql
to make a type safe request with gql method.
ts
import { Graffle } from 'graffle'
import { parse, type TypedQueryDocumentNode } from 'graphql'
const graffle = Graffle.create()
.transport({
url: `http://localhost:3000/graphql`,
})
type Document = TypedQueryDocumentNode<
{
pokemonByName: {
id: string
name: string
hp: number
attack: number
defense: number
trainer: { name: string }
}
},
{ name: string }
>
const document = parse(`
query ($name: String!) {
pokemonByName (name: $name) {
name
hp
attack
defense
trainer {
name
}
}
}
`) as Document
const data = await graffle.gql(document).send({ name: `Pikachu` })
console.log(data?.pokemonByName)
Outputs
txt
[
{
name: 'Pikachu',
hp: 35,
attack: 55,
defense: 40,
trainer: { name: 'Ash' }
}
]