In the Introduction to The Graph network part I, we learned about The Graph network, subgraphs, and how The Graph network is intended to operate in the future.
In this part, we’ll be looking at how to deploy a subgraph and how to query it.
However we won’t be creating our own node, we will be using the hosting service provided by The Graph.
Using Hosting Service Of The Graph:
Before we create a subgraph, we need a place for it to exist on, in our case we are using the hosting service of The Graph network.
In order to do that the first important thing is having an account on graph explorer.
Just sign in to your GitHub account and click sign up with Github.
After you sign up, go to the dashboard.
Add a subgraph:
To add a subgraph, click on the add subgraph button.
You will be prompted to a page like the following snippet.
I’ll explain the relevant fields only.
- Subgraph Name: This is the name of the subgraph, once named, you can’t change it.
- Account: As you probably know the Github can have a personal account or organizational accounts, you can toggle between those here.
- Subtitle: Just add little information regarding the subgraph. This field is necessary,
Then click create subgraph.
This way we have an end-point to create the subgraph on.
Now you are all set up on The Graph Networks side, we will now move to our system’s terminal and proceed with deploying the subgraph from there.
Deploying the Subgraph:
From here on all commands are to be entered in your system terminal.
Installing The Graph cli
On your system, install the cli globally using either yarn or npm
$ npm install -g @graphprotocol/graph-cli
$ yarn global add @graphprotocol/graph-cli
There are instances when installation from yarn globally doesn’t work.
$ export PATH=”$(yarn global bin):$PATH”
Initiating the Graph
To create the graph, run the following command
$ graph init
Then the console will prompt for information.
- Subgraph Name: This is the name of the subgraph.should be identical to the one entered on the dashboard preceding your GitHub username.
- Directory to create the subgraph in: This is the directory name.
- Ethereum network: The Graph network supports multiple ethereum networks such as mainnet, kovan, rinkeby, ropsten, goerli, and poa-core. In this field just choose the one your contract is on.
- Contract Address: Enter your contract address here. I used a dummy address
After you enter the relevant data, the cli will fetch the ABI from etherscan, You can enter the local path of ABI as well.
The end result will be as follows:
Understanding the file structure:
Once the subgraph is initiated, default files will show up in your directory.
It consists of:
- Schema.graphQL: Define the entities and their attributes in this file.
- Subgraph.yaml: This file consists of all the information regarding your project, like network, contract address, start block, event handlers, call handlers, block handlers, and data sources.
- src/mapping.ts: In this file, you create the logic for event handling, block handling, and call handling.
After you edit the schema and subgraph.yaml file, use the command
$ yarn codegen
This command will generate or update the auto-generated files
Then make relevant changes to the mapping file.
To save those changes run
$ yarn build
Deploying the Subgraph:
To deploy the subgraph first we need to authenticate with the hosted service. To do that copy the access token from the dashboard.
Then run the following command
$ graph auth https://api.thegraph.com/deploy/ <your access token>
Go inside your subgraph directory by simply running
$ cd <directory name>
Then a simple command will deploy the subgraph
$ yarn deploy
Keeping track of progress:
Once a subgraph is deployed, the graph node will sync, the progress can be tracked via the dashboard.
This syncing starts from the genesis block however it is a more practical approach to give start block property, in this way the subgraph will start syncing from the starting block to the recently mined blocks.
Redeploying the subgraph:
If you make a change in the subgraph files, you can redeploy it on the same endpoint by using the same command of:
$ yarn deploy
When a subgraph is deployed for the first time, it has no version, however, when subgraphs are updated they exist in two states. The current version holds the initial state of the subgraph. While the updating / syncing version falls under the pending version category.
Querying the Subgraph:
GraphQL is used to query the subgraph.
You can query any subgraph you own, or the subgraphs lying on the explorer. You can even query the subgraphs which aren’t on explorer, all you need is the Query link.
In the above snippet, a simple query to fetch all balances and their id where balance is greater than 0 is taking place. The results can be seen as well. In the schema column, lies the entities present in your schema.
In cli installation, I mentioned that you can install it using either yarn or npm. But The Graph installs dependency using yarn, which is why, even if you install the package using npm, to deploy and build subgraph you still have to use the yarn package.
This article covers all you need to know about deploying the subgraph. Now you can create and deploy subgraphs on any contract of your liking.
Uniswap, Airswap, Opium Network, Sabier, and many other services hosted their subgraphs on The Graphs hosting service as well.
Xord will help you integrate your business processes with Blockchain technology. Follow the link to connect with us: https://xord.com/contact/