Skip to content

Queries in GraphQL — Complete Guide

DodaTech Updated 2026-06-28 3 min read

In this tutorial, you will learn about Queries in Graphql. We cover key concepts, practical examples, and best practices to help you master this topic.

GraphQL queries read data from the server. Clients specify the exact fields they need, optionally with arguments to filter or transform results. The server returns a JSON response matching the query shape exactly.

What You'll Learn

  • Writing queries with fields, arguments, aliases, and fragments
  • Using variables to parameterize queries
  • Inline fragments for conditional field selection
  • Query execution flow and resolver mapping

Why It Matters

Queries are the primary way clients interact with a GraphQL API. Understanding query syntax and capabilities lets frontend developers fetch exactly what they need without backend changes.

Real-World Use

GitHub's GraphQL API uses queries with arguments for pagination (first, after), filtering (states, labels), and search (query string). Shopify's Storefront API uses fragments extensively to reuse field selections across product queries.

flowchart LR
    Client[Client] -->|Query| Server[GraphQL Server]
    Server --> Parse[Parse Query]
    Parse --> Validate[Validate Schema]
    Validate --> Execute[Execute Resolvers]
    Execute -->|JSON| Client
    Execute --> DB[(Database)]

Teacher Mindset

Every query starts at the root Query type. The resolver for each field either returns data directly or delegates to a sub-resolver. The response mirrors the query structure, so client and server always agree on the data shape.

Code Examples

# Example 1: Simple query with argument
query GetBook($id: ID!) {
  book(id: $id) {
    title
    author {
      name
    }
    price
  }
}
# Example 2: Query with aliases and fragments
query GetComparison {
  cheap: products(priceLte: 10) {
    ...ProductFields
  }
  expensive: products(priceGte: 100) {
    ...ProductFields
  }
}

fragment ProductFields on Product {
  id
  name
  price
  inStock
}
# Example 3: Query with inline fragment on interface
query GetMedia {
  media {
    title
    ... on Book {
      isbn
      pageCount
    }
    ... on Video {
      duration
      resolution
    }
  }
}

Common Mistakes

  • Hardcoding values instead of using variables for dynamic queries
  • Overusing inline fragments when a concrete type field suffices
  • Forgetting to alias fields when querying the same field with different arguments
  • Requesting the same field in multiple locations without using fragments
  • Not using the operation keyword (query) for clarity

Practice

  1. Write a query that fetches a user by ID with name and email.
  2. Use aliases to fetch two different products by different IDs in one query.
  3. Create a fragment for user fields and reuse it in two different queries.
  4. Write a query with variables for filtering products by price range.
  5. Challenge: Combine aliases, fragments, and variables in a single query that fetches paginated results.

FAQ

What is the difference between a query and a mutation?

Queries are read-only operations that fetch data. Mutations modify data on the server. GraphQL guarantees queries can run in parallel but mutations execute sequentially.

Do I always need the query keyword?

No. For a query operation, the query keyword is optional. However, using it improves readability and required for named queries.

Can I pass variables to fragments?

Fragments cannot receive arguments directly. Use fragments to reuse field selections and pass arguments at the query level.

How does GraphQL handle null in responses?

If a nullable field resolves to null, it appears as null in the response. If a non-null field resolves to null, the error propagates to the parent field.

What happens if I query a field that does not exist?

The server returns a validation error before executing the query. GraphQL's type system catches unknown fields at parse time.

Mini Project

Write five different queries against your library schema: fetch a book by ID with author, list all books with authors, find books by genre using a variable, alias two author queries, and use a fragment for common book fields.

What's Next

Next, you will learn about mutations for creating, updating, and deleting data in GraphQL.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro