You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/pages/en/developing/graph-ts/api.mdx
+20-11Lines changed: 20 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,16 @@
2
2
title: AssemblyScript API
3
3
---
4
4
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).
6
6
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:
8
8
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`.
-Code generated from subgraph files by `graph codegen`
11
11
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).
13
15
14
16
## API Reference
15
17
@@ -246,7 +248,9 @@ export function handleTransfer(event: TransferEvent): void {
246
248
247
249
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.
248
250
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.
250
254
251
255
#### Loading entities from the store
252
256
@@ -262,15 +266,18 @@ if (transfer == null) {
262
266
// Use the Transfer entity as before
263
267
```
264
268
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.
266
270
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.
268
272
269
273
#### Looking up entities created withing a block
270
274
271
275
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.
272
276
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.
274
281
275
282
```typescript
276
283
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
497
504
498
505
#### Handling Reverted Calls
499
506
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:
501
510
502
511
```typescript
503
512
let gravity =Gravity.bind(event.address)
@@ -509,7 +518,7 @@ if (callResult.reverted) {
509
518
}
510
519
```
511
520
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.
0 commit comments