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
+16-9Lines changed: 16 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,12 @@
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
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
12
You can also add other libraries as dependencies, as long as they are compatible with [AssemblyScript](https://github.com/AssemblyScript/assemblyscript).
13
13
@@ -248,7 +248,9 @@ export function handleTransfer(event: TransferEvent): void {
248
248
249
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.
250
250
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. 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.
252
254
253
255
#### Loading entities from the store
254
256
@@ -264,15 +266,18 @@ if (transfer == null) {
264
266
// Use the Transfer entity as before
265
267
```
266
268
267
-
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.
268
270
269
-
> **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.
270
272
271
273
#### Looking up entities created withing a block
272
274
273
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.
274
276
275
-
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.
276
281
277
282
```typescript
278
283
let id =event.transaction.hash// or however the ID is constructed
@@ -499,7 +504,9 @@ Any other contract that is part of the subgraph can be imported from the generat
499
504
500
505
#### Handling Reverted Calls
501
506
502
-
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:
503
510
504
511
```typescript
505
512
let gravity =Gravity.bind(event.address)
@@ -511,7 +518,7 @@ if (callResult.reverted) {
511
518
}
512
519
```
513
520
514
-
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