Overview
The Graph team have prepared a highly informative video featuring an overview of the Graph protocol along with some hands-on examples you can try here. This video covers everything you need to know to build support for The Graph into your full stack application, and create subgraphs to query the relevant data.
The Graph is a decentralized protocol for indexing and querying blockchain data that makes it possible to query historical data that is difficult to query from smart contracts directly.
Projects with complex smart contracts such as Uniswap and NFT projects like Bored Ape Yacht Club store data on the Ethereum blockchain, making it difficult to read anything but the most basic information.
In the case of Bored Ape Yacht Club, it's possible to perform basic read operations on the contract such as getting the owner of a certain Ape, getting the content URI of an Ape based on their ID, or the total supply, as these read operations are programmed directly into the smart contract. More advanced real-world queries and operations like aggregation, search, relationships, and non-trivial filtering however are not possible. For example, if we would like to query all Apes owned by a certain address and filter results by one of its characteristics, we cannot rely on the contract itself; it cannot provide historical information.
To obtain this kind of data, we would need to process every transfer event ever emitted from the BAYC smart contract, read the metadata from IPFS using the Token ID and IPFS hash, and then aggregate all the results. Put simply, to answer these kinds of (relatively) simple questions, it would take hours or even days for a decentralized application running in a browser to obtain a result, which is why The Graph exists - to make these kinds of queries simpler.
How do developers integrate The Graph?
Developers can either run a standalone Graph node and define their own subgraph(s), or choose a subgraph that is publicly available on the Graph's decentralized network. The Graph processes subgraphs and makes the data accessible through GraphQL, a popular query language.
Subgraph Overview
A subgraph is essentially a definition created by a developer that specifies which data The Graph should index from the blockchain, and how it should be stored.
To define a subgraph, developers write a manifest (in YAML format) and a set of GraphQL schemas. This tells The Graph which events to listen for on the blockchain, and how to map event data to entities that can be queried using GraphQL. Since developers define their own subgraphs, they have considerable flexibility in the way they index and expose blockchain data to their applications.
Prerequisites for using Subgraphs
- An account and API key on Subgraph Studio if using The Graph decentralized network, and balance of GRT tokens for queries.
- A standalone Graph node requires the following:
- Docker: Containerization platform for software solutions.
docker-compose
: Used to automate interactions between docker containers.- JQ: JSON processor for graph requests.
- Before exploring the contents of the subgraph manifest file and building and deploying subgraphs, install the Graph CLI.
In this guide, we will walk through setting up an Astar node to gain more insight into transactions on the blockchain by providing indexing data to a standalone Graph node.
Running a standalone Graph Node
After successfully setting up an RPC node, the Graph node needs to be installed and configured to connect as a different container or virtual machine. If you are running a self-signed RPC node, you will need to set up an extra environment variable to allow for this, or run the Graph node on a different host.
The first step is to clone the Graph Node repository:
git clone [https://github.com/graphprotocol/graph-node/](https://github.com/graphprotocol/graph-node/) \\
&& cd graph-node/docker
Next, execute the setup.sh
file. This will pull all the necessary Docker images and write the necessary information to the docker-compose.yml
file. Ensure that docker-compose
and jq
are installed.
sudo bash ./setup.sh
After running the command, the tail end of the output should be similar to that shown below:
Modifying the Ethereum Environment
Once everything is set up, you will need to modify the "Ethereum environment" inside the docker-compose.yml
file, so that the Graph node points to the endpoint of the RPC that you are connecting with. Note that the setup.sh
file detects the RPC's Host IP and writes its value, so you'll need to modify it accordingly.