Skip to content

Commit a27ae49

Browse files
idalithbbenface
andauthored
API-Edits (#781)
* Update website/pages/en/developing/graph-ts/api.mdx Co-authored-by: Benoît Rouleau <[email protected]>
1 parent 56d0d0c commit a27ae49

File tree

1 file changed

+20
-11
lines changed
  • website/pages/en/developing/graph-ts

1 file changed

+20
-11
lines changed

website/pages/en/developing/graph-ts/api.mdx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
title: AssemblyScript API
33
---
44

5-
> Note: if you created a subgraph prior to `graph-cli`/`graph-ts` version `0.22.0`, you're using an older version of AssemblyScript, we recommend taking a look at the [`Migration Guide`](/release-notes/assemblyscript-migration-guide)
5+
> Note: If you created a subgraph prior to `graph-cli`/`graph-ts` version `0.22.0`, then you're using an older version of AssemblyScript. It is recommended to review the [`Migration Guide`](/release-notes/assemblyscript-migration-guide).
66
7-
This page documents what built-in APIs can be used when writing subgraph mappings. Two kinds of APIs are available out of the box:
7+
Learn what built-in APIs can be used when writing subgraph mappings. There are two kinds of APIs available out of the box:
88

9-
- the [Graph TypeScript library](https://github.com/graphprotocol/graph-tooling/tree/main/packages/ts) (`graph-ts`) and
10-
- code generated from subgraph files by `graph codegen`.
9+
- The [Graph TypeScript library](https://github.com/graphprotocol/graph-tooling/tree/main/packages/ts) (`graph-ts`)
10+
- Code generated from subgraph files by `graph codegen`
1111

12-
It is also possible to add other libraries as dependencies, as long as they are compatible with [AssemblyScript](https://github.com/AssemblyScript/assemblyscript). Since this is the language mappings are written in, the [AssemblyScript wiki](https://github.com/AssemblyScript/assemblyscript/wiki) is a good source for language and standard library features.
12+
You can also add other libraries as dependencies, as long as they are compatible with [AssemblyScript](https://github.com/AssemblyScript/assemblyscript).
13+
14+
Since language mappings are written in AssemblyScript, it is useful to review the language and standard library features from the [AssemblyScript wiki](https://github.com/AssemblyScript/assemblyscript/wiki).
1315

1416
## API Reference
1517

@@ -246,7 +248,9 @@ export function handleTransfer(event: TransferEvent): void {
246248

247249
When a `Transfer` event is encountered while processing the chain, it is passed to the `handleTransfer` event handler using the generated `Transfer` type (aliased to `TransferEvent` here to avoid a naming conflict with the entity type). This type allows accessing data such as the event's parent transaction and its parameters.
248250

249-
Each entity must have a unique ID to avoid collisions with other entities. It is fairly common for event parameters to include a unique identifier that can be used. Note: Using the transaction hash as the ID assumes that no other events in the same transaction create entities with this hash as the ID.
251+
Each entity must have a unique ID to avoid collisions with other entities. It is fairly common for event parameters to include a unique identifier that can be used.
252+
253+
> Note: Using the transaction hash as the ID assumes that no other events in the same transaction create entities with this hash as the ID.
250254
251255
#### Loading entities from the store
252256

@@ -262,15 +266,18 @@ if (transfer == null) {
262266
// Use the Transfer entity as before
263267
```
264268

265-
As the entity may not exist in the store yet, the `load` method returns a value of type `Transfer | null`. It may thus be necessary to check for the `null` case before using the value.
269+
As the entity may not exist in the store yet, the `load` method returns a value of type `Transfer | null`. It may be necessary to check for the `null` case before using the value.
266270

267-
> **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.
271+
> 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.
268272
269273
#### Looking up entities created withing a block
270274

271275
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.
272276

273-
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.
277+
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.
278+
279+
- In the case where the transaction does not exist, the subgraph will have to go to the database simply 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.
280+
- For some subgraphs, these missed lookups can contribute significantly to the indexing time.
274281

275282
```typescript
276283
let id = event.transaction.hash // or however the ID is constructed
@@ -497,7 +504,9 @@ Any other contract that is part of the subgraph can be imported from the generat
497504

498505
#### Handling Reverted Calls
499506

500-
If the read-only methods of your contract may revert, then you should handle that by calling the generated contract method prefixed with `try_`. For example, the Gravity contract exposes the `gravatarToOwner` method. This code would be able to handle a revert in that method:
507+
If the read-only methods of your contract may revert, then you should handle that by calling the generated contract method prefixed with `try_`.
508+
509+
- For example, the Gravity contract exposes the `gravatarToOwner` method. This code would be able to handle a revert in that method:
501510

502511
```typescript
503512
let gravity = Gravity.bind(event.address)
@@ -509,7 +518,7 @@ if (callResult.reverted) {
509518
}
510519
```
511520

512-
Note that a Graph node connected to a Geth or Infura client may not detect all reverts, if you rely on this we recommend using a Graph node connected to a Parity client.
521+
> Note: A Graph node connected to a Geth or Infura client may not detect all reverts. If you rely on this, we recommend using a Graph Node connected to a Parity client.
513522
514523
#### Encoding/Decoding ABI
515524

0 commit comments

Comments
 (0)