Interface
This example shows how to work with interface types.
ts
import { Graffle } from './graffle/__.js'
const pokemon = Graffle.create()
const beings = await pokemon.query.beings({
__typename: true,
id: true,
name: true,
___on_Patron: {
money: true,
},
___on_Trainer: {
class: true,
},
___on_Pokemon: {
type: true,
},
})
// The following contrived switch console.logs how the returned type is a discriminated union.
// After checking the __typename, the type is known to be one of the three possible types
// and TypeScript narrows accordingly.
for (const being of beings) {
console.log(being.name)
switch (being.__typename) {
case `Patron`:
console.log(being.money)
break
case `Trainer`:
console.log(being.class)
break
case `Pokemon`:
console.log(being.type)
break
}
}
Outputs
txt
Sally
txt
1080000
txt
Dylan
txt
3530000
txt
Ash
txt
youth
txt
Misty
txt
teamRocketGrunt
txt
Brock
txt
youth
txt
Gary
txt
youth
txt
Pikachu
txt
electric
txt
Charizard
txt
fire
txt
Squirtle
txt
water
txt
Bulbasaur
txt
grass
txt
Caterpie
txt
bug
txt
Weedle
txt
bug