QuickNode
Published in

QuickNode

Intro to The Graph: Indexed Data for dApps

What is The Graph, how to create a subgraph, and how does it help my dApp?

  • An Ethereum Node (Interface to the ETH network)

“dApps need indexed data to perform large-scale queries to enable a full-featured User Experience in a trust-minimized way”

Enter: The Graph.

What is The Graph?

The Graph team is solving this problem by building a decentralized protocol which will be enabled by Graph Nodes processing Ethereum events and storing them as indexed data which dApps can query through an API endpoint.

Graph Protocol falls into a category we refer to as a layer 2 read-scalability solution. Its purpose is to enable decentralized applications (dApps) to query public blockchain data efficiently and trustlessly via a service that, like blockchains and the Internet itself, operates as a public utility. This is in the interest of minimizing the role of brittle, centralized infrastructure seen in many “decentralized” application architectures today. — Graph Protocol Specification

These Indexes (“subgraphs”) are currently being hosted by The Graph team. But, in the future, these Indexes will live on fully decentralized network of Graph Nodes.

How does The Graph work?

Let's dig deeper and understand how The Graph actually works:

  • Graph Nodes scan every Ethereum block looking for events.
  • Graph Nodes find events for your subgraph in Ethereum blocks and runs the mapping handlers you provided. This mapping decides how data gets stored and updated in Graph Nodes.
  • Your dApp can query this data through GraphQL APIs, which get translated by Graph Nodes to fetch indexed data.

So, what are we waiting for? Let’s build a Subgraph!

Image source — TheGraph Docs

Build a Subgraph

Let’s use a sample smart contract with Truffle project and build a subgraph.

sudo yarn global add @graphprotocol/graph-cli
graph init buddies2705/example-subgraph example-subgraph
// Command Syntax
graph init <GITHUB_USER>/<SUBGRAPH_NAME> <DIRECTORY>// Here Direcotry is optional, if you don't pass it it will create a directory with SUBGRAPH_NAME
//Install Dependencies yarn//Generate Mappingsyarn codegen
graph auth https://api.thegraph.com/deploy/<ACCESS_TOKEN>
graph deploy     --debug     --node https://api.thegraph.com/deploy/     --ipfs https://api.thegraph.com/ipfs/     buddies2705/example
//You can see we are using buddies/example where "buddies2705" is my //github username and "example" is our subgraph name create using //Graph explorer UI.
Subgraph GraphQL API URLs

Under the Hood

Let’s go deeper and understand what happened “Under the Hood”. We had a Truffle project with a Gravity.sol contract, which is simply creating a Gravatar (your Avatar on the Internet) on Ethereum.

  • UpdatedGravatar — When an existing Gravatar gets updated
event NewGravatar(uint id, address owner, string displayName, string imageUrl);event UpdatedGravatar(uint id, address owner, string displayName, string imageUrl);
  • How many Gravatars get updated per day on average?
  • What is the top 10 image hosts for all our Gravatars? (it will be IPFS 😎)
  • What are the most common names for Gravatars?
Subgraph.yaml
type Gravatar @entity {
id: ID!
owner: Bytes!
displayName: String!
imageUrl: String!
}

Build a subgraph for you dApp

If you are running a dApp, you’ve probably faced this Data problem.

  • Create a subgraph.yaml
  • Create schema.graphql
  • Generate schema files
  • Create a Mapping file with the event handlers

We have built a basic FOMO3d subgraph, check it out!

Conclusion

The best property which people still do not still recognize is that Web3 Infrastructure will enable autonomous internet applications, which can sustain for foreseeable future without any maintenance.

Web3 — Build, Deploy, no need to Maintain

Decentralization and trust minimization is a difficult problem to solve. The Graph team is trying to do that and build an important part of dApp infrastructure. If you are building a scalable dApp you should look into The Graph protocol!

About QuikNode

QuikNode is building infrastructure to support the future of Web3. Since 2017, we’ve worked with hundreds of developers & companies, helping scale dApps and providing high-performance Ethereum nodes. We’re working on something interesting from the past few months and will be launching soon, so subscribe our newsletter for more updates!! 😃

--

--

A platform to access ETH, BTC, BSC, MATIC, OPTIMISM, ARBITRUM, FTM, CELO, SOL, TERRA & xDAI blockchains via API. Infra & tools to build + scale your #Web3 dApp!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store