How to use GraphQL Alias and Aggregation?
Tags: graphqltutorials, graphql
Published: 2020-12-02 07:12:14

---
title: How to use GraphQL Alias and Aggregation?
published: true
date: 2020-12-01 10:29:37 UTC
tags: GraphQLTutorials,graphql
canonical_url: https://bitquery.io/blog/graphql-alias-and-aggregation?utm_source=rss&utm_medium=rss&utm_campaign=graphql-alias-and-aggregation
---

This article is a continuation of our GraphQL basics series; you can read the series’s previous articles [here](https://bitquery.io/blog/category/graphql-tutorials). 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](https://explorer.bitquery.io/ethereum/dex) over the years on the [Ethereum](https://explorer.bitquery.io/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](https://explorer.bitquery.io/ethereum) and [Binance smart chain](https://explorer.bitquery.io/bsc). 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](https://graphql.org/learn/schema/). 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](https://t.me/Bloxy_info) and let us know.

You might also be interested in:

- [Offline Dashboards | Client only Blockchain data visualization and tools](https://bitquery.io/blog/offline-dashboards)
- [Ethereum DEX GraphQL APIs with Examples](https://bitquery.io/blog/ethereum-dex-graphql-api)
- [How to get newly created Ethereum Tokens?](https://bitquery.io/blog/newly-created-etheruem-token)
- [How to investigate an Ethereum address?](https://bitquery.io/blog/investigate-ethereum-address)
- [API to get Ethereum Smart Contract Events](https://bitquery.io/blog/ethereum-events-api)
- [Simple APIs to get Latest Uniswap Pair Listing](https://bitquery.io/blog/uniswap-pool-api)
- [ETH2.0 Analytical Explorer, Widgets, and GraphQL APIs](https://bitquery.io/blog/eth2-explorer-api-widgets)
- [The Graph vs Bitquery – Solving Blockchain Data Problems](https://bitquery.io/blog/thegraph-and-bitquery)
- [Analyzing Decentralized Exchange using Bitquery Blockchain Explorer](https://bitquery.io/blog/dex-blockchain-explorer)

#### **About** Bitquery

[**Bitquery**](https://bitquery.io/?source=blog&utm_medium=about_coinpath) is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. Our products are:

- **[Coinpath®](https://bitquery.io/products/coinpath?utm_source=blog) APIs** provide [blockchain money flow analysis](https://blog.bitquery.io/coinpath-blockchain-money-flow-apis) for more than 24 blockchains. With Coinpath’s APIs, you can monitor blockchain transactions, investigate crypto crimes such as bitcoin money laundering, and create crypto forensics tools. Read [this to get started with Coinpath®](https://blog.bitquery.io/coinpath-api-get-start).

- **[Digital Assets API](https://bitquery.io/products/digital_assets?utm_source=blog&utm_medium=about)** provides index information related to all major cryptocurrencies, coins, and tokens. 

- **[DEX API](https://bitquery.io/products/dex?utm_source=blog&utm_medium=about)** 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](https://t.me/Bloxy_info) or email us at [hello@bitquery.io](mailto:hello@bitquery.io). Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.

The post [How to use GraphQL Alias and Aggregation?](https://bitquery.io/blog/graphql-alias-and-aggregation) appeared first on [Bitquery](https://bitquery.io).