Introduction

They say that cooking is quick and easy, it’s the preparation that takes all the time. In coding, it is no different. Getting a Hello World example up and running is quick, but the preparation takes a bit of time. Here we’ll spin up a Hello World sample in less than 5 mins but only if you have the right ingredients. This example will spin up a Subql node, a Graphql engine and a Postgres database. It will then synchronise with the live Polkadot mainnet blockchain, and then allow you to query for the block height.

First, let’s do a quick pre-requisite check.

Prerequisite check:

Run the following:

yarn -v
subql -v
docker -v

For more advanced users, copy and paste the following:

echo -e "My yarn version is:" `yarn -v` "\nMy subql version is:" `subql -v`  "\nMy docker version is:" `docker -v`

This should return:

If you get the above, then you are good to go. If not, install the required applications.

Subql Hello world

Let’s initialise a starter project with the name subqlHelloWorld. Note that only Author is mandatory.

subql init --starter subqlHelloWorld

Next change into this directory.

cd subqlHelloWorld/

Now do a yarn or node install. I’ll use yarn here.

yarn install

Then run yarn codegen

yarn codegen

Then yarn build:

yarn build

Then run the docker command:

docker-compose pull && docker-compose up

This will kick everything into life where eventually you will get blocks being fetched.

subquery-node_1   | 2021-06-05T22:20:31.450Z <subql-node> INFO node started 
subquery-node_1   | 2021-06-05T22:20:35.134Z <fetch> INFO fetch block [1, 100] 
subqlhelloworld_graphql-engine_1 exited with code 0
subquery-node_1   | 2021-06-05T22:20:38.412Z <fetch> INFO fetch block [101, 200] 
graphql-engine_1  | 2021-06-05T22:20:39.353Z <nestjs> INFO Starting Nest application... 
graphql-engine_1  | 2021-06-05T22:20:39.382Z <nestjs> INFO AppModule dependencies initialized 
graphql-engine_1  | 2021-06-05T22:20:39.382Z <nestjs> INFO ConfigureModule dependencies initialized 
graphql-engine_1  | 2021-06-05T22:20:39.383Z <nestjs> INFO GraphqlModule dependencies initialized 
graphql-engine_1  | 2021-06-05T22:20:39.809Z <nestjs> INFO Nest application successfully started 
subquery-node_1   | 2021-06-05T22:20:41.122Z <fetch> INFO fetch block [201, 300] 
graphql-engine_1  | 2021-06-05T22:20:43.244Z <express> INFO request completed 

Navigate to http://localhost:3000/ and paste the below query into the left side and then hit the play button.

{
  query{
    starterEntities(last:10, orderBy:FIELD1_ASC ){
      nodes{
        field1
      }
    }
  }
}

You should get something like:

The block count in the playground should match the block count (technically the block height) in the terminal.

You may also notice that the output gives an indication of how long it will take to fully synch with Polkadot mainnet. At the time of writing, this would take me about one and a half days!

Summary

There you have it. A Subql Hello World example up in less than 5 minutes. Assuming you had all the ingredients of course :). What did we do? We initialised a starter project template that was pre-populated with a simple query, ie code was provided for us already, the code also spun up a node, synchronise it, and then we queired the live Polkadot mainnet for the block height.

Where to from here? Let’s take a deeper dive at what is happening under the covers.

Bloopers

Using spaces?

If you use spaces in your project name you will get the following error. No spaces please!

seandotau$ subql init --starter subql hello world
 ›   Error: Unexpected arguments: hello, world
 ›   See more help with --help

Not a Number?

NaN bps, target: #5379522, current: #690, estimate time: NaN days NaN hours NaN mins 

NaN? This tends to indicate a connectivity error. Try to restart the docker container. ie stop docker with ctrl+c and then run

docker-compose pull && docker-compose up

Failed to fetch?

This indicates that your docker container is not up and running.

{
  "error": "Failed to fetch. Please check your connection"
}

Check your docker instance and re-run:

docker-compose pull && docker-compose up

1 thought on “Part 1: SubQuery (aka Subql) Hello World in less than 5 mins

Leave a Reply

Your email address will not be published. Required fields are marked *