How to Build a Subgraph on Windows for a Non-Tech Curator
By: Reda Benkirane
Tags: GraphQL, Cryptocurrency, Orion Protocol, Blockchain, The Graph

The Graph

In our last phase as a curators we had to evaluate subgraphs, now The Graph team has asked us to build subgraphs from scratch, and the question is: is it possible and how can we do it if we don’t have tech background ? The answer is simply Yes.

As a non tech curator, let me show you a step by step how I did it. In this turorial I have built a subgraph for Orion Protocol, the first gateway to the entire crypto market: aggregating every CEX, DEX, and swap pool into one decentralized platform.

Let’s be honest, The Graph team has done a great job for helping developers generate the files which defines a subgraph (manifest, schema, etc). The biggest challenge I had to face for a non tech guy was to set-up the right environment on my windows machine (downloading and installing Node.js, nmp, yarn, …).

First and before doing anything on our computer, I suggest to look at the following resources to understand what we need to do:

https://thegraph.com/docs is The Graph internal resources which help understand what we are talking about https://www.youtube.com/watch?v=coa0Vw47qNc is a video from David Kajpust (from The Graph team) showing how to build a subgraph on Mac.

Then let’s start !

Create a subgraph in the The Graph explorer

This is the first step: sign in The Graph explorer with your Github account and click on ‘Add subgraph’

Add subgraph in the Explorer

then you will need to fill the fields to create your subgraph. The Github url is not mandatory at this stage

Create a subgraph

After clicking on ‘Create subgraph’, we will be reaching the following screen with 3 needed steps before beeing able to deploy our subgraph

Steps to follow

1. Install Graph CLI

This is the first step, and here we will need to deep dive into our operating system to install correctly the environment we need to be able to get Graph CLI going. I admit, this is where I have spent most of my time getting everything set-up. When you click on ‘Show commands’, we get:

Install step

We will need to have npm or/and yarn installed to be able to have Graph CLI running in our Windows computer. So how to install yarn and npm ?

First I would recommend to use Microsoft PowerShell as an administrator to run your commands. I have found it more reliable than the standard CMD.

To be able to use PowerShell properly, you will need to give it the ability to execute scripts which is not by default. To do that, lauch PowerShell as an administrator and run:

$ Set-ExecutionPolicy RemoteSigned

$ Set-ExecutionPolicy Unrestricted -scope Process

Each time you will need to confirm by tapping Y (for Yes).

Then install the following:

Node.js (https://nodejs.org/en/)

Git for Windows (https://gitforwindows.org/)

Yarn for Windows with the installer (https://classic.yarnpkg.com/en/docs/install/#windows-stable)

In the PowerShell run:

$ npm install -g npm

You might need to upgrade your version. In this case run:

$ npm-windows-upgrade

You will have to choose which version you want to upgrade to. In my case I choose the last one 7.0.15:

npm windows upgrade

Now that we have gone through all those steps, we can finally install Graph CLI:

Install Graph CLI

As you can see installing Graph CLI comes ou with some pain (errors), but at the end the installation is completed and we do’t have to be afraid for what is coming next.

2. Initiate the subgraph

Let’s move now to step 2

Initiate the subgraph

Unlike the exmple provided by The Graph team (here we have an example built by the team), we will need to initiate the subgraph by going into the Orion Protocol smartcontract, that we will need to find out first.

Google is your friend in this case :-)

Google search result

Let’s head to Etherscan to find the Orion Protocol contract address

Orion Protocol smart contract address

Now that we have the address we can run the command:

$ graph init \ --from-contract 0x0258F474786DdFd37ABCE6df6BBb1Dd5dfC4434a /<Githubusername>/<Directory>

which will give in the MS PowerShell with my Github username and the directory I will create the subgraph in:

The subgraph is created ! Now we need to deploy it. But before that, we will need to double check the generated schema and mappings and update it with the correct fields and values.

3. Deploy the subgraph

Now step 3 !

Frankly, I have spent a lot of time in this step, and I will explain why a little bit later

Step 3 to deploy the subgraph

First we need to authenticate with The Graph hosted service to be able to deploy our subgraph. In this case we need to run the following command:

$ graph auth https://api.thegraph.com/deploy/ <ACCESS_TOKEN>

We will gate the ACCESS TOKEN from our subgraph page in the Explorer:

Access token from The Graph Explorer subgraph page

When running the command on MS Powershell, I’ve got the following:

Error while trying to authenticate with The Graph

When read the error message, you will find the following: ‘The original error was: Cannot find module ‘Keytar’

So I’m missing a module named Keytar in my set-up environment …

Let’s install it then:

Install Keytar

You need absolutely to add the ‘-g’ before ‘keytar’, otherwise your command will fail again and again and again …. (tried it probably more than 10 times). Putting ‘-g’ before ‘keytar’ will install it in the global space of your environment. Now, let’s run the command again:

The Graph authentication confirmed

the next step now would be to deploy the subgraph with the command:

$ graph deploy \

--debug \

--node https://api.thegraph.com/deploy/ \

--ipfs https://api.thegraph.com/ipfs/ \

<SUBGRAPH_NAME>

I have tried this command many many times but failed to make it working …

Subgraph deployment failing

What is needed to do in this case is:

1. Go to the Directory where the subgraph is

$ cd Orion

2. Run the command

$ yarn deploy

Subgraph deployed !

There we go the subgraph is syncing with the hosted service !

To check it we can go back to The Graph Explorer subgraph page where after synching we can see:

Subgraph synked with The Graph hosted service

And if we search in the Explorer for Orion we can find:

Subgraph deployed

The subgraph is deployed !

Conclusion

Nothing’s impossible and as a non tech curators we can also initiate and deploy subgraphs. I hope this tutorial will ease the path for all those who are not tech savy and show exactly how everyone can initiate and deploy subgraphs easily in The Graph Explorer