Advanced Queries

Master advanced GraphQL features including fragments, variables, directives, and complex selections!

Using Fragments

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
    }
  }
}
Loading tree-sitter...

Query Variables

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
    }
  }
}
Loading tree-sitter...

Inline Fragments with Interfaces

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
      }
    }
  }
}
Loading tree-sitter...

Nested Fragments

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
    }
  }
}
Loading tree-sitter...

Aliases and Field Arguments

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
    }
  }
}
Loading tree-sitter...

Advanced Fragment Patterns

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
      }
    }
  }
}
Loading tree-sitter...

Connection Pagination

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
    }
  }
}
Loading tree-sitter...