The Graph Network [Part - II] - Deploying the Subgraph


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. 

Create Account:

In order to do that the first important thing is having an account on graph explorer.

Hosting service of the The Graph

Just sign in to your GitHub account and click sign up with Github.

After you sign up, go to the dashboard.

Hosting service of the The Graph

Add a subgraph:

Hosting service of the The Graph

To add a subgraph, click on the add subgraph button.

You will be prompted to a page like the following snippet.

Hosting service of the The Graph

I’ll explain the relevant fields only.

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.

Just use

$ 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.

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:

Deploying the Subgraph

Understanding the file structure:

Once the subgraph is initiated, default files will show up in your directory.

It consists of:

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.

Deploying the Subgraph

Then run the following command

$ graph auth <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. 

Deploying the Subgraph

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

Subgraph Versions:

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.

Deploying the Subgraph

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: