Alias

An Alias is about renaming. We can rename a field to what we want. A field can have both parameter as well as not have parameters. Let's look at the two different versions:

Field with parameter

firstUser: user(id: '1') {
  name,
  age
}

secondUser: user(id: '2') {
  name,
  age
}

//
{
  data: {
    firstUser: {
      name: "Chris"
    },
    secondUser: {
      name: "Chross"
    }
  }
}

As we can see above we rename the field user to firstUser and secondUser and we do so by letting our chosen name precede the field name user

Field, with no parameter

Our second version of using an alias is when we simply rename a field like so:

users {
 lastName: name,
 deliveryAddress: address {
   city,
   country
   postalAddress
 }
}

Above we have created the aliases lastName and deliveryAddress.

Summary

As you can see using an Alias is simply about renaming a field. This kind of renaming might be something you are used to be able to do when querying a database with SQL.

However when you are on the frontend you might need to resort to first make your query and after receiving, then and only then, are you able to rename the fields to suit your frontend. With GraphQL and aliases you are able to take away that chore from the front end and do the rename in the GraphQL query itself when you use Alias.

results matching ""

    No results matching ""