What is a subgraph?
- Author
- CRYPTO PORT Editorial
- Published
- Updated
- Reading time
- 4 min
In short
A subgraph is a definition of which contracts' events to watch and what shape the resulting data should take. Registering one triggers a replay from past blocks to build the queryable dataset. Changing the definition means rebuilding, and until that finishes queries return the old shape.
Key points
- Defines which contracts and events to index, and the shape
- Registering it replays past blocks to build the dataset
- A flawed definition yields flawed aggregates
- After a change, old results stand until the rebuild ends
Definition
A definition describing what to index, how to extract it, and the data structures to store. Blocks are replayed according to it to produce a queryable dataset.
Contracts emit event logs as they work — who sent what to whom, which pool a swap went through. A subgraph declares which of those to pick up and how to organise them into fields. Thinking of it as a database schema is close enough.
Once registered, logs are replayed from the chosen starting block to build the dataset. For a busy contract that initial build takes time, and the application has to wait for it before querying.
In practice the thing to watch is that the definition determines the numbers. Miss an event, or mishandle a unit, and the figures on screen quietly drift. Many bugs where the chain is right but the display is wrong originate in this layer.
Watch out for
- · A wrong definition skews the display even when the chain is correct
- · During an initial build or a rebuild, the latest state is not reflected
- · If the contract is replaced, an unchanged definition stops picking up its events