About The Graph

Reading time: 4 min

What is The Graph?

Link to this section

The Graph is a powerful decentralized protocol that enables seamless querying and indexing of blockchain data. It simplifies the complex process of querying blockchain data, making dapp development faster and easier.

Understanding the Basics

Link to this section

Projects with complex smart contracts such as Uniswap and NFTs initiatives like Bored Ape Yacht Club store data on the Ethereum blockchain, making it very difficult to read anything other than basic data directly from the blockchain.

Challenges Without The Graph

Link to this section

In the case of the example listed above, Bored Ape Yacht Club, you can perform basic read operations on the contract. You can read the owner of a certain Ape, read the content URI of an Ape based on their ID, or read the total supply.

Why is this a problem?

Link to this section

It would take hours or even days for a decentralized application (dapp) running in a browser to get an answer to these simple questions.

Alternatively, you have the option to set up your own server, process the transactions, store them in a database, and create an API endpoint to query the data. However, this option is resource intensive, needs maintenance, presents a single point of failure, and breaks important security properties required for decentralization.

Blockchain properties, such as finality, chain reorganizations, and uncled blocks, add complexity to the process, making it time-consuming and conceptually challenging to retrieve accurate query results from blockchain data.

The Graph Provides a Solution

Link to this section

The Graph solves this challenge with a decentralized protocol that indexes and enables the efficient and high-performance querying of blockchain data. These APIs (indexed "subgraphs") can then be queried with a standard GraphQL API.

Today, there is a decentralized protocol that is backed by the open source implementation of Graph Node that enables this process.

How The Graph Functions

Link to this section

Indexing blockchain data is very difficult, but The Graph makes it easy. The Graph learns how to index Ethereum data by using subgraphs. Subgraphs are custom APIs built on blockchain data that extract data from a blockchain, processes it, and stores it so that it can be seamlessly queried via GraphQL.

  • The Graph uses subgraph descriptions, which are known as the subgraph manifest inside the subgraph.

  • The subgraph description outlines the smart contracts of interest for a subgraph, the events within those contracts to focus on, and how to map event data to the data that The Graph will store in its database.

  • When creating a subgraph, you need to write a subgraph manifest.

  • After writing the subgraph manifest, you can use the Graph CLI to store the definition in IPFS and instruct an Indexer to start indexing data for that subgraph.

The diagram below provides more detailed information about the flow of data after a subgraph manifest has been deployed with Ethereum transactions.

A graphic explaining how The Graph uses Graph Node to serve queries to data consumers

The flow follows these steps:

  1. A dapp adds data to Ethereum through a transaction on a smart contract.
  2. The smart contract emits one or more events while processing the transaction.
  3. Graph Node continually scans Ethereum for new blocks and the data for your subgraph they may contain.
  4. Graph Node finds Ethereum events for your subgraph in these blocks and runs the mapping handlers you provided. The mapping is a WASM module that creates or updates the data entities that Graph Node stores in response to Ethereum events.
  5. The dapp queries the Graph Node for data indexed from the blockchain, using the node's GraphQL endpoint. The Graph Node in turn translates the GraphQL queries into queries for its underlying data store in order to fetch this data, making use of the store's indexing capabilities. The dapp displays this data in a rich UI for end-users, which they use to issue new transactions on Ethereum. The cycle repeats.

The following sections provide a more in-depth look at subgraphs, their deployment and data querying.

Before you write your own subgraph, it's recommended to explore Graph Explorer and review some of the already deployed subgraphs. Each subgraph's page includes a GraphQL playground, allowing you to query its data.

Edit page

Previous
Get Started
Next
Overview
Edit page