A GraphQL Interface for Aion Blockchain

Satya
3 min readAug 9, 2018

--

Edit: The queries provided in this post are no longer valid. For more up-to-date queries, please follow my other post https://medium.com/@satran004/aion-graphql-a-query-layer-for-aion-blockchain-29ec899bd9d7

If you want to setup your own Aion kernel (blockchain) for development purpose, you can follow my previous post.

How to interact with Aion blockchain

You can use any of the following ways to interact with the Aion kernel (blockchain) from your application :

  • web3 Api (JS)
  • Aion Java Api

To know how to use these APIs, check the following wiki page :

https://github.com/aionnetwork/aion/wiki/Application-Development-On-Aion

You can use these APIs to build your own dApps on Aion platform.

But this post is about the possibility of providing another way or API layer to front-end developers, GraphQL Api endpoint for Aion.

Aion GraphQL

GraphQL is a query language created by Facebook. It allows the front-end developers to write queries that have the exact data shape required by the client. Unlike REST, GraphQL server exposes only one endpoint which is accessed by client to get different types of data.

GraphQL is becoming more and more popular and being used with other front-end technologies / JS libraries.

So why not a GraphQL API layer for Aion ?

I decided to use Aion java api and expose a GraphQL layer on top of Aion kernel. The idea is that during building this layer, I will get to know how to use the java api provided by Aion and also we will have a GraphQL service layer ready.

I have done a very initial implementation. You can find the source code here :

The current implementation only supports the following functionalities :

  • Get blocks (with Pagination)
  • Get block by hash
  • Get Transaction by hash

I will keep adding support for other data, posting transaction and finally invoking contract method on aion blockchain through GraphQL api.

If you are interested, you can watch this Github project.

Later this GraphQL layer can be used with other client-side JS frameworks like Reactjs, Angular etc. to build dApps.

Test GraphQL Endpoint

If you want to check how the GraphQL interface for Aion looks like, you can try the following graphiql link

https://graphql.aion-tools.info/graphiql

FYI, this deployment is only for testing or demo purpose. It connects to Aion mainet.

Sample Queries:

You can try the following sample queries. You need to copy & paste the following queries in the above graphiql page.

You can also add other fields or remove existing fields from any of the below query and the server will send the response accordingly.

  1. Get last 10 blocks
{
blocks{
number
hash
nrgConsumed
txDetails {
to
from
value
nonce
}
}
}

2. Get 20 blocks before a given block number (Example: 883995)

{
blocks(first:20, offset:883995) {
number
hash
nrgConsumed
txDetails {
to
from
value
nonce
}
}
}

3. Get a transaction by hash

{
transaction(txHash:"c04b14987bd6449af37787e6efb65d4ae9bc93026d6b51abdd7a5302996da957") {
to
from
value
blockNumber
nrgConsumed
nrgPrice
txHash
}
}

Build & Deploy your own Aion GraphQL service layer

If you want to deploy aion-graphql project on your own server, you need to build the project from source first.

Build from Source

To build Aion GraphQL project, you need the followings :

> Ubuntu 16.04 LTS or later

> Java 10.x and later

> Maven 3.5.x or later

To compile, run the following maven command:

mvn clean package

To start the GraphQL server, execute the following maven command:

mvn spring-boot:run -Dfile.encoding=UTF-8 -Dspring-boot.run.arguments= — rpc.endpoint=tcp://<aion_kernel_host>:8547

This will start the server on port 8080. You can then access your local deployment’s graphiql interface.

https://localhost:8080/graphiql

Link to GitHub project: https://github.com/satran004/aion-graphql

Test deployment: https://graphql.aion-tools.info/graphiql

--

--

Satya
Satya

Written by Satya

A developer’s journey in blockchain world

No responses yet