Why The Graph exists — indexing deep dive
EVM storage contains no ORDER BY or query features. How to compile smart contract events into instantly queryable GraphQL endpoints.

If you are a web developer coming from MySQL or MongoDB, you are used to writing queries like this:
When you transition to smart contract development, you hit a massive wall: Solidity has no query engine.
Smart contract storage is a key-value store optimized for state transitions, not reading. There is no ORDER BY, no LIMIT, and no text indexing.
If you want to render a home feed of posts from the users you follow, your frontend would have to fetch all posts from the blockchain and sort/filter them in memory. This is completely unscalable.
This is why The Graph was invented.
1. The EVM Query Limitation
Let's look at how posts are stored inside PostContract.sol:
If your frontend wants to show "the latest 10 posts uploaded by Alice," the only native way to do it is:
- Query
totalPoststo find the highest index. - Run a loop in React fetching every post one-by-one (
posts[1],posts[2], etc.). - Filter out posts where
author != Alice. - Sort by
timestamp.
If there are 50,000 posts in the system, this process requires 50,000 RPC requests, crashing the user's browser.
2. How The Graph Solves the Read Problem
Instead of reading contract storage directly, The Graph monitors blockchain events and compiles them into a structured database.
3. The Subgraph Specification (subgraph.yaml)
To index Socio3, we configure a subgraph mapping to listen for the PostCreated event on Polygon Amoy:
4. The Mapping Handler (mapping.ts)
When a PostCreated event occurs, the Graph node executes an AssemblyScript function to update its local database:
5. Querying the Indexer
Now, the client can query Alice's posts using standard GraphQL:
The Graph processes this query in under 50ms, returning exactly the 10 posts Alice created.
The Graph is a READ cache. It does not replace the blockchain. Write actions (sending tokens, creating posts) must still go through smart contract transactions. The Graph is strictly for indexing those actions after they occur.
If a user unfollows another user in Socio3, how does The Graph update the social graph? Sketch the schema and handler mappings required to delete or update a follow relation entity.
Was this lesson helpful?
Let us know what you think of this specification. (submitting anonymously)
