0
0
GraphQLquery~10 mins

Query variables in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Query variables
Define Query with Variables
Send Query + Variables
Server Receives Query
Server Substitutes Variables
Execute Query with Values
Return Result to Client
This flow shows how GraphQL queries use variables: define variables, send them with the query, server substitutes them, executes, and returns results.
Execution Sample
GraphQL
query GetUser($id: ID!) {
  user(id: $id) {
    name
    email
  }
}

variables: {"id": "123"}
This query fetches a user by ID using a variable $id, which is provided separately in the variables object.
Execution Table
StepActionQuery PartVariable StateServer ActionOutput
1Receive query and variablesquery GetUser($id: ID!) {...}{"id": "123"}Parse query and variablesReady to substitute variables
2Substitute variable $iduser(id: $id){"id": "123"}Replace $id with "123"user(id: "123")
3Execute queryuser(id: "123") { name, email }{"id": "123"}Fetch user with id 123{"name": "Alice", "email": "alice@example.com"}
4Return resultFull query result{"id": "123"}Send data back to client{"data": {"user": {"name": "Alice", "email": "alice@example.com"}}}
5EndN/AN/AQuery completeExecution stops
💡 Query completes after returning user data for id 123
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$idundefined"123""123""123"
Key Moments - 3 Insights
Why do we define variables separately from the query?
Variables are defined separately so the query can be reused with different values without rewriting the query text, as shown in execution_table step 1 and 2.
What happens if a variable is missing when the query runs?
If a required variable like $id is missing, the server cannot substitute it (see step 2), so the query will fail before execution.
How does the server know which value to use for $id?
The server uses the variables object sent alongside the query (step 1), matching variable names to values for substitution (step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what value is $id substituted with?
A"123"
B"user"
Cundefined
D"name"
💡 Hint
Check the 'Variable State' and 'Server Action' columns at step 2 in the execution_table.
At which step does the server fetch user data from the database?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Server Action' column describing 'Fetch user with id 123' in the execution_table.
If the variable $id was missing, what would happen at step 2?
AThe server substitutes $id with null and continues
BThe server fails to substitute $id and query execution stops
CThe server substitutes $id with an empty string
DThe server ignores $id and fetches all users
💡 Hint
Refer to key_moments about missing variables and execution_table step 2.
Concept Snapshot
GraphQL Query Variables:
- Define variables in query signature: query Name($var: Type!)
- Send variables separately as JSON
- Server substitutes variables before execution
- Allows query reuse with different inputs
- Missing required variables cause errors
Full Transcript
This visual execution shows how GraphQL query variables work. First, the client defines a query with variables like $id. Then it sends the query text plus a variables object with actual values. The server receives both, parses them, and substitutes the variables in the query with the provided values. After substitution, the server executes the query using those values, fetches data, and returns the result to the client. If a required variable is missing, the server cannot substitute it and the query fails. This process allows queries to be reused with different inputs easily.