Unlocking the Power of Blockchain Data with The Graph

The Graph is a revolutionary indexing protocol that enables easier access to blockchain data. By leveraging a GraphQL API, it provides a more efficient and streamlined way to query on-chain information compared to traditional methods.

Key Players in The Graph Ecosystem

The Graph ecosystem consists of three key players:

  • Indexers: Operate network nodes, indexing data and serving queries. They stake Graph Tokens (GRT) to provide indexing and query processing services.
  • Curators: Signal high-quality subgraphs, organizing data from the subgraphs and indicating which ones should be indexed by the Graph Network.
  • Delegators: Secure the network by staking GRT to one or more Indexers, earning portions of the Indexer’s query fees and rewards.

The Graph Foundation and Services

The Graph is developed and maintained by The Graph Foundation, which distributes grants to community members working on protocol infrastructure, tooling, dApps, subgraphs, and community building.

There are three ways to interact with The Graph:

  • Graph Explorer: Explore different subgraphs and interact with the Graph protocol.
  • Subgraph Studio: Create, manage, and publish subgraphs and API keys using Ethereum Mainnet.
  • Hosted Service: Create, manage, and publish subgraphs and API keys using other networks aside from Ethereum.

Creating a Project on Hosted Service

To get started with The Graph, create an account on the Hosted Service and set up a new project. This involves creating a subgraph, defining entities, generating types, and writing mappings.

Defining Entities and Subgraph

In the schema.graphql file, define two types: Token and User.

type Token {
  name: String!
  contentUri: String!
  ipfsFilePath: String!
}

type User {
  id: ID!
  tokensOwned: [Token!]!
  tokensCreated: [Token!]!
}

Generating Types and Writing Mappings

Generate AssemblyScript types for the ABI and subgraph schema. Write mappings to transform smart contract events into entities in the store.

// Generate AssemblyScript types
//...

// Write mappings
//...

Deploying the Subgraph

Build and deploy the project, including the subgraph and API endpoint. Wait for the subgraph to sync with the current state of the blockchain before running queries.

Querying the Subgraph

Use the API endpoint or HTTP endpoint to send GraphQL queries directly with cURL.

// Run a query to retrieve data
curl -X POST \
  https://api.example.com/subgraphs/namepace/subgraph \
  -H 'Content-Type: application/json' \
  -d '{"query": "query { tokens(orderBy: id_DESC, first: 2) { id name } }"}'

Run queries to retrieve data, such as the first two tokens ordered by ID in descending order or the first user and their associated content.

// Query example
query {
  tokens(orderBy: id_DESC, first: 2) {
    id
    name
  }
}

query {
  user(id: "1") {
    id
    tokensOwned {
      id
      name
    }
  }
}

Leave a Reply