{"id":3036,"date":"2021-06-15T15:16:00","date_gmt":"2021-06-15T03:16:00","guid":{"rendered":"https:\/\/www.talkcrypto.org\/blog\/?p=3036"},"modified":"2023-11-28T01:23:41","modified_gmt":"2023-11-27T12:23:41","slug":"how-to-strip-down-a-subquery-starter-project-to-its-minimum","status":"publish","type":"post","link":"https:\/\/www.talkcrypto.org\/blog\/2021\/06\/15\/how-to-strip-down-a-subquery-starter-project-to-its-minimum\/","title":{"rendered":"How to strip down a SubQuery starter project to its minimum"},"content":{"rendered":"\n<p>The SubQuery starter project is great way to get up and running very quickly. One drawback however is that it labels its field as &#8220;field1, field 2&#8221; etc and that it attempts to demonstrate the 3 types of mapping functions, namely block handlers, event handlers and call handers all in one go. This isn&#8217;t a bad thing but it can be slightly overwhelming for a beginner.  <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-4.png\" alt=\"\" class=\"wp-image-3037\" width=\"545\" height=\"352\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-4.png 850w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-4-300x194.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-4-768x497.png 768w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure>\n\n\n\n<p>Here, we&#8217;ll strip everything down to its bare minimum. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TL;DR<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Delete all the field variables leaving field 1, but rename field1 to blockHeight<\/li><li>Remove the handleEvent and handleCall from the mappingHandlers.ts to leave only handleBlock.<\/li><li>Update the project.yaml file to remove the unnecessary mapping handlers (linked to #2).<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Pre-requisite<\/h2>\n\n\n\n<p>Start by running the standard init command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>subql init --starter myproject<\/code><\/pre>\n\n\n\n<p>If you are not sure what the above means, check out <a rel=\"noreferrer noopener\" href=\"https:\/\/www.talkcrypto.org\/blog\/2021\/05\/04\/part-1-subql-hello-world-in-less-than-5-mins\/\" target=\"_blank\">this<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Modify the variables<\/h2>\n\n\n\n<p>Change the schema.graph from:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type StarterEntity @entity {\n  id: ID! #id is a required field\n  field1: Int!\n  field2: String #filed2 is an optional field\n  field3: BigInt\n  field4: Date\n  field5: Boolean\n}\n<\/code><\/pre>\n\n\n\n<p>to <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type StarterEntity @entity {\n  id: ID! #id is a required field\n  blockHeight: Int!\n}\n<\/code><\/pre>\n\n\n\n<p>We are a) giving the variable a meaningful name and b) simplifying by removing what is not necessary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Just keep handleBlock<\/h2>\n\n\n\n<p>In the mappingHandler.ts file of the default starter project, delete the handleEvent and the handleCall function. We&#8217;ll look at these later on. This is what the file should look like. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {SubstrateExtrinsic,SubstrateEvent,SubstrateBlock} from \"@subql\/types\";\nimport {StarterEntity} from \"..\/types\";\nimport {Balance} from \"@polkadot\/types\/interfaces\";\n\n\nexport async function handleBlock(block: SubstrateBlock): Promise&lt;void&gt; {\n    \/\/Create a new starterEntity with ID using block hash\n    let record = new StarterEntity(block.block.header.hash.toString());\n    \/\/Record block number\n    record.blockHeight = block.block.header.number.toNumber();\n    await record.save();\n}\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Update the project.yaml file<\/h2>\n\n\n\n<p>The last step is to update the project.yaml file to remove references to the removed functions. It should look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>specVersion: 0.0.1\ndescription: \"\"\nrepository: \"\"\nschema: .\/schema.graphql\nnetwork:\n  endpoint: wss:\/\/polkadot.api.onfinality.io\/public-ws\n  dictionary: https:\/\/api.subquery.network\/sq\/subquery\/dictionary-polkadot\ndataSources:\n  - name: main\n    kind: substrate\/Runtime\n    startBlock: 1\n    mapping:\n      handlers:\n        - handler: handleBlock\n          kind: substrate\/BlockHandler\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Build and run the project<\/h2>\n\n\n\n<p>Generate and build the code (yarn codegen &amp; yarn build) and then run the project, either in Docker, or have it hosted by SubQuery and then test it with a query. Below is a query you can use for convenience.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  query{\n    starterEntities(last:10){\n      nodes{\n        blockH\n      }\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"361\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-5-1024x361.png\" alt=\"\" class=\"wp-image-3038\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-5-1024x361.png 1024w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-5-300x106.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-5-768x271.png 768w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-5-1536x541.png 1536w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/07\/image-5.png 1726w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The SubQuery starter project is great way to get up<\/p>\n","protected":false},"author":1,"featured_media":3423,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3036","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\/06\/subquery.png",1800,773,false],"thumbnail":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-150x150.png",150,150,true],"medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-300x129.png",300,129,true],"medium_large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-768x330.png",640,275,true],"large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-1024x440.png",640,275,true],"1536x1536":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-1536x660.png",1536,660,true],"2048x2048":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery.png",1800,773,false],"chromenews-featured":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-1024x440.png",1024,440,true],"chromenews-large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-825x575.png",825,575,true],"chromenews-medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-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":"0","_links":{"self":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/3036","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=3036"}],"version-history":[{"count":1,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/3036\/revisions"}],"predecessor-version":[{"id":3039,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/3036\/revisions\/3039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media\/3423"}],"wp:attachment":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media?parent=3036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/categories?post=3036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/tags?post=3036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}