-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(docs): add example for union (#1148)
- Loading branch information
1 parent
e31a59c
commit 5ca93e2
Showing
74 changed files
with
2,567 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* This example shows how to work with GraphQL union types in the TypeScript interface. | ||
*/ | ||
|
||
import { Pokemon } from '../../tests/_/schemas/pokemon/graffle/__.js' | ||
import { show } from '../$/helpers.js' | ||
|
||
const pokemon = Pokemon.create() | ||
|
||
// dprint-ignore | ||
const battles = await pokemon.query.battles({ | ||
__typename: true, | ||
___on_BattleRoyale: { | ||
date: true, | ||
combatants: { | ||
trainer: { | ||
name: true, | ||
}, | ||
pokemons: { | ||
name: true, | ||
} | ||
}, | ||
winner: { | ||
name: true, | ||
}, | ||
}, | ||
___on_BattleTrainer: { | ||
date: true, | ||
combatant1: { | ||
trainer: { | ||
name: true, | ||
}, | ||
pokemon: { | ||
name: true, | ||
}, | ||
}, | ||
combatant2: { | ||
trainer: { | ||
name: true, | ||
}, | ||
pokemon: { | ||
name: true, | ||
}, | ||
}, | ||
winner: { | ||
name: true, | ||
}, | ||
}, | ||
___on_BattleWild: { | ||
date: true, | ||
trainer: { | ||
name: true, | ||
}, | ||
pokemon: { | ||
name: true, | ||
}, | ||
wildPokemons: { | ||
name: true, | ||
}, | ||
result: true, | ||
}, | ||
}) | ||
|
||
// The following contrived switch shows how the returned type is a discriminated union. | ||
// After checking the __typename, the type is known to be one of the possible battle types | ||
// and TypeScript narrows accordingly. | ||
|
||
const dateFormatter = new Intl.DateTimeFormat(`en-US`, { timeZone: `UTC` }) | ||
|
||
for (const battle of battles) { | ||
switch (battle.__typename) { | ||
case `BattleRoyale`: { | ||
// eslint-disable-next-line | ||
// @ts-ignore-error fixme | ||
const trainers = battle.combatants?.map(_ => _.trainer?.name) | ||
let info = `` | ||
info += `${battle.__typename} on ${dateFormatter.format(new Date(battle.date ?? 0))}\n` | ||
info += `combatants: ${trainers?.join(`, `) ?? `null`}\n` | ||
info += `winner: ${battle.winner?.name ?? `null`}` | ||
show(info) | ||
break | ||
} | ||
case `BattleTrainer`: { | ||
let info = `` | ||
info += `${battle.__typename} on ${dateFormatter.format(new Date(battle.date ?? 0))}\n` | ||
info += `${battle.combatant1?.trainer?.name ?? `null`} vs ${battle.combatant2?.trainer?.name ?? `null`}\n` | ||
info += `winner: ${battle.winner?.name ?? `null`}` | ||
show(info) | ||
break | ||
} | ||
case `BattleWild`: { | ||
let info = `` | ||
info += `${battle.__typename} on ${dateFormatter.format(new Date(battle.date ?? 0))}\n` | ||
info += `trainer: ${battle.trainer?.name ?? `null`} with ${battle.pokemon?.name ?? `null`}\n` | ||
info += `vs wild pokemons: ${battle.wildPokemons?.map(_ => _.name).join(`, `) ?? `null`}\n` | ||
info += `result: ${battle.result ?? `null`}` | ||
show(info) | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,14 @@ | |
"name": "Squirtle" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "Brock", | ||
"pokemon": [] | ||
}, | ||
{ | ||
"name": "Gary", | ||
"pokemon": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,12 @@ | |
}, | ||
{ | ||
"name": "Misty" | ||
}, | ||
{ | ||
"name": "Brock" | ||
}, | ||
{ | ||
"name": "Gary" | ||
} | ||
], | ||
"pokemons": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,11 @@ | |
}, | ||
{ | ||
"name": "Bulbasaur" | ||
}, | ||
{ | ||
"name": "Caterpie" | ||
}, | ||
{ | ||
"name": "Weedle" | ||
} | ||
] |
13 changes: 13 additions & 0 deletions
13
examples/__outputs__/55_generated/generated_union.output.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
---------------------------------------- SHOW ---------------------------------------- | ||
BattleWild on 1/1/2020 | ||
trainer: Ash with Pikachu | ||
vs wild pokemons: Squirtle, Bulbasaur | ||
result: pokemonsCaptured | ||
---------------------------------------- SHOW ---------------------------------------- | ||
BattleTrainer on 1/1/2003 | ||
Ash vs Misty | ||
winner: Misty | ||
---------------------------------------- SHOW ---------------------------------------- | ||
BattleRoyale on 1/13/1987 | ||
combatants: Ash, Misty | ||
winner: Ash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.