Skip to content

Commit 4d20d33

Browse files
committed
fix(hydra-cli): fix ts compilation error in generated relationship resolver
affects: @dzlzv/hydra-cli, hydra-e2e-tests, sample, sample-mappings
1 parent 5cc8040 commit 4d20d33

File tree

7 files changed

+102
-13
lines changed

7 files changed

+102
-13
lines changed

packages/hydra-cli/src/templates/entities/resolver.ts.mst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ export class {{className}}Resolver {
128128
.getMany();
129129
let {{relatedTsProp}}Ids: string[] = []
130130
{{#relationType.isOTM}}
131-
{{relatedTsProp}}Ids = Array.from(new Set(result.map(v => v.{{relatedTsProp}}.id)));
131+
{{relatedTsProp}}Ids = Array.from(new Set(result.map(v => v!.{{relatedTsProp}}!.id)));
132132
{{/relationType.isOTM}}
133133
{{#relationType.isMTM}}
134-
Array.from(new Set(result.map(v => v.{{relatedTsProp}}.map(c => {{relatedTsProp}}Ids.push(c.id)))));
134+
Array.from(new Set(result.map(v => v!.{{relatedTsProp}}!.map(c => {{relatedTsProp}}Ids.push(c.id)))));
135135
{{/relationType.isMTM}}
136136

137137
delete where.{{fieldName}}_some

packages/hydra-e2e-tests/fixtures/mappings/mappings.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
BlockTimestamp,
44
BlockHook,
55
HookType,
6+
Account,
67
} from '../generated/graphql-server/model'
78

89
// run 'NODE_URL=<RPC_ENDPOINT> EVENTS=<comma separated list of events> yarn codegen:mappings-types'
@@ -14,8 +15,25 @@ import {
1415
EventContext,
1516
BlockContext,
1617
StoreContext,
18+
DatabaseManager,
1719
} from '@dzlzv/hydra-common'
1820

21+
async function getOrCreate<T>(
22+
e: { new (...args: any[]): T },
23+
id: string,
24+
store: DatabaseManager
25+
): Promise<T> {
26+
let entity: T | undefined = await store.get<T>(e, {
27+
where: { id },
28+
})
29+
30+
if (entity === undefined) {
31+
entity = new e() as T
32+
;(<any>entity).id = id
33+
}
34+
return entity
35+
}
36+
1937
export async function balancesTransfer({
2038
store,
2139
event,
@@ -30,6 +48,24 @@ export async function balancesTransfer({
3048
transfer.tip = extrinsic ? new BN(extrinsic.tip.toString(10)) : new BN(0)
3149
transfer.insertedAt = new Date(block.timestamp)
3250

51+
const fromAcc = await getOrCreate<Account>(Account, from.toString(), store)
52+
fromAcc.hex = from.toHex()
53+
const toAcc = await getOrCreate<Account>(Account, to.toString(), store)
54+
toAcc.hex = to.toHex()
55+
56+
fromAcc.balance = fromAcc.balance || new BN(0)
57+
fromAcc.balance = fromAcc.balance.sub(value)
58+
fromAcc.balance = fromAcc.balance.sub(transfer.tip)
59+
60+
await store.save<Account>(fromAcc)
61+
62+
toAcc.balance = toAcc.balance || new BN(0)
63+
toAcc.balance = toAcc.balance.add(value)
64+
await store.save<Account>(toAcc)
65+
66+
transfer.fromAccount = fromAcc
67+
transfer.toAccount = toAcc
68+
3369
transfer.block = block.height
3470
transfer.comment = `Transferred ${transfer.value} from ${transfer.from} to ${transfer.to}`
3571
transfer.timestamp = new BN(block.timestamp)

packages/hydra-e2e-tests/fixtures/schema.graphql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
type Transfer @entity {
33
from: Bytes!
44
to: Bytes!
5+
fromAccount: Account!
6+
toAccount: Account!
57
value: BigInt!
68
comment: String @fulltext(query: "commentSearch")
79
block: Int!
@@ -27,6 +29,15 @@ enum HookType {
2729
POST
2830
}
2931

32+
type Account @entity {
33+
"Account address"
34+
id: ID!
35+
hex: String!
36+
balance: BigInt!
37+
incoming: [Transfer!] @derivedFrom(field: "toAccount")
38+
outgoing: [Transfer!] @derivedFrom(field: "fromAccount")
39+
}
40+
3041
############## Test purpose only @hydra-e2e-tests #################
3142
# To make sure graphql api is generated as expected
3243
type Extrinsic @entity {

packages/sample/.env

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ MAPPINGS_LOCATION=../../mappings
4141
TYPES_JSON=
4242

4343
# Indexer GraphQL API endpoint to fetch indexed events
44-
#INDEXER_ENDPOINT_URL=https://indexer-kusama.joystream.app/graphql
45-
INDEXER_ENDPOINT_URL=http://localhost:4001/graphql
44+
INDEXER_ENDPOINT_URL=https://indexer-kusama.joystream.app/graphql
4645

4746

4847
# Block height from which the processor starts. Note that if
4948
# there are already processed events in the database, this setting is ignored
5049
BLOCK_HEIGHT=100000
51-
BATCH_SIZE=5000
50+
BATCH_SIZE=10000
5251
BLOCK_WINDOW=10000
5352

5453

packages/sample/manifest.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ mappings:
3434
handler: balancesTransfer
3535
filter:
3636
specVersion: '[0,)'
37-
preBlockHooks:
38-
- handler: preHook
39-
postBlockHooks:
40-
- handler: postHook
37+
# preBlockHooks:
38+
# - handler: preHook
39+
# postBlockHooks:
40+
# - handler: postHook
4141

packages/sample/mappings/mappings.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
BlockTimestamp,
44
BlockHook,
55
HookType,
6+
Account,
67
} from '../generated/graphql-server/model'
78

89
// run 'NODE_URL=<RPC_ENDPOINT> EVENTS=<comma separated list of events> yarn codegen:mappings-types'
@@ -14,13 +15,30 @@ import {
1415
EventContext,
1516
BlockContext,
1617
StoreContext,
18+
DatabaseManager,
1719
} from '@dzlzv/hydra-common'
1820

1921
const start = Date.now()
2022
let blockTime = 0
2123
let totalEvents = 0
2224
let totalBlocks = 0
2325

26+
async function getOrCreate<T>(
27+
e: { new (...args: any[]): T },
28+
id: string,
29+
store: DatabaseManager
30+
): Promise<T> {
31+
let entity: T | undefined = await store.get<T>(e, {
32+
where: { id },
33+
})
34+
35+
if (entity === undefined) {
36+
entity = new e() as T
37+
;(<any>entity).id = id
38+
}
39+
return entity
40+
}
41+
2442
export async function balancesTransfer({
2543
store,
2644
event,
@@ -29,10 +47,25 @@ export async function balancesTransfer({
2947
}: EventContext & StoreContext) {
3048
const transfer = new Transfer()
3149
const [from, to, value] = new Balances.TransferEvent(event).params
32-
transfer.from = Buffer.from(from.toHex())
33-
transfer.to = Buffer.from(to.toHex())
50+
const fromAcc = await getOrCreate<Account>(Account, from.toString(), store)
51+
fromAcc.hex = from.toHex()
52+
const toAcc = await getOrCreate<Account>(Account, to.toString(), store)
53+
toAcc.hex = to.toHex()
54+
3455
transfer.value = value.toBn()
3556
transfer.tip = extrinsic ? new BN(extrinsic.tip.toString(10)) : new BN(0)
57+
58+
fromAcc.balance = fromAcc.balance.sub(value)
59+
fromAcc.balance = fromAcc.balance.sub(transfer.tip)
60+
61+
await store.save<Account>(fromAcc)
62+
63+
toAcc.balance = toAcc.balance.add(value)
64+
await store.save<Account>(toAcc)
65+
66+
transfer.from = fromAcc
67+
transfer.to = toAcc
68+
3669
transfer.insertedAt = new Date(block.timestamp)
3770
transfer.block = block.height
3871
transfer.comment = `Transferred ${transfer.value} from ${transfer.from} to ${transfer.to}`

packages/sample/schema.graphql

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" All transfers "
22
type Transfer @entity {
3-
from: Bytes!
4-
to: Bytes!
3+
from: Account!
4+
to: Account!
55
value: BigInt!
66
comment: String @fulltext(query: "commentSearch")
77
block: Int!
@@ -10,6 +10,16 @@ type Transfer @entity {
1010
insertedAt: DateTime!
1111
}
1212

13+
14+
type Account @entity {
15+
"Account address"
16+
id: ID!
17+
hex: String!
18+
balance: BigInt!
19+
incoming: [Transfer!] @derivedFrom(field: "to")
20+
outgoing: [Transfer!] @derivedFrom(field: "from")
21+
}
22+
1323
" Tracks block timestamps "
1424
type BlockTimestamp @entity {
1525
blockNumber: BigInt!

0 commit comments

Comments
 (0)