Skip to content

Commit

Permalink
Adding example (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMacaulay authored Feb 10, 2025
1 parent 8f719c8 commit 04d6097
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions website/pages/en/subgraphs/developing/creating/ql-schema.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ type TokenBalance @entity {
}
```

Here is an example of how to write a mapping for a subgraph with reverse lookups:

```typescript
let token = new Token(event.address) // Create Token
token.save() // tokenBalances is derived automatically

let tokenBalance = new TokenBalance(event.address)
tokenBalance.amount = BigInt.fromI32(0)
tokenBalance.token = token.id // Reference stored here
tokenBalance.save()
```

#### Many-To-Many Relationships

For many-to-many relationships, such as users that each may belong to any number of organizations, the most straightforward, but generally not the most performant, way to model the relationship is as an array in each of the two entities involved. If the relationship is symmetric, only one side of the relationship needs to be stored and the other side can be derived.
Expand Down

0 comments on commit 04d6097

Please sign in to comment.