Building & Deploying Subgraph (local node)
This tutorial will demonstrate how to build a subgraph and deploy it locally

Install the graph-cli

Build your own graph-indexer local node

Copy paste the below docker-compose file and replace services.graph-node.environment.ethereum accordingly to the network: mainnet : mainnet:no_eip1898,archive,traces:https://a.api.s0.t.hmny.io testnet : testnet:no_eip1898,archive,traces:https://api.s0.pops.one
mainnet
testnet
1
version: "3"
2
services:
3
graph-node:
4
container_name: hmy_indexer
5
image: graphprotocol/graph-node:v0.23.1
6
ports:
7
- "8000:8000"
8
- "8001:8001"
9
- "8020:8020"
10
- "8030:8030"
11
- "8040:8040"
12
depends_on:
13
- ipfs
14
- postgres
15
environment:
16
postgres_host: postgres
17
postgres_user: graph-node
18
postgres_pass: let-me-in
19
postgres_db: graph-node
20
ipfs: "ipfs:5001"
21
GRAPH_ETH_CALL_BY_NUMBER: 1
22
GRAPH_NO_EIP_1898_SUPPORT: 1
23
GRAPH_ALLOW_NON_DETERMINISTIC_IPFS: 1
24
ethereum: 'mainnet:no_eip1898,archive,traces:https://a.api.s0.t.hmny.io"
25
RUST_LOG: info
26
ipfs:
27
container_name: ipfs
28
image: ipfs/go-ipfs:v0.4.23
29
ports:
30
- "5001:5001"
31
volumes:
32
- ./data/ipfs:/data/ipfs
33
postgres:
34
image: postgres
35
ports:
36
- "5432:5432"
37
command: ["postgres", "-cshared_preload_libraries=pg_stat_statements"]
38
environment:
39
POSTGRES_USER: graph-node
40
POSTGRES_PASSWORD: let-me-in
41
POSTGRES_DB: graph-node
42
volumes:
43
- ./data/postgres:/var/lib/postgresql/data
Copied!
1
version: "3"
2
services:
3
graph-node:
4
container_name: hmy_indexer
5
image: graphprotocol/graph-node:0.23.1
6
ports:
7
- "8000:8000"
8
- "8001:8001"
9
- "8020:8020"
10
- "8030:8030"
11
- "8040:8040"
12
depends_on:
13
- ipfs
14
- postgres
15
environment:
16
postgres_host: postgres
17
postgres_user: graph-node
18
postgres_pass: let-me-in
19
postgres_db: graph-node
20
ipfs: "ipfs:5001"
21
GRAPH_ETH_CALL_BY_NUMBER: 1
22
GRAPH_NO_EIP_1898_SUPPORT: 1
23
GRAPH_ALLOW_NON_DETERMINISTIC_IPFS: 1
24
ethereum: 'testnet,archive,traces:https://api.s0.backup1.b.hmny.io"
25
RUST_LOG: info
26
ipfs:
27
container_name: ipfs
28
image: ipfs/go-ipfs:v0.4.23
29
ports:
30
- "5001:5001"
31
volumes:
32
- ./data/ipfs:/data/ipfs
33
postgres:
34
image: postgres
35
ports:
36
- "5432:5432"
37
command: ["postgres", "-cshared_preload_libraries=pg_stat_statements"]
38
environment:
39
POSTGRES_USER: graph-node
40
POSTGRES_PASSWORD: let-me-in
41
POSTGRES_DB: graph-node
42
volumes:
43
- ./data/postgres:/var/lib/postgresql/data
Copied!
and run your indexer node
1
docker-compose up -d
2
Creating network "thegraph_default" with the default driver
3
Creating ipfs ...
4
Creating postgres ...
5
Creating postgres done
6
Creating ipfs ... done
7
Creating indexer ...
8
Creating indexer ... done
Copied!
docker logs indexer -f should show the indexer synching with the harmony chain
1
docker logs indexer -f
2
Jul 29 05:33:22.849 INFO Graph Node version: 0.23.1 (2021-06-23)
3
Jul 29 05:33:22.861 INFO Generating configuration from command line arguments
4
Jul 29 05:33:22.909 INFO Starting up
5
Jul 29 05:33:22.911 INFO Trying IPFS node at: http://ipfs:5001/
6
Jul 29 05:33:22.928 INFO Creating transport, capabilities: archive, traces, url: https://api.s0.pops.one, provider: mainnet-rpc-0
7
Jul 29 05:33:22.948 INFO Successfully connected to IPFS node at: http://ipfs:5001/
8
Jul 29 05:33:23.680 INFO Connecting to Postgres, weight: 1, conn_pool_size: 10, url: postgresql://graph-node:[email protected]:5432/graph-node, pool: main, shard: primary
9
Jul 29 05:33:23.716 INFO Pool successfully connected to Postgres, pool: main, shard: primary, component: Store
10
Jul 29 05:33:23.721 INFO Setting up fdw, pool: main, shard: primary, component: ConnectionPool
11
Jul 29 05:33:23.728 INFO Running migrations, pool: main, shard: primary, component: ConnectionPool
12
Jul 29 05:33:24.748 INFO Migrations finished, pool: main, shard: primary, component: ConnectionPool
13
Jul 29 05:33:24.748 INFO Mapping primary, pool: main, shard: primary, component: ConnectionPool
14
Jul 29 05:33:24.767 INFO Connecting to Ethereum to get network identifier, capabilities: archive, traces, provider: mainnet-rpc-0
15
Jul 29 05:33:25.030 INFO Connected to Ethereum, capabilities: archive, traces, network_version: 0x6357d2e0, provider: mainnet-rpc-0
16
Jul 29 05:33:25.117 INFO Creating LoadManager in disabled mode, component: LoadManager
17
Jul 29 05:33:25.119 INFO Starting block ingestors
18
Jul 29 05:33:25.119 INFO Starting block ingestor for network, network_name: mainnet
19
Jul 29 05:33:25.120 INFO Starting job runner with 1 jobs, component: JobRunner
20
Jul 29 05:33:25.126 INFO Starting JSON-RPC admin server at: http://localhost:8020, component: JsonRpcServer
21
Jul 29 05:33:25.130 INFO Starting GraphQL HTTP server at: http://localhost:8000, component: GraphQLServer
22
Jul 29 05:33:25.130 INFO Starting index node server at: http://localhost:8030, component: IndexNodeServer
23
Jul 29 05:33:25.130 INFO Started all subgraphs, component: SubgraphRegistrar
24
Jul 29 05:33:25.130 INFO Starting metrics server at: http://localhost:8040, component: MetricsServer
25
Jul 29 05:33:25.131 INFO Starting GraphQL WebSocket server at: ws://localhost:8001, component: SubscriptionServer
26
Jul 29 05:33:25.258 INFO Downloading latest blocks from Ethereum. This may take a few minutes..., provider: mainnet-rpc-0, component: BlockIngestor
27
Jul 29 05:33:33.600 INFO Syncing 4 blocks from Ethereum., code: BlockIngestionStatus, blocks_needed: 4, blocks_behind: 4, latest_block_head: 12886125, current_block_head: 12886121, provider: mainnet-rpc-0, component: BlockIngestor
28
Jul 29 05:33:35.252 INFO Syncing 1 blocks from Ethereum., code: BlockIngestionStatus, blocks_needed: 1, blocks_behind: 1, latest_block_head: 12886126, current_block_head: 12886125, provider: mainnet-rpc-0, component: BlockIngestor
Copied!
A few component are installed
Management: https://localhost:8020/ where subgraph are being created/deployed/deleted
Metrics / playground: https://localhost:8030/​
Vvisit your playground using the URL http://127.0.0.1:8030/graphql/playground and start playing around with graphQL API.
An example of query you can use to show subgraph currently being indexed would be :
1
query {
2
indexingStatuses {
3
subgraph
4
entityCount
5
fatalError {
6
handler
7
}
8
nonFatalErrors {
9
handler
10
}
11
chains {
12
network
13
latestBlock {
14
number
15
}
16
chainHeadBlock {
17
number
18
}
19
lastHealthyBlock {
20
number
21
}
22
}
23
}
24
}
Copied!
Right now of course, the result is empty
1
{
2
"data": {
3
"indexingStatuses": []
4
}
5
}
Copied!

Our first subgraph

lets use blocklytics/ethereum-blocks subgraph as example
1
git clone https://github.com/blocklytics/ethereum-blocks
2
cd ethereum-blocks
Copied!

Edit package.json file and add these lines

1
"create-harmony": "graph create --node http://localhost:8020 harmony/blocks",
2
"deploy-harmony": "graph deploy --node http://localhost:8020 --ipfs http://localhost:5001 harmony/blocks",
Copied!
It is highly recommended to minimize the number of blocks to be indexed to avoid putting load on the RPCs and to speed up the usage of your subgraph/application

Update the manifest

Update the manifest subgraph.yaml file datasources[0]['network'] from rinkeby to mainnet or testnet accordingly to how you edited the network in your docker-compose.yaml if there is no need to index the entire blockchain, you can add an attribute startBlock to speed up the sync : datasources[0]['source']['startBlock']
It is highly recommended to minimize the number of blocks to be indexed to avoid putting load on the RPCs and to speed up the usage of your subgraph/application

Create and deploy the subgraph

1
yarn codegen
2
yarn build
3
yarn create-harmony
4
yarn deploy-harmony
Copied!
Sync begins and you are good to query your subgraph http://localhost:8000/subgraphs/name/harmony/blocks/graphql​
Note that the above example query should now show your something similar to the below
1
{
2
"data": {
3
"indexingStatuses": [
4
{
5
"chains": [
6
{
7
"chainHeadBlock": {
8
"number": "12887470"
9
},
10
"lastHealthyBlock": null,
11
"latestBlock": {
12
"number": "10701810"
13
},
14
"network": "testnet"
15
}
16
],
17
"entityCount": "1812",
18
"fatalError": null,
19
"nonFatalErrors": [],
20
"subgraph": "QmVDqqTJZM3ZHizSubG2H5b1vQ1Jp7iMD6Sc94A9zyRnAK"
21
}
22
]
23
}
24
}
Copied!
data['indexingStatuses'][0]['chains'][0]['latestBlock']['number'] will indicate the last block synched
check your indexer log if the sync is stuck docker logs indexer -f --since 1m

How to write subgraphs

Official Graph's doc

Community article

Getting help

the Graph’s Discord server https://discord.gg/vtvv7FP​
Last modified 1mo ago