{"id":2887,"date":"2021-05-04T10:46:00","date_gmt":"2021-05-03T22:46:00","guid":{"rendered":"https:\/\/www.talkcrypto.org\/blog\/?p=2887"},"modified":"2023-11-28T01:08:20","modified_gmt":"2023-11-27T12:08:20","slug":"part-1-subql-hello-world-in-less-than-5-mins","status":"publish","type":"post","link":"https:\/\/www.talkcrypto.org\/blog\/2021\/05\/04\/part-1-subql-hello-world-in-less-than-5-mins\/","title":{"rendered":"Part 1: SubQuery (aka Subql) Hello World in less than 5 mins"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>They say that cooking is quick and easy, it&#8217;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&#8217;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. <\/p>\n\n\n\n<p>First, let&#8217;s do a quick pre-requisite check.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisite check:<\/h2>\n\n\n\n<p>Run the following: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn -v\nsubql -v\ndocker -v<\/code><\/pre>\n\n\n\n<p>For more advanced users, copy and paste the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo -e \"My yarn version is:\" `yarn -v` \"\\nMy subql version is:\" `subql -v`  \"\\nMy docker version is:\" `docker -v`<\/code><\/pre>\n\n\n\n<p>This should return:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"727\" height=\"117\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-1.png\" alt=\"\" class=\"wp-image-2889\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-1.png 727w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-1-300x48.png 300w\" sizes=\"(max-width: 727px) 100vw, 727px\" \/><\/figure>\n\n\n\n<p>If you get the above, then you are good to go. If not, install the required applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Subql Hello world<\/h2>\n\n\n\n<p>Let&#8217;s <strong>initialise<\/strong> a <strong>starter<\/strong> project with the name <strong>subqlHelloWorld<\/strong>. Note that only Author is mandatory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>subql init --starter subqlHelloWorld<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"541\" height=\"153\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-3.png\" alt=\"\" class=\"wp-image-2891\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-3.png 541w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-3-300x85.png 300w\" sizes=\"(max-width: 541px) 100vw, 541px\" \/><\/figure>\n\n\n\n<p>Next change into this directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd subqlHelloWorld\/<\/code><\/pre>\n\n\n\n<p>Now do a yarn or node install. I&#8217;ll use yarn here.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn install<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"338\" height=\"170\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-4.png\" alt=\"\" class=\"wp-image-2892\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-4.png 338w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-4-300x151.png 300w\" sizes=\"(max-width: 338px) 100vw, 338px\" \/><\/figure>\n\n\n\n<p>Then run yarn codegen<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn codegen<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"335\" height=\"191\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-6.png\" alt=\"\" class=\"wp-image-2894\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-6.png 335w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-6-300x171.png 300w\" sizes=\"(max-width: 335px) 100vw, 335px\" \/><\/figure>\n\n\n\n<p>Then yarn build:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn build<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"205\" height=\"75\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-7.png\" alt=\"\" class=\"wp-image-2895\"\/><\/figure>\n\n\n\n<p>Then run the docker command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose pull &amp;&amp; docker-compose up<\/code><\/pre>\n\n\n\n<p>This will kick everything into life where eventually you will get blocks being fetched.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>subquery-node_1   | 2021-06-05T22:20:31.450Z &lt;subql-node&gt; INFO node started \nsubquery-node_1   | 2021-06-05T22:20:35.134Z &lt;fetch&gt; INFO fetch block &#91;1, 100] \nsubqlhelloworld_graphql-engine_1 exited with code 0\nsubquery-node_1   | 2021-06-05T22:20:38.412Z &lt;fetch&gt; INFO fetch block &#91;101, 200] \ngraphql-engine_1  | 2021-06-05T22:20:39.353Z &lt;nestjs&gt; INFO Starting Nest application... \ngraphql-engine_1  | 2021-06-05T22:20:39.382Z &lt;nestjs&gt; INFO AppModule dependencies initialized \ngraphql-engine_1  | 2021-06-05T22:20:39.382Z &lt;nestjs&gt; INFO ConfigureModule dependencies initialized \ngraphql-engine_1  | 2021-06-05T22:20:39.383Z &lt;nestjs&gt; INFO GraphqlModule dependencies initialized \ngraphql-engine_1  | 2021-06-05T22:20:39.809Z &lt;nestjs&gt; INFO Nest application successfully started \nsubquery-node_1   | 2021-06-05T22:20:41.122Z &lt;fetch&gt; INFO fetch block &#91;201, 300] \ngraphql-engine_1  | 2021-06-05T22:20:43.244Z &lt;express&gt; INFO request completed <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"912\" height=\"231\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-8.png\" alt=\"\" class=\"wp-image-2896\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-8.png 912w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-8-300x76.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-8-768x195.png 768w\" sizes=\"(max-width: 912px) 100vw, 912px\" \/><\/figure>\n\n\n\n<p>Navigate to http:\/\/localhost:3000\/ and paste the below query into the left side and then hit the play button.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  query{\n    starterEntities(last:10, orderBy:FIELD1_ASC ){\n      nodes{\n        field1\n      }\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>You should get something like: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"427\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-11.png\" alt=\"\" class=\"wp-image-2901\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-11.png 864w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-11-300x148.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-11-768x380.png 768w\" sizes=\"(max-width: 864px) 100vw, 864px\" \/><\/figure>\n\n\n\n<p>The block count in the playground should match the block count (technically the block height) in the terminal.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"888\" height=\"755\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.28.37-am.png\" alt=\"\" class=\"wp-image-2903\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.28.37-am.png 888w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.28.37-am-300x255.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.28.37-am-768x653.png 768w\" sizes=\"(max-width: 888px) 100vw, 888px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>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!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"42\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.23.34-am-1024x42.png\" alt=\"\" class=\"wp-image-2904\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.23.34-am-1024x42.png 1024w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.23.34-am-300x12.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.23.34-am-768x31.png 768w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.23.34-am.png 1324w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>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. <\/p>\n\n\n\n<p>Where to from here? Let&#8217;s take a deeper dive at what is happening under the covers.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bloopers<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Using spaces?<\/h3>\n\n\n\n<p>If you use spaces in your project name you will get the following error. No spaces please!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>seandotau$ subql init --starter subql hello world\n \u203a   Error: Unexpected arguments: hello, world\n \u203a   See more help with --help<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Not a Number?<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>NaN bps, target: #5379522, current: #690, estimate time: NaN days NaN hours NaN mins <\/code><\/pre>\n\n\n\n<p>NaN? This tends to indicate a connectivity error. Try to restart the docker container. ie stop docker with ctrl+c and then run <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose pull &amp;&amp; docker-compose up<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"47\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.25.57-am-1024x47.png\" alt=\"\" class=\"wp-image-2899\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.25.57-am-1024x47.png 1024w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.25.57-am-300x14.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.25.57-am-768x35.png 768w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-06-at-8.25.57-am.png 1329w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Failed to fetch?<\/h3>\n\n\n\n<p>This indicates that your docker container is not up and running. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"error\": \"Failed to fetch. Please check your connection\"\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"234\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-12-1024x234.png\" alt=\"\" class=\"wp-image-2902\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-12-1024x234.png 1024w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-12-300x68.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-12-768x175.png 768w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/image-12.png 1083w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Check your docker instance and re-run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose pull &amp;&amp; docker-compose up<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Introduction They say that cooking is quick and easy, it&#8217;s<\/p>\n","protected":false},"author":1,"featured_media":3419,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2887","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blockchain"],"featured_image_urls":{"full":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow.png",1800,773,false],"thumbnail":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow-150x150.png",150,150,true],"medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow-300x129.png",300,129,true],"medium_large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow-768x330.png",640,275,true],"large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow-1024x440.png",640,275,true],"1536x1536":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow-1536x660.png",1536,660,true],"2048x2048":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow.png",1800,773,false],"chromenews-featured":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow-1024x440.png",1024,440,true],"chromenews-large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow-825x575.png",825,575,true],"chromenews-medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/05\/hellow-590x410.png",590,410,true]},"author_info":{"info":["seandotau"]},"category_info":"<a href=\"https:\/\/www.talkcrypto.org\/blog\/category\/blockchain\/\" rel=\"category tag\">Blockchain<\/a>","tag_info":"Blockchain","comment_count":"1","_links":{"self":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/2887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/comments?post=2887"}],"version-history":[{"count":7,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/2887\/revisions"}],"predecessor-version":[{"id":2989,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/2887\/revisions\/2989"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media\/3419"}],"wp:attachment":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media?parent=2887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/categories?post=2887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/tags?post=2887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}