Subgraph
Introduction
Web3Task utilizes a Subgraph that can be deployed on the local network or executed in the front end to fetch data quickly and effectively.
Running subgraph locally
Prerequisites
Install dependencies
Guide to install docker and Nodejs
Install dependencies inside the web3task-contracts directory
web3task-contracts directoryNPM or Yarn to install the graph-cli
Setting Up Local Blockchain
Run a Hardhat node:
The node will generate some accounts. You can realize they are already set at .env.sample, you should just let it be.
Add the first one to Metamask to be the leader and the second to be the member. The account will be:
To add the localhost network to metamask, click on the network dropdown and select Custom RPC. Fill in the following fields:
Network Name:
localhostNew RPC URL:
http://localhost:8545Chain ID:
31337Currency Symbol:
ETH
Deploying Smart Contracts in the Localhost
Makefile will set everything for us, just run:
Installing dependencies of subgraph
Running docker
Go to the graph-node directory
Make sure there is not any residual file remaining in the directory
start the docker
You should see the log looking like this for the docker:
And looking something like this for hardhat:
Create a subgraph
Deploy the subgraph
The Graph Explorer
IMPORTANT
You must run the test scripts on the local network to generate events to check on The Graph Explorer with queries. In case that step is skipped, it won't be possible to fetch data from the subgraph.
Query Examples
Below are some sample queries you can use to gather information from the Web3Task contracts.
You can build your own queries using GraphQL Explorer to test it out and query exactly what you need.
Updated tasks
Retrieve all tasks that have been updated.
Querying Authorized Personnel for a Specific Role
Fetch all personnel authorized for a specific role.
All approvals
Fetch all approvals.
In case you update the smart contract with new events to be added
Cancel and clean the docker. In the
graph-nodefolder running the docker, presscrtl + cand typedocker-compose down, then delete the data folderrm -rf /dataUpdate the smart contract and compile it
Update the files
web3task-subgraph/networks.jsonand theweb3task-subgraph/subgraph.yamlwith the new addressCopy and paste the new ABI on
web3task-subgraph/abis/TasksManager.jsonUpdate the file
web3task-subgraph/schema.graphqlwith the new entitiesUpdate the file
web3task-subgraph/subgraph.ymalwith the new event handlersUpdate the file
web3task-subgraph/src/tasks-manager.tswith the new functions and importsRun the commands
graph codegenandgraph build --network localhostin theweb3task-subgraphfolder.Deploy the new version of the subgraph
graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 subgraph/web3task
Deploying to Tesnet and Mainnet
Access the studio https://thegraph.com/studio/, connect your wallet and create a subgraph
Give a name and select a network
Run the command line:
graph init --studio <your subgraph name>Choose the protocol you want to deploy the subgraph
Create the slug which will be the unique identifier your subgraph will be identified by
Create the name of the directory that will have the subgraph files
Choose the network you want to deploy the subgraph
Fill the address field with the smart contract deployed on the network
Set the path to the ABI file
Fill the block in which the contract was deployed to
Give the name of the contract
Press "Y" to index events as entities
Press "n" if you don't want to add another contract.
run the command
graph auth --studio <your subgraph deploy key>Go to the subgraph's directory
cd web3task-subgraphRun the commands
graph codegen && graph buildto generate the necessary filesRun the command
graph deploy --studio web3task-subgraphto deploy the subgraph
Use The Graph Studio to play around with the queries. You can use the query mentioned earlier to start.
Executing Subgraph in the frontend
Prerequisites
Node service.js
Install dependencies
Install dependencies inside the web3task-frontend directory
web3task-frontend directoryExecute the subgraph service
This will execute an example query that will retrieve data from the Web3Task smart contract deployed to the Mumbai testnet.
To test other queries, you can follow the previous examples by editing the file web3task-frontend/src/services/subgraph-queries.ts
Last updated