From 4f367f94e1e4548590e42885666b5d97b759c01b Mon Sep 17 00:00:00 2001 From: Saihajpreet Singh Date: Thu, 15 Jun 2023 17:40:43 -0400 Subject: [PATCH] feat: derived loader docs (#404) --- .../en/developing/assemblyscript-api.mdx | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/website/pages/en/developing/assemblyscript-api.mdx b/website/pages/en/developing/assemblyscript-api.mdx index b8d735f029d6..f618606334cf 100644 --- a/website/pages/en/developing/assemblyscript-api.mdx +++ b/website/pages/en/developing/assemblyscript-api.mdx @@ -298,6 +298,33 @@ if (transfer == null) { > 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. +#### Looking up derived entities + +As of `graph-node` v0.31.0, `@graphprotocol/graph-ts` v0.31.0 and `@graphprotocol/graph-cli` v0.51.0 the `loadRelated` method is available. + +This enables loading derived entity fields from within an event handler. For example, given the following schema: + +```graphql +type Token @entity { + id: ID! + holder: Holder! + color: String +} + +type Holder @entity { + id: ID! + tokens: [Token!]! @derivedFrom(field: "holder") +} +``` + +The following code will load the `Token` entity that the `Holder` entity was derived from: + +```typescript +let holder = Holder.load('test-id') +// Load the Token entity that the Holder entity was derived from +let token = holder.tokens.load() +``` + #### Updating existing entities There are two ways to update an existing entity: