Getting started for the local development
The Steps of Deploying a Subgraph Locally.
Before we start, let’s have a quick look at the steps covered in this guide
1. Setting up Ganache CLI.
Set up your terminal and run the following two commands within it to install Ganache and Truffle globally:
Wait for it to finish installing. Once it’s done, run the following command in the same terminal:
What this does is to create a total of 10 test accounts for you that have some ETH on all of them.
Ganache binds by default to 127.0.0.1
, making it only accessible from the host machine it is running on. For this reason, it is necessary to add -h 0.0.0.0
within the command to make it assessable from within Docker.
2. Starting Graph Node locally.
A) Cloning The Graph Node
Open another terminal and clone The Graph Node with the following command:
B) Accessing Docker directory of The Graph Node
Continue in the process by accessing The Graph Node’s Docker directory:
C) Replacing host IP (Linux users only)
(This step is for Linux users only): Linux does not support the host.docker.internal
alias for the host machine that The Graph Node Docker Compose uses. Run the following command from the Docker directory to replace the alias with the host IP address. What this does is that the host IP address will be automatically written into docker-compose.yml
.
D) Starting the local Graph Node
You can now start the local Graph Node to connect to Ganache on your host. To do so, run the following command:
3. Initializing new subgraph.
A) Globally installing Graph CLI
In the next step we will have to globally install The Graph CLI. The commands for npm or yarn (depending on which package manager you are using) are as follows:
B) Creating an example subgraph
Run the following command inside the folder on your machine you want to create a subgraph in:
Replace the placeholders with the following credentials:
By running the command you create an example subgraph. You can use the subgraph to build your own subgraph on top of it. In the next tutorial, we will show you how to define a subgraph.
4. Deploying smart contract to Ganache.
After creating the sample subgraph via graph init
you’re now ready compile and deploy a sample smart contract. By deploying the example smart contract, the subgraph you have created earlier will index it to Ganache. You need to run the following command to deploy the smart contract in the subgraph’s root directory (from step 3):
The command compiles the GravatarRegistry
smart contract in order to deploy it to Ganache. A handful of transactions will be executed against the contract. This gives the subgraph the very first data to index.
5. Deploying to local Graph Node.
A) Adding the contract address
After you have copied the contract address, you can enter the root directory of your subgraph to deploy it to the local Graph Node. To do so, use this command:
B) Installing dependencies
Once the contract addresses deployed to the local Graph Node, you can install the dependencies of your subgraph. The second part of the command runs The Graph CLI code generation:
C) Allocating subgraph name
The next step is to allocate the subgraph name locally in the Graph Node:
D) Local deployment
The last step consists of deploying the subgraph to your local Graph Node. The logs that are being processed during the deployment can be found in the terminal output of the Graph Node.
6. Using the subgraph.
The final step of developing your subgraph in a local environment is to use it in a dApp build with React. To do so, open another terminal and select a sample dApp to clone. Make sure the decentralized application you clone is compatible with your subgraph.
Now you need to enter and configure the app directory so that it uses the GraphQL endpoint of the subgraph you created.
The last step is to install and start the dApp’s dependencies with this command: