Eating blockchain data using GraphQL spoon

In this article, we will talk about how to get blockchain data (on-chain data) using Bitquery GraphQL APIs and why it’s better than the product out there. 

Note: The article is focused on developers and engineering teams.

Data problems in the Blockchain space

Blockchains only emits transactions and events. You can run a blockchain node and consume this raw data, store it, and then query it based on your application needs. 

However, if you are a developer, you just want to focus on your application. Running, marinating, and then storing, indexing, and maintaining the database is a overhead. This extra effort kills many blockchain ideas everyday.

Many blockchain data provides like provide simple APIs to consume blockchain data. However, almost all of them provide you with rest APIs. 

Rest API good enough to get your job done, but they are not the best choice when we talk about consuming complex blockchain data. 

In this article, I will argue, why our GraphQL APIs are better for building complex blockchain products. If you already know GraphQL, then get started with the first Blockchain GraphQL query.

What is GraphQL?

GraphQL is the syntax that describes how to ask for the data. It is a query language for APIs using which you can describe your exact data needs. 

GraphQL is better than rest APIs for many reasons.

Get exactly you ask for

With GraphQL, you get what you describe in your queries. Nothing more, nothing less. This reduce network call size and increase performance.

Let’s take an example where you just need the latest block height number nothing else.

Usually, in rest APIs, you get an endpoint like — /get/latestblockand it provides you all the details of the latest block for a given blockchain, even if you just need the height.

With GraphQL you can write something like below. 

Note: You can test these queries here — https://graphql.bitquery.io/

{
ethereum {
blocks(options: {desc: "height", limit: 1}) {
height
}
}
}

And you get the following result 

{
"data": {
"ethereum": {
"blocks": [
{
"height": 10928214
}
]
}
}
}

Let’s say you also want to know the transaction count in the latest block. You just need to add the transactionCount in the query.

{
ethereum {
blocks(options: {desc: "height", limit: 1}) {
height
transactionCount
}
}
}

Run the query above and you will get the following result:

{
"data": {
"ethereum": {
"blocks": [
{
"height": 10928214,
"transactionCount": 142
}
]
}
}
}

Did you see it? we are only getting what we need, nothing more, nothing less.

One request, many resources

Let’s say you want to see the block count of Bitcoin and Ethereum blockchain. Usually, in rest APIs, you have /bitcoin/blocks and /ethereum/blocks endpoints and you need to call both of them to get the numbers of blocks in both blockchains.

However, with GraphQL you can write a single query to get data from both blockchains. 

{
bitcoin {
blocks {
count
}
}
ethereum {
blocks {
count
}
}
}

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

{
"data": {
"bitcoin": {
"blocks": [
{
"count": 650429
}
]
},
"ethereum": {
"blocks": [
{
"count": 10953695
}
]
}
}
}

This is a significant boost for your application performance and also help you build application faster and extend them easily.

Single Endpoint for every request

Unlike rest, where you need to manage multiple resource endpoints. With GraphQL, you only get a single endpoint and you hit all your queries to that endpoint. For example, our GraphQL endpoint is graphql.bitquery.io.

Scale, Documentation, Integration, and better readability

GraphQL is easy to scale, for example, currently, we support more than 20 blockchains through our APIs and you can get data across blockchain using our GraphQL endpoint. GraphQL automatically generates schema documentation, which is easier to read and understand when integrating with APIs.

In addition, GraphQL descriptive query provides better code readability.

However, there are also some disadvantages of using GraphQL APIs like browser caching. Because, there is only a single end point, therefore browser caching for GraphQL requests doesn’t work. In addition, error handling is also different then rest APIs.

You can read more about why GraphQL is better than rest APIs.

Also, Read

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:

  • Coinpath® APIs provide blockchain money flow analysis 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®.
  • 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.