← Back to all tutorials

Query Basics

Learn the fundamentals of writing GraphQL queries — fields, nested queries, and arguments.

GraphQL Query Basics

Queries are the bread and butter of GraphQL. They define what data you want to fetch. In this episode, we'll explore the syntax and structure of GraphQL queries.

Your First Query

The simplest GraphQL query looks like this:

{
  games {
    id
    title
    platform
  }
}

This asks the server: "Give me all games, and for each game, return the id, title, and platform." The response will mirror this exact structure:

{
  "data": {
    "games": [
      { "id": "1", "title": "Zelda", "platform": ["Switch"] },
      { "id": "2", "title": "Mario Kart", "platform": ["Switch"] }
    ]
  }
}

Fields

Fields are the core building blocks of a query. You select which fields you want returned:

{
  reviews {
    rating
    content
  }
}

If you don't request a field, it won't be returned. This is the key advantage over REST — no over-fetching.

Nested Queries

GraphQL excels at fetching related data in a single query. If reviews have an associated game, you can nest the query:

{
  reviews {
    rating
    content
    game {
      title
      platform
    }
  }
}

This returns each review along with its associated game data — in a single request!

Query with Arguments

You can pass arguments to filter or find specific data:

{
  game(id: "1") {
    title
    platform
    reviews {
      rating
      content
    }
  }
}

Arguments can be used for:

  • Filteringgames(platform: "Switch")
  • Selecting by IDgame(id: "1")
  • Paginationgames(limit: 10, offset: 0)
  • Sortinggames(sort: "title")

Named Queries

You can name your queries for better readability and debugging:

query GetAllGames {
  games {
    id
    title
    platform
  }
}

query GetSingleGame {
  game(id: "1") {
    title
    reviews {
      rating
    }
  }
}

Named queries help you organize complex operations and show up in developer tools.

Aliases

If you need to query the same field with different arguments, use aliases:

{
  zelda: game(id: "1") {
    title
    platform
  }
  mario: game(id: "2") {
    title
    platform
  }
}

Key Takeaways

  • Queries define what data to fetch and its shape
  • Only requested fields are returned (no over-fetching)
  • Nested queries let you fetch related data in one request
  • Arguments let you filter and find specific data
  • Named queries and aliases improve organization

Next up: we'll build our own GraphQL server with Apollo!