GraphQL is a query language for API (Application Programming Interface). It was developed by Facebook in 2012 after they released it publicly in 2015. Graphql Provides a more efficient, powerful, and flexible alternative to traditional REST APIs. In this article, we will learn about variables in GraphQL.
What are Variables in Graphql?
In Graphql, Variables are used to parametrize queries and mutations. They allow developers to parameterize the queries and mutations to enable more dynamic content and reusable queries. Instead of adding hardcode values into the queries, variables provide a way to inject dynamic values at runtime.
Features of GraphQL Variables
- Dynamic Querying: GraphQL variables enable the creation of dynamic queries where values can be provided at runtime. This dynamic nature allows for a wide range of use cases, such as filtering, sorting, or specifying criteria based on user input.
- Multiple Variables in a Single Query: GraphQL queries can include multiple variables, each serving a specific purpose within the query. This capability allows for complex and parameterized queries without sacrificing readability.
- Variable Default Values: In GraphQL can have default values, providing a fallback in case a value is not explicitly provided during query execution. This feature enhances the flexibility of queries while maintaining a level of predictability.
Syntax of Variables in GrabhpQL
To use a variable in a GraphQL query, you must follow a specific syntax. Let's break down the key components. Follow the following steps.
- Variables are declared within the parentheses of a query or mutation.
- In GrahphQL, each varaible starts with a dollar sign ($) followed by the variablle name and its type. There is the example of this variable.
query GetPerson($id: ID!) {
person(id: $id) {
name
age
address
}
}
In this query example, we can see '$id' is a variable of type ID. This query fetches information about a person with specific id, actual value provided in the variable in the execution time.
Declaring Variables in GraphQL
To declare a variable in Graphql. Variables can be defined withing parentheses and are prefixed with a dollar sign ("$"). We can also specify Graphql type, ensuring a clear definition of the expected data.
Syntax:
query MyQuery($variableName: VariableType) {
// Your query or mutation goes here, using $variableName where needed
}
Syntax breakdown here:
- $variableName: This is the declaration of the variable. Variable must be starts with the '$' symbol.
- Variable Type: This is the GraphQL type of the variable. It could be any built type of Graphpql like (String,Int, Boolean,Float) or a custom type defined in the your Graphql.
- field(argument:$variableName): It is the example of the declared variable in the query. It shows how we can use this variable in the query.
Providing Variables Values
Providing variables values in crucial aspect of making queries and mutation dynamic and adaptable in GraphQL. We can pass the values in the graphql query using the Graphql Query Variable Field.
query GetPerson($id: ID!) {
person(id: $id) {
name
age
address
}
}
Values:
{
"id" :7
}
Output:

Declaring Multiple Variables
In GraphQL, you can declare mutliple variables in a query or mutation by including them in the parenteses following the query or mutation keyword. Each variable is declared with a name, a colon to indicate its type, and optionally an equals sign and a default value.
Query:
query MyComplexQuery($var1: String!, $var2: Int!, $var3: Boolean) {
firstData: getData(id: $var1) {
id
name
}
secondData: getMoreData(limit: $var2) {
id
description
}
optionalData: getOptionalData(flag: $var3) {
id
value
}
}
Variable Value:
{
"var1": "abc123",
"var2": 5,
"var3": true
}
Output:

Define a Default Variable
In GraphQL, you can define default values for variables directly within the variable declaration. Default values are useful when you want to provide a fallback or default behaviour if a value is not explicity provided during the query execution.
To assign a default value to a variable in the query, add it after the type declaration, as follows:
query($id:ID=5){
person(id:$id){
name
address
}
}
In this example query
- 'id' is the variable declared within the parentheses of the query with its default value which is '5'.
- 'ID' is the type of the variable.
Output:

Disadvantages of GraphqlQL Variable
GraphQL variables had numerious advantages. There is also some disadvantages of graphql. Let's explore the sume disadvantage of GraphQL.
- Complexity in Simple Case: In case where a query is realatively simple and doesn't require dynamic values, intorducing variables might add unnecessasry complexity in GraphQL Variable.
- Overhead in Small Projects: In small projects or applicatinos where the benefits of dynamic queries and code resue are not as pronounced, using variables might introduced necessary overhead. Simplicity could be sacrificed for little gain.
- Increased Payload Size: GraphQL allows for precise data fetching, in scenarios wher only a subset of fields is needed, using variables might still result in fetching unnecessary data. This could lead to larger payload sizes than necessary.
Conclusion
GraphQL variables are a fundamental feature that enhances the flexibility and reusability of queries. Variables are very useful in Graphql. It create dynamic query and fetch the dynamic data. They provide a powerful tool for crafting queries and mutations that can cater to diverse scenarios, making GraphQL a truly flexible and robust API querying language. Embrace the versatility of variables in GraphQL to unlock a new level of dynamism and efficency in your data-fetching operations.
Similar Reads
Resolvers in GraphQL
Resolvers are a crucial part of GraphQL that determines how data is fetched and returned in response to a query. They act as the bridge between the client's request and the data source, whether it's a database, API, or any other data store. Resolvers are responsible for fetching the data for each fi
5 min read
Types in GraphQL
GraphQL is a strongly typed query language used as a manipulative language for various APIs. It is also called a query language for APIs. GraphQL also helps to describe our data. GraphQL services can run in any language In this, Types are the fundamental concepts that define various data types prese
4 min read
Mutations in GraphQL
GraphQL is a query language for REST APIs created by Facebook which is designed to provide more efficiency, flexibility, and superiority in data handling. While GraphQL queries are used to retrieve information, mutations are used to perform data operations on the server, similar to HTTP Commands lik
6 min read
Pagination in GraphQL
Pagination in GraphQL is a powerful tool that allows developers to efficiently retrieve large amounts of data by breaking it into smaller, more manageable parts. This not only enhances the performance of data queries but also reduces the burden on servers and networks. In this article, We will learn
6 min read
GraphQL Tutorial
In today's fast-paced digital world, efficient data fetching is more important than ever. That's where GraphQL comes in. Developed by Facebook, GraphQL is a powerful query language for APIs that allows you to request exactly the data you need, no more and no less. Unlike traditional REST APIs, which
4 min read
What is GraphQL Queries
GraphQL is a powerful open-source Query Language for APIs. It is most commonly known for its single endpoint query which allows the user to define a single endpoint to fetch all the information needed. Queries in GraphQL allow us to retrieve the data from an API endpoint, and the data is what we spe
4 min read
Schema in GraphQL
GraphQL is a powerful open-source Query Language for APIs. It is most commonly known for its single endpoint query which allows the user to define a single endpoint to fetch all the information needed. Schemas in GraphQL are the blueprints for its APIs and define what the request-response structure
5 min read
Installation in GraphQL
GraphQL is a powerful query language that enables clients to request the data they need from an API. Setting up a GraphQL server involves installing the necessary dependencies and configuring the server to handle GraphQL queries. In this article, we will go through the steps to install and set up a
3 min read
Type Language in GraphQL
Type language in GraphQL is the fundamental component that distinguishes GraphQL from other query languages. It plays an important role in defining the structure, shape, and behavior of the data that can be queried from a GraphQL API. In this article, we will learn about the concept of the type lang
5 min read
Scalar Types in GraphQL Schema
GraphQL is a powerful open-source Query Language for APIs. In 2012 it was first developed by a team of developers in Facebook and then it was later made public to the general people by making it open-source. Now it is being maintained by the GraphQL community. GraphQL is most commonly known for its
5 min read