GraphQL, a query language, is an API standard for data query and manipulation. GraphQL queries enable declarative data fetching and expose only a single endpoint that you can use to get data.
This guide is a continuation of our interactive series on GraphQL. In the previous one, we learned about GraphQL queries, fields, and arguments. This guide will cover the use of aliases and variables in GraphQL queries.
To understand how GraphQl works, think of it as a graph. When requested, it exposes a single "edge" of the graph, which is an endpoint.
As a result, you can pass arguments to related fields or query the same object with multiple arguments in one API call.
What if you wanted to query the same field with different arguments? The initial thought will be to do something like this:
graphql
{user(id: 2) {nameusername}user(id: 4) {nameusername}}
However, this query will return an error because the query fields and response fields match. Hence there will be a conflict when deciding which one of the two arguments applies to a given field.
Aliases are used to avoid this error. You can query the same field with different arguments by using aliases. They allow you to change the name of the resulting field to prevent conflicts.
Let's change the above query using aliases:
graphql
{firstUser: user(id: 2) {nameusername}secondUser: user(id: 4) {nameusername}}
Now, we can get both results in one request as we are using aliases. Try the following interactive playground to understand aliases. Submit it to see the response to this query.
Until now, we have hard-coded the arguments inside the query string, such as id: 4
. However, in most applications, these arguments are dynamic. For example, there might be an option to look up a specific user on the client side. We could add these dynamic arguments inside the query string, but it is a bad practice. Because then, our client-side code would need to manipulate the query string dynamically at runtime and serialize it into a GraphQL-specific format.
The right way to pass dynamic arguments is by using GraphQL Variables. When sending the request, you can pass them as a separate object outside the query. See the following example:
graphql
# Without using variablesuser (id: 3) {idname}# Using variablesquery getUser($input: ID!) {user (id: $input) {idname}}
To use variables for our arguments, we need to do these three things:
Replace hard-coded value with our variable name, like id: $input
in the above example.
Declare the variable with its type to be accepted by the query, Like $input: ID!
.
Pass the value for this variable in a separate dictionary when making the request. For the above query, we will pass variables in JSON like this:
json
variables: {"input": "3"}
The following interactive playground uses variables to pass arguments to the query. Enter an id
value and submit it to see the response.
You can also set default values of the variables. You can assign them after the type declaration like this:
graphql
query getUser($input: ID = 3) {user(id: $input) {idname}}
If no variable is provided, the id
argument will default to the value 3.