the-graph
The Graph
The Graph is a decentralized indexing protocol for querying blockchain data. Subgraphs define which smart contract events to index, how to transform them into entities, and expose them via a GraphQL API. The protocol supports Ethereum, Arbitrum, Optimism, Base, Polygon, Avalanche, BSC, Celo, Gnosis, and 40+ other networks.
What You Probably Got Wrong
-
Hosted service is DEPRECATED -- do not use
graph deploy --node https://api.thegraph.com/deploy/. Use Subgraph Studio exclusively. Hosted service endpoints stopped serving queries in Q2 2024. All documentation referencing--node https://api.thegraph.com/deploy/is outdated. -
Mappings are AssemblyScript, NOT TypeScript -- despite
.tsfile extensions, subgraph mappings compile to WebAssembly via AssemblyScript. This means: no closures, no union types, no optional chaining (?.), no nullish coalescing (??), noArray.map/filter/reduce, noJSON.parse, no async/await, no try/catch. If you write standard TypeScript, the build will fail with cryptic errors. -
graph-tstypes are NOT standard TS types --BigInt,BigDecimal,Bytes,Address, andethereum.Eventcome from@graphprotocol/graph-ts. They are NOTbigint,number, orUint8Array. You must useBigInt.fromI32(),BigDecimal.fromString(), andAddress.fromString()constructors. Arithmetic uses method calls:a.plus(b),a.minus(b),a.times(b),a.div(b). -
graph codegenmust run before build -- entities and contract bindings are auto-generated fromschema.graphqland ABIs. If you skip codegen, imports likeimport { Transfer } from '../generated/ERC20/ERC20'will fail. Always rungraph codegenafter ANY change to schema or ABIs. -
Entity IDs must be
BytesorString, not numeric -- the@entitydirective requires anidfield of typeID!which maps toBytesorStringin AssemblyScript. UsingBigIntorIntas entity ID causes schema validation failure. -
store.getreturns nullable --Entity.load(id)returnsEntity | null. You must null-check before accessing fields. AssemblyScript does not have optional chaining, so you need explicitif (entity != null)blocks. -
Subgraph Studio requires authentication per machine --
graph auth --studio <deploy-key>stores the key in~/.graph. This is per-machine, not per-project. CI/CD must re-auth on each run.