04 / 05

How to pass variables to query?

Difficulty: 4/10
variable syntax, client‑server payload, validation & scaling

A GraphQL operation can be parameterized with variables, maximizing reuse, and avoiding costly string building in clients at runtime.

Operation Name:
  1. 1

    Declare $variableName as one of the variables accepted by the query

  2. 2

    Use the $variableName in the query

  3. 3

    Pass variableName: value in the separate, transport-specific (usually JSON) variables dictionary

  4. 4

    Values for those variables are provided to a GraphQL service as part of a request so they may be substituted in during execution.

javascript
Default values can also be assigned to the variables in the query by adding the default value after the type declaration:

Scenario Questions

0-2 years experience

  1. 1You need to fetch a list of books filtered by author name using GraphQL. How would you structure the query and pass the author name as a variable from the client?
  2. 2If you forget to include a variable definition in the GraphQL query but still send a variable in the request payload, what error would the server return and why?

2-5 years experience

  1. 1We have a UI component that lets users search for products with optional price range filters. How would you design the GraphQL query and variable handling to support optional filters without sending undefined variables?
  2. 2During a code review you notice that a query is constructing its variable object by spreading a large state object, causing extra fields to be sent. How would you debug the issue and what changes would you recommend?

5-8 years experience

  1. 1Our service receives thousands of concurrent GraphQL requests, many of which include large JSON objects as variables. What strategies would you employ to mitigate performance or security risks associated with variable payload size?
  2. 2We need to introduce server‑side validation for variables to enforce business rules before resolver execution. How would you integrate this into the GraphQL execution pipeline without breaking existing clients?

8+ years experience

  1. 1Across multiple microservices we share a common GraphQL gateway. How would you standardize variable naming, typing, and versioning to avoid breaking changes when teams evolve their schemas?
  2. 2Legacy clients still embed variables directly in the query string instead of using the separate variables map. What migration plan would you propose to move them to the modern approach while maintaining backward compatibility?

Follow-up Questions

  • Can you walk me through the exact shape of the request payload you’d send?
  • What would you monitor to ensure your mitigation strategy is effective?
  • How would you handle backward compatibility for clients that can’t be updated immediately?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.