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
NOTE When choosing between NPM and Yarn for your project, it's generally recommended to stick with the package manager you started with. If you initially used NPM, it's best to continue using NPM for any future package installations. Likewise, if you began with Yarn, it's best to stick with Yarn for future package installations. This helps to maintain consistency and avoid potential conflicts between the two package managers.
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:
localhost
New RPC URL:
http://localhost:8545
Chain ID:
31337
Currency Symbol:
ETH
NOTE
Use the recommended accounts to avoid errors.
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-node
folder running the docker, presscrtl + c
and typedocker-compose down
, then delete the data folderrm -rf /data
Update the smart contract and compile it
Update the files
web3task-subgraph/networks.json
and theweb3task-subgraph/subgraph.yaml
with the new addressCopy and paste the new ABI on
web3task-subgraph/abis/TasksManager.json
Update the file
web3task-subgraph/schema.graphql
with the new entitiesUpdate the file
web3task-subgraph/subgraph.ymal
with the new event handlersUpdate the file
web3task-subgraph/src/tasks-manager.ts
with the new functions and importsRun the commands
graph codegen
andgraph build --network localhost
in theweb3task-subgraph
folder.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-subgraph
Run the commands
graph codegen && graph build
to generate the necessary filesRun the command
graph deploy --studio web3task-subgraph
to 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