What are GraphQL variables and how do they differ from arguments?
Learn what GraphQL variables are, how they differ from field arguments, and why declaring typed $variables keeps queries static, reusable and injection-safe.
Expected Interview Answer
GraphQL arguments are the named inputs a field declares and accepts (like id or first), while variables are typed placeholders declared on the operation and passed in a separate variables map, so the query string stays static and the dynamic values are supplied at execution time.
You declare variables in the operation signature with a $name and a type (e.g. query GetUser($id: ID!)), then reference $id wherever an argument value is needed. The actual values travel in a separate JSON variables object, not baked into the query text. Arguments are the field-level parameters themselves; variables are one way to feed those arguments dynamically. This separation enables query reuse, server-side caching of the static document, type validation of inputs, and safety from string-interpolation injection.
- Keeps the query document static and reusable
- Type-checks inputs against declared variable types
- Avoids unsafe string interpolation of user values
- Enables caching and persisted queries
- Cleanly separates the query shape from its data
AI Mentor Explanation
An argument is like the slot on a team sheet that says 'batting position' — the field expects a value there. A variable is the reusable strategy card you fill in before each match: the plan is written once with a blank for who bats at number three, and you slot in the player's name per game without rewriting the whole batting order.
Step-by-Step Explanation
Step 1
Recognize the argument
A field declares named arguments in the schema, e.g. user(id: ID!): User — id is the argument.
Step 2
Declare a variable
Add a typed placeholder to the operation signature: query GetUser($id: ID!).
Step 3
Reference it
Use $id as the value of the field argument: user(id: $id).
Step 4
Supply values separately
Send the actual values in a JSON variables object: { "id": "42" }.
Step 5
Let the server validate
GraphQL type-checks each variable against its declared type before executing, rejecting mismatches.
What Interviewer Expects
- Clear distinction: arguments are field inputs, variables feed them dynamically
- Correct variable syntax with $name and a type
- Knowledge that variables travel in a separate variables map
- Understanding of type validation and required (!) variables
- Awareness of reuse, caching and injection-safety benefits
Common Mistakes
- Confusing variables with arguments as the same thing
- Interpolating user values directly into the query string
- Forgetting to declare the variable's type in the operation signature
- Marking a variable non-null but not providing it
- Thinking variables change the query shape rather than just its inputs
Best Answer (HR Friendly)
“Arguments are the inputs a GraphQL field asks for, like an ID or a search term. Variables are a clean way to supply those inputs from outside the query: you write the query once with named blanks and then pass the real values in a separate list, so the same query can be reused safely with different data.”
Code Example
# Operation declares typed variables; fields use them as argument values
query GetUser($id: ID!, $withPosts: Boolean = false) {
user(id: $id) {
name
posts @include(if: $withPosts) {
title
}
}
}
# Values are sent separately, keeping the query document static:
# variables JSON:
# { "id": "42", "withPosts": true }Follow-up Questions
- How do you give a GraphQL variable a default value?
- Why is passing values via variables safer than string interpolation?
- How do variables interact with @include and @skip directives?
- What happens if a required (!) variable is omitted?
- How do persisted queries rely on static query documents?
MCQ Practice
1. Where are the actual values for GraphQL variables sent?
Variable values travel in a separate variables JSON object, keeping the query document static and reusable.
2. How is a variable declared in an operation?
Variables are declared as $name: Type in the operation signature, e.g. query GetUser($id: ID!).
3. What is the relationship between arguments and variables?
Arguments are the field inputs; variables are typed placeholders that feed those arguments at execution time.
Flash Cards
How do you declare a GraphQL variable? — In the operation signature as $name: Type, e.g. query GetUser($id: ID!).
Where do variable values go? — In a separate variables JSON map, not inside the query string.
Argument vs variable? — An argument is a field's declared input; a variable is a typed placeholder that supplies that input dynamically.
Why prefer variables over interpolation? — They keep queries static, enable caching, get type-checked, and avoid injection risks.