Skip to main content
Glossary

GraphQL Query

A request written in GraphQL syntax that specifies exactly what data a client needs from a GraphQL API, including the fields, relationships, and filters.

Detailed Explanation

A GraphQL query describes the shape of the data you want. The server returns a response matching that exact shape. Queries can include arguments for filtering and pagination, fragments for reusable field selections, and variables for dynamic values.

GraphQL queries support nested data fetching: you can request a user, their posts, the comments on each post, and the author of each comment—all in a single query. This eliminates the N+1 request problem common with REST APIs. Queries are typed, so the server can validate them before execution.

Why It Matters

GraphQL queries give clients precise control over data fetching, reducing over-fetching and under-fetching. They are fundamental to working with GraphQL APIs.

Real-World Example

A mobile app queries: `{ user(id: "123") { name email posts(limit: 5) { title comments { text author { name } } } } }`—getting all needed data in one request.

When to Use

When building GraphQL clients, optimizing data fetching, or working with APIs that support GraphQL.

Advantages

  • Fetch exactly the data you need
  • Single request for nested data
  • Strongly typed for validation
  • Self-documenting through schema
  • Supports variables for dynamic queries

Disadvantages

  • Complex queries can be expensive for servers
  • Learning curve for query syntax
  • Caching is harder than REST
  • File uploads require additional handling
  • Requires careful query complexity analysis

Frequently Asked Questions

What is the difference between a query and a mutation?

Queries read data. Mutations write data. Both use similar syntax, but mutations have side effects. Queries are cacheable; mutations typically are not.

How do I handle pagination in GraphQL?

Common approaches: cursor-based (Relay Connection specification, most recommended), offset-based (simpler but less efficient), and page-based. Cursor-based pagination works best for infinite scroll and real-time data.

What is a GraphQL fragment?

A fragment is a reusable set of fields that can be included in queries. They reduce duplication and make queries more maintainable. Example: `fragment UserFields on User { name email }` can be reused across multiple queries.

How do I pass arguments in a GraphQL query?

Use parentheses after the field name: `users(first: 10, after: "cursor")`. Arguments can be passed as literals or as variables: `query GetUser($id: ID!) { user(id: $id) { name } }`.

What is query complexity analysis?

Query complexity analysis assigns costs to fields and calculates the total cost of a query before executing it. It prevents expensive queries from overwhelming your server. Tools: graphql-query-complexity, Ali.

Back to Glossary

Browse all terms in our software development glossary.

Browse All Terms