Spark: The Graph Mini-Series (2/3) — Creating a Subgraph

Spark: The Graph Mini-Series (2/3) — Creating a Subgraph https://celo.academy/uploads/default/optimized/2X/9/997f046ffe6feac456b6250fb794b7039f991212_2_1024x576.png
none 0.0 0

We will learn how to create a subgraph using Celo Spark and The Graph. A subgraph acts as the data layer for your decentralized application, allowing you to efficiently retrieve and display data from smart contracts on the Celo blockchain. We will cover the theory behind subgraphs, explore the new file data sources feature, and walk through the process of creating a subgraph.

Introduction to The Graph and its Benefits

The Graph is the missing data layer of Web3. It allows developers to efficiently retrieve and display data from smart contracts on the blockchain. The Graph solves the pain point of slow and repetitive JSON-RPC calls by pre-processing the data and providing a decentralized GraphQL API. This enables faster and more user-friendly interactions with blockchain data.

Setting up the Development Environment

  1. Install Celo Spark and create a monorepo with two packages: one for the NFT smart contract (CeloBox) and another for the subgraph.

Creating the Subgraph

  1. Log in to The Graph’s Subgraph Studio.
  2. Create a new subgraph using the setup wizard.
  • Choose the Ethereum protocol.
  • Define a subgraph slug (e.g., “subcraft”).
  • Select the Celo Alpha chorus chain.
  • Copy the contract address from the Celo Block Explorer.
  1. Scaffold the subgraph by automatically generating the event handlers and database schema based on the events emitted by the smart contract.

Exploring the Subgraph Schema

  1. Open the subgraph in Subgraph Studio.
  2. Explore the graphical schema to understand the structure of the subgraph.
  • Click on the events (e.g., mint and transfer) to view the corresponding entities and their fields.
  • Note the information provided, such as contract name, address, start block, and APIs.

Using File Data Sources

  1. Understand the separation between deterministic and secure data from the blockchain and deterministic data from IPFS or other file data sources.
  2. Replace existing mappings with new mappings for tokens, token metadata, and users to leverage file data sources.
  3. Update the API reference to include the file data source.

Testing and Updating the Subgraph

  1. Build and deploy the updated subgraph locally.
  2. Verify that the file data sources are successfully retrieving data from IPFS.
  3. Explore the subgraph using the Subgraph Explorer to ensure that the changes have been applied correctly.

Congratulations! You have successfully created a subgraph using Celo Spark and The Graph, integrating file data sources and leveraging the power of GraphQL to efficiently retrieve data from the Celo blockchain. In the next part of this mini-series, we will learn how to build a frontend on top of the GraphQL API. Stay tuned for more exciting development with Celo Spark and The Graph!

4 Likes

Nice video. That was good enough.

1 Like

The video explains everything in detail

1 Like