Step-by-Step Guide: Setting Up a Self-Hosted Node on The Graph Protocol
As discussed in a previous article (Efficiently Querying Blockchain Data), The Graph operates in two distinct modes: decentralized and self-hosted. In the decentralized mode, previously explored, deploying a subgraph to The Graph Studio and adhering to the protocol’s tokenomics is the only concern. Although this approach offers ease of implementation, it may not provide the necessary flexibility for more complex projects, such as direct interaction with IPFS or support for chains other than Ethereum, Goerli, and Gnosis.
On the other hand, Self-hosted mode supports a broader range of chains, including Polygon, Near, BNB, and more. In this mode, one must manage its Graph node, which requires a Postgres database and IPFS connection.
In this article, we will guide you through the process of setting up the entire infrastructure with a simple docker-compose configuration. It’s important to note that this setup is intended for testing purposes only. For a production environment, scalability and resilience of your infrastructure must be taken into consideration for a robust and secure solution.
Get Started with a self-hosted node
Requirements
- Acquire a Node on the Target Chain or Subscribe to a Service Provider such as Chainstack, Alchemy, Infura, etc.
- Install Docker on Your System
- Download the Graph-Node Repository ```git clone https://github.com/graphprotocol/graph-node.git```
- Obtain the TheGraph-Simple-Blog Repository ```git clone https://github.com/kchain-solutions/thegraph-simple-blog.git```
- Install NodeJS
Run the node
After you have cloned the repository:
- Change your terminal to the “docker” directory.
- In the “docker-compose.yml” file, modify the Ethereum property with your target node endpoint (ex goerli:https://goerli.infura.io/v3/API-KEY). Note: The example uses Goerli for compatibility with the Prior Tutorial, But any other EVM Chain such as Mumbai can be used.”
- Run: docker-compose up -d
version: '3'
services:
graph-node:
image: graphprotocol/graph-node
ports:
- '8000:8000'
- '8001:8001'
- '8020:8020'
- '8030:8030'
- '8040:8040'
depends_on:
- ipfs
- postgres
extra_hosts:
- host.docker.internal:host-gateway
environment:
postgres_host: postgres
postgres_user: graph-node
postgres_pass: let-me-in
postgres_db: graph-node
ipfs: 'ipfs:5001'
ethereum: 'goerli:https://goerli.infura.io/v3/<<APIKEY>>'
GRAPH_LOG: info
ipfs:
image: ipfs/go-ipfs:v0.10.0
ports:
- '5001:5001'
volumes:
- ./data/ipfs:/data/ipfs
postgres:
image: postgres
ports:
- '5432:5432'
command:
[
"postgres",
"-cshared_preload_libraries=pg_stat_statements"
]
environment:
POSTGRES_USER: graph-node
POSTGRES_PASSWORD: let-me-in
POSTGRES_DB: graph-node
# FIXME: remove this env. var. which we shouldn't need. Introduced by
# <https://github.com/graphprotocol/graph-node/pull/3511>, maybe as a
# workaround for https://github.com/docker/for-mac/issues/6270?
PGDATA: "/var/lib/postgresql/data"
POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C"
volumes:
- ./data/postgres:/var/lib/postgresql/data
Testing the node deploying a Subgraph
For testing purposes, refer to the Previous Tutorial, but replacing the “npm deploy” command with “npm create-local” and “npm deploy-local”.
Here is the command sequence to deploy thegraph-simple-blog to the local node.
cd thegraph-simple-blog
npm install
npm run codegen
npm run create-deploy
nmp run deploy-local
when the deployment is successful we can open the indicated link and test with a query to verify that everything worked correctly.