The functionality of everything around us is dependent on data, and if a system intends to perform better, then it is imperative that the system data gets analyzed and modified as required. The creation of a distributed ledger technology called blockchain, gave us new methods to deal with data transfer, analysis, storage, and retrieval. It’s important to note that blockchain is a decentralized technology with an immutable system that has been discovered to possess more abilities than just driving digital currencies. One blockchain-based protocol that has efficient abilities for querying and indexing data is “The Graph”, and since its inception, it has fared pretty nicely as a data tool.
The Graph: What is it?
One of the beautiful things about the revolutionary blockchain technology is the diverse use cases it brings to various systems, The Graph was designed as a decentralized framework to facilitate Ethereum blockchain-based methods to facilitate data querying, and data indexing. Performing queries of data that has been sent to the blockchain network is an almost impossible task, because blockchain data is encoded with cryptographic properties. Running a protocol like The Graph, it becomes possible to ask a direct query of DApps, for instance, if there is a DApp that is used for collectibles, a query like “What number of cards does XYZ Ethereum account have?” becomes possible. That is possible because the system has a read protocol that supports a ‘balanceof’ query linked with smart contracts. Without an indexing and querying frameworkl like The Graph, it would be Herculean to task a random DApp with performing the indexing and querying.
It isn’t a straightforward process to index data on the chain, because there are different factors that could complicate the process, including reorganizations of the chain, finality, etc. It is not just a time-consuming process, but there is also the possibility of not getting accurate results. However, with the Graph, it has become much easier and straightforward, because the hosted service performs the function of indexing blockchain data. It is also worthy of note that these indexes are also known as ‘Subgraphs’, and the right tool to facilitate the processes in the queries is GraphQLAPI.
The Subgraph
This protocol comes into view when it’s imperative to identify blockchain data intended for indexing, and also to define how its storage will happen. After its deployment, it becomes an integral part of the global graph. There are several files that make up subgraph’s processes, and they include:
schema.graphql
This prompt is utilized for defining the storage of the subgraph blockchain data. Furthermore, it also determines how the indexed data should be queried with the GraphQL.
subragph.yam1
This refers to the process’s YAML protocol where the subgraph’s records are contained.
AssemblyScript Mappings
In order to perform some translation from the data to the schema-defined entities, an AssemblyScript code is used.
How to Use a Subgraph
There are various reasons why you may want to index and/or query data, and thus it’s important to know the steps involved when building and utilizing the subgraph, and these steps include:
Installation
It is imperative that you perform an installation of the Graph CLI, because that’s the building block for the creation and deployment of a subgraph. It may interest you to know that Graph CLI is a JavaScript program, and thus it requires the pre-installation of either https://www.npmjs.com/get-npm (NPM) or https://yarnpkg.com/lang/en/docs/install/#centos-stable (Yarn). For this post, let us assume you pick yarn, here are the detailed steps to have it installed: https://github.com/graphprotocol/graph-cli/wiki/Installation-on-different-operating-systems. After installing yarn, performing a Graph CLI installation is next, and this can be achieved by using: yarn global add @graphprotocol/graph-cli
Create Project
After installing the Graph CLI, you should create your first subgraph project. This can be achieved by running a ‘graph init’ (without the quotes) command. Getting a subgraph project created, can be achieved with an already deployed subgraph like https://github.com/graphprotocol/example-subgraph or from a contract contained within Ethereum networks.
Creating from a contract, this is the command to run:
graph init \
— from-contract <CONTRACT_ADDRESS> \
[ — network <ETHEREUM_NETWORK>] \
[ — abi <FILE>] \
<GITHUB_USER>/<SUBGRAPH_NAME> [<DIRECTORY>]
The above command is used when creating a subgraph for a smart contract’s events indexing.
Deploying a subgraph requires the following steps:
Graph Explorer Account
It is important that you sign up for a Github account https://github.com/. The GitHub account is your key to getting signed up on the Explorer (https://thegraph.com/explorer/). Use the ‘Sign up with Github’ option and follow the simple authorization process.
Access Token Storage
After creating your Explorer account, there will be an access token on your control panel. Run this code: graph auth https://api.thegraph.com/deploy/ <ACCESS_TOKEN>. This access token gets stored on your computer for easy retrieval
Create Subgraph
Visit your Explorer’s control panel and select the ‘Add Subgraph’ option. Fill the form with the required data.
Deploy
With your just created subgraph, the next part of the process is getting it deployed, and this involves having the subgraph files uploaded to IPFS, where the Explorer will begin the indexing processes.
It’s important that you use ‘yarn build’ to have the subgraph files built, and ‘yarn deploy’ to send them.
After the successful deployment of the subgraph, the Explorer will begin the syncing process between your subgraph and the Ethereum blocks. When the Graph Node is done extracting data, the status of the subgraph will change to ‘Synced’. Furthermore, the Graph Node also performs the function of checking the Ethereum blocks for changes.
You can also redeploy the subgraph when changes have been made, and use the ‘yarn deploy’ to execute the redeployment of the updated subgraph. The subgraph can also be sent to different Ethereum networks, but a major challenge with that is the disparity in contract addresses of each Ethereum network. However, a system like https://mustache.github.io/ or https://handlebarsjs.com/ can be employed to change contract addresses into parameters, and thus facilitate the deployment of the subgraph on multiple networks.
The subgraph is healthy if there is a successful sync, however, the Graph Node performs regular checks and can be used to expose endpoints.
Useful links: