Skip to content

Commit 91d252f

Browse files
authored
docs: load in block (#366)
1 parent 25bad0e commit 91d252f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

website/pages/en/developing/assemblyscript-api.mdx

+18
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,24 @@ As the entity may not exist in the store yet, the `load` method returns a value
280280

281281
> **Note:** Loading entities is only necessary if the changes made in the mapping depend on the previous data of an entity. See the next section for the two ways of updating existing entities.
282282
283+
#### Looking up entities created withing a block
284+
285+
As of `graph-node` v0.31.0, `@graphprotocol/graph-ts` v0.30.0 and `@graphprotocol/graph-cli` v0.49.0 the `loadInBlock` method is available on all entity types.
286+
287+
The store API facilitates the retrieval of entities that were created or updated in the current block. A typical situation for this is that one handler creates a Transaction from some on-chain event, and a later handler wants to access this transaction if it exists. In the case where the transaction does not exist, the subgraph will have to go to the database just to find out that the entity does not exist; if the subgraph author already knows that the entity must have been created in the same block, using loadInBlock avoids this database roundtrip. For some subgraphs, these missed lookups can contribute significantly to the indexing time.
288+
289+
```typescript
290+
let id = event.transaction.hash // or however the ID is constructed
291+
let transfer = Transfer.loadInBlock(id)
292+
if (transfer == null) {
293+
transfer = new Transfer(id)
294+
}
295+
296+
// Use the Transfer entity as before
297+
```
298+
299+
> Note: If there is no entity created in the given block, `loadInBlock` will return `null` even if there is an entity with the given ID in the store.
300+
283301
#### Updating existing entities
284302

285303
There are two ways to update an existing entity:

0 commit comments

Comments
 (0)