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
- 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
- Log in to The Graph’s Subgraph Studio.
- 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.
- 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
- Open the subgraph in Subgraph Studio.
- 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
- Understand the separation between deterministic and secure data from the blockchain and deterministic data from IPFS or other file data sources.
- Replace existing mappings with new mappings for tokens, token metadata, and users to leverage file data sources.
- Update the API reference to include the file data source.
Testing and Updating the Subgraph
- Build and deploy the updated subgraph locally.
- Verify that the file data sources are successfully retrieving data from IPFS.
- 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!