The Graph: An open-source query protocol for blockchains, using GraphQL

Anyone who’s ever tried to build distributed applications (dApps) on the (Ethereum) blockchain would concur: Although blockchains are conceptually quite close to databases, querying databases feels like a different world entirely compared to querying blockchains.
First off, there are notable performance issues with storing data on blockchains. These have a lot to do with the distributed nature of blockchains, and the penalty imposed by the combination of consensus protocols and cryptography.
Databases would be slow, too, if they were comprised of a network of nodes in which every node kept a full copy of the entire database, and every transaction had to be verified by every node. This is why people have been experimenting with various approaches to use blockchains as a database, including altering blockchain structure. The Graph does something different: it lets blockchains be, but offers a way to index and query data stored on them efficiently using GraphQL.
Actually, performance is only part of the issue with retrieving data from blockchains. It gets worse: Blockchains have no query language to speak of. Just let that sink in for a moment: No query language. Imagine a database with no query language! How would you ever get what you need out of it? How do people build dApps, really? With a lot of effort, and brittle, ad-hoc code. As pointed out in this post by Jesus Rodriguez, blockchain data access is challenging mainly due to three fundamental reasons: Decentralization, Opacity, and Sequential Data Storage. So people are left with a few choices: Writing custom code to locate the data they need on blockchains, and either repeating those (expensive) calls every time they need the data, or retrieving the data once and storing in an off-chain database, and building an index to point to the original blockchain data.
This is where The Graph comes in. The Graph is a decentralized protocol for indexing and querying blockchain data. But it’s more than just a protocol: The Graph also has an implementation, which is open source and uses GraphQL. GraphQL is a query language for APIs, developed and open sourced by Facebook. GraphQL has taken a life of its own, it’s gaining in popularity and being used to access databases, too — see Prisma or FaunaDB, for example. ZDNet had a Q&A with The Graph’s co-founders, project lead Yaniv Tal and research lead Brandon Ramirez. In Tal’s words, right now, teams working on dApps have to write a ton of custom code and deploy proprietary indexing servers in order to efficiently serve applications. Because all of this code is custom there’s no way to verify that indexing was done correctly or outsource this computation to public infrastructure. By defining a standardized way of doing this indexing and serving queries deterministically, Tal went on to add, developers will be able to run their indexing logic on public open infrastructure where security can be enforced. The Graph have open sourced all their main components including: Graph Node (an implementation of an indexing node built in Rust), Graph TS (AssemblyScript helpers for building mappings), and Graph CLI (Command line tools for speeding up development).
As per Tal, the core of what The Graph have done is to define a deterministic way of doing indexing. Graph Node defines a store abstraction that they implement using Postgres:
The subgraph that Tal refers to here is simply a part of the blockchain used to store data for specific dApps. Defining a subgraph is the first step to use The Graph. Subgraphs for popular protocols and dApps are in use already, and can be browsed using the Graph Explorer, which provides a user interface to execute GraphQL queries against specific smart contracts or dApps. When The Graph was introduced in July 2018, Tal mentioned they would launch a local node, a hosted service, and then a fully decentralized network.


