Master advanced GraphQL features including fragments, variables, directives, and complex selections!
Fragments help you reuse common field selections:
fragment CoreStats on Stats {
hp
attack
defense
specialAttack
specialDefense
speed
}
fragment PokemonSummary on Pokemon {
id
name
types
stats {
...CoreStats
}
}
query ComparePokemon {
pikachu: pokemon(id: "025") {
...PokemonSummary
abilities {
name
}
}
raichu: pokemon(id: "026") {
...PokemonSummary
abilities {
name
}
}
}
Make your queries reusable with variables:
query GetPokemonDetails(
$id: ID!
$includeStats: Boolean = true
$includeAbilities: Boolean = false
) {
pokemon(id: $id) {
id
name
types
stats @include(if: $includeStats) {
hp
attack
defense
}
abilities @include(if: $includeAbilities) {
name
description
isHidden
}
}
}
Handle different item types:
query GetItem($itemId: ID!) {
item(id: $itemId) {
__typename
id
name
description
... on HealingItem {
hpRestore
curesStatus
}
... on Pokeball {
catchRate
specialEffect
}
... on Berry {
effect
power
flavors {
spicy
sweet
bitter
}
}
}
}
Fragments can reference other fragments:
fragment MoveDetails on Move {
name
type
power
accuracy
pp
}
fragment PokemonWithMoves on Pokemon {
name
types
moves {
...MoveDetails
}
}
query TeamMovesets {
trainer(id: "ash-ketchum") {
name
pokemon {
nickname
...PokemonWithMoves
}
}
}
Fetch multiple variations of the same field:
query PokemonByType {
fireTypes: pokemonByType(type: FIRE) {
name
types
}
waterTypes: pokemonByType(type: WATER) {
name
types
}
shinyPokemon {
name
types
isShiny
}
braveNature: pokemonByNature(nature: BRAVE) {
name
nature
stats {
attack
speed
}
}
}
Use fragments for complex type hierarchies:
# Base fragments for reuse
fragment BaseStats on Stats {
hp
attack
defense
specialAttack
specialDefense
speed
total
}
fragment TypeInfo on Pokemon {
types
}
# Composed fragments
fragment CompletePokemon on Pokemon {
id
name
...TypeInfo
stats {
...BaseStats
}
abilities {
name
isHidden
}
}
query ComplexTeamAnalysis($trainerId: ID!) {
trainer(id: $trainerId) {
name
pokemon {
nickname
...CompletePokemon
moves {
name
type
power
accuracy
}
}
}
}
Query paginated results with GraphQL connections:
query PaginatedPokemon($limit: Int = 10, $offset: Int = 0) {
allPokemon(limit: $limit, offset: $offset) {
edges {
node {
id
name
types
stats {
total
}
}
}
pageInfo {
hasNextPage
hasPreviousPage
}
}
}