How to use GraphQL Alias and Aggregation?

This article is a continuation of our GraphQL basics series; you can read the series’s previous articles here. In this article, we will talk about how to use alias and aggregation with GraphQL queries.

GraphQL Alias

Client-side GraphQL aliasing helps in increasing the readability of APIs and avoid confusion.

Let’s understand this with an example.

The query below provides the DEX trading volume over the years on the Ethereum blockchain.

When you run this query, you will get the following result. 

{
  "data": {
    "ethereum": {
      "dexTrades": [
        {
          "count": 26025471,
          "tradeAmount": 79630600909.80501,
          "date": {
            "year": 2020
          }
        },
        {
          "count": 4839824,
          "tradeAmount": 2352564161.2578993,
          "date": {
            "year": 2019
          }
        },
        {
          "count": 6059070,
          "tradeAmount": 4742543997.574773,
          "date": {
            "year": 2018
          }
        },
        ...
        ...
        ...

However, following this result is easy, but the count parameter can create confusion, such as “What does this count represent?”. Let’s make it better.

{
ethereum {
dexTrades(options: {desc: ["date.year"]}) {
numberOfTrades: count
tradeAmount(in: USD)
date {
year
}
}
}
}

As you can see, we have changed count to numberOfTrades: count . It is now more understandable for anyone looking at API results. Additionally, you can give it any name you want.

Sometimes alias becomes necessary for queries to work. For example, check out the query below.

This query is getting active addresses over the years from Ethereum and Binance smart chain. However, if we don’t provide an alias for ethereum(network: bsc), it will throw an error. Therefore, sometimes it is necessary to use an alias to avoid name resolution problems.

Note: Only in GraphQL, you have the power to use aliases in API parameters. It is possible in the Rest APIs.

There are many benefits of using aliases, for example, increasing readability, replacing exiting APIs, etc.

GraphQL Aggregation

Aggregation is critical for performing data analytics. GraphQL technology doesn’t have any pre-build support for aggregation; it only responds to Types. Therefore, you need to define the Types which allow aggregations for your users, and this is exactly what we did. 😃

For example, check out the query below, in which we will get Gas related analytics for the Binance smart chain.

In which, we are performing multiple aggregation functions such as average, median, maximum, count, etc. In addition, we are also performing aggregation on the USD value of Gas for every transaction. gasValue(calculate: average in:USD)

Let’s see another example in which we will sum all the input values of Bitcoin transactions.

Aggregating with limitBy

We recently added an option called limitBy to aggregate data for any arbitrary field used in the query. For example, let’s say we want to know, “what are the top addresses with most transactions daily on the Binance smart chain?

 For this, we can use the following query.

Here we are using limitBy option to perform our aggregation on every date. You can also perform this for any timeframe, such as hour, week, minute, etc. 

limitBy option takes 3 parameters.

  • each — each is the field name. The query will return the first “limit” rows for each distinct combination of “each”
  • limit — number of records 
  • offset — number of records to skip, starts with 0

As you can see, you can perform different types of aggregation functions using our GraphQL APIs. If you have any questions please, join our Telegram group and let us know.

You might also be interested in:

About Bitquery

Bitquery is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. Our products are:

  • Digital Assets API provides index information related to all major cryptocurrencies, coins, and tokens.
  • DEX API provides real-time deposits and transactions, trades, and other related data on different DEX protocols like Uniswap, Kyber Network, Airswap, Matching Network, etc.

If you have any questions about our products, ask them on our Telegram channel or email us at hello@bitquery.io. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.