The Graph
Text

What’s the Graph?

Lesson 1

The Graph is sometimes referred to as the Google of blockchains.  Like Google indexes search results, The Graph enables the indexing of blockchain data.  

It makes it possible to use the data on blockchains in ways that are difficult or impossible to directly ask the chain for.  Indexing the data allows complex queries to take place.

Many smart contracts will program specific types of queries into them that make getting specific data like an owner of a NFT, a balance of an account, or a total supply simple to retrieve.  For example, this function allows anyone to query its contract and get the admin stored in ADMIN_KEY.

export function getAdmin(): string {
     return storage.getSome<string>(ADMIN_KEY)
}

However, more complex queries that really make that chain data useful - things like aggregating the data, searching through the chain for specific types of information, discovering relationships or doing more complex filtering are not possible. On large chains, it may take days or weeks to do a search through the chain data to find something.  That kind of lag doesn't make for performant dapps or great user experience.

The NEAR explorer is an example of an app that relies on an indexer.

To address this data issue, many highly performant dapps run their own indexers to extract and prepare the data that is on a chain so that it can be brought into their dapp and used effectively.

These indexers are servers that process transactions, save them to a database and then have an API built on top of it to do the querying.  

While that's definitely one way to solve the challenge associated with efficiently querying the chain for data, it's also centralized and brings with it a high necessity for ongoing maintenance and technical know-how. Lots of things occur on chain that need to be taken into consideration which further add to the complexity of running an indexer like finality, chain reorgs, and uncled blocks.

In short, indexing is hard, but The Graph proves an effective way to make indexing and querying relatively simple and importantly does it in a decentralized manner.  It allows the creation of indexed subgraphs which are essentially open blockchain APIs that are defined to provide access to specific types of data on the chain.  They can then be queried using a standard GraphQL API.

Today, The Graph provides a Hosted Service as well as a decentralized protocol where these subgraphs are provided that are backed by an open source implementation of Graph Node.

How Does The Graph Work?

As of this writing, The Graph works with the Ethereum blockchain as well as the NEAR blockchain (limited beta).  Understanding The Graph is aiming to work across many different networks, from here on out, we'll just refer to a chain in a general sense (until we get to the NEAR specifics) to explain how it works.  The actual mechanics will differ somewhat depending on the chain.

The image and the stars in sequence describe how The Graph is works to index and provide data to an application.

Summarizing the flow, The Graph works using something known as a subgraph manifest to provide subgraph descriptions of what and how to index the chain data.  The subgraph description identifies which smart contracts are of interest, the events/receipts (handlers) that the graph needs to pay attention to in those contracts, and then how to map the data it gets when those events/receipts occur to data that The Graph will store in its database.

You can then use a tool like Online GraphiQL or have your applications make GraphQL queries against that database to retrieve and manipulate the stored data much more effectively than they ever could trying to get it directly off chain.  

If there's one thing to take away/understand about why The Graph is important to you in developing applications - it's that indexing and this data manipulation is what separates working/prototype applications from large scale production applications.  That is even more relevant if you're building some type of platform that allows hundreds or thousands of users to interact at the same time.

Next Steps

Now that you have a general understanding of what The Graph is, what it does, and why you should be interested in it as an application developer, we'll move on to take a quick look at it's network overview and how it achieves decentralization.  On to the next lesson.


Maybe you'd rather have us build your NEAR subgraph for you?

Not a problem and it can be pretty quick and inexpensive depending on the complexity of your contract.  Get in touch using one of the methods below.

Pen
>