File tree Expand file tree Collapse file tree 9 files changed +88
-2
lines changed
ethereum-basic-event-handlers/generated
example-subgraph/src/types
call-handler-with-tuple/generated
example-values-found/generated Expand file tree Collapse file tree 9 files changed +88
-2
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @graphprotocol/graph-cli ' : minor
3
+ ---
4
+
5
+ Add loadInBlock function for entities as part of codegen
Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @graphprotocol/graph-ts ' : minor
3
+ ---
4
+
5
+ expose Host's ` get_in_block ` function
Original file line number Diff line number Diff line change @@ -29,6 +29,12 @@ export class NewGravatar extends Entity {
29
29
}
30
30
}
31
31
32
+ static loadInBlock ( id : string ) : NewGravatar | null {
33
+ return changetype < NewGravatar | null > (
34
+ store . get_in_block ( "NewGravatar" , id )
35
+ ) ;
36
+ }
37
+
32
38
static load ( id : string ) : NewGravatar | null {
33
39
return changetype < NewGravatar | null > ( store . get ( "NewGravatar" , id ) ) ;
34
40
}
@@ -104,6 +110,12 @@ export class UpdatedGravatar extends Entity {
104
110
}
105
111
}
106
112
113
+ static loadInBlock ( id : string ) : UpdatedGravatar | null {
114
+ return changetype < UpdatedGravatar | null > (
115
+ store . get_in_block ( "UpdatedGravatar" , id )
116
+ ) ;
117
+ }
118
+
107
119
static load ( id : string ) : UpdatedGravatar | null {
108
120
return changetype < UpdatedGravatar | null > ( store . get ( "UpdatedGravatar" , id ) ) ;
109
121
}
Original file line number Diff line number Diff line change @@ -29,6 +29,10 @@ export class ExampleEntity extends Entity {
29
29
}
30
30
}
31
31
32
+ static loadInBlock ( id : string ) : ExampleEntity | null {
33
+ return changetype < ExampleEntity | null > ( store . get_in_block ( 'ExampleEntity' , id ) ) ;
34
+ }
35
+
32
36
static load ( id : string ) : ExampleEntity | null {
33
37
return changetype < ExampleEntity | null > ( store . get ( 'ExampleEntity' , id ) ) ;
34
38
}
@@ -417,6 +421,10 @@ export class OtherEntity extends Entity {
417
421
}
418
422
}
419
423
424
+ static loadInBlock ( id : string ) : OtherEntity | null {
425
+ return changetype < OtherEntity | null > ( store . get_in_block ( 'OtherEntity' , id ) ) ;
426
+ }
427
+
420
428
static load ( id : string ) : OtherEntity | null {
421
429
return changetype < OtherEntity | null > ( store . get ( 'OtherEntity' , id ) ) ;
422
430
}
Original file line number Diff line number Diff line change @@ -127,6 +127,15 @@ describe('Schema code generator', () => {
127
127
}
128
128
` ,
129
129
} ,
130
+ {
131
+ name : 'loadInBlock' ,
132
+ static : true ,
133
+ params : [ new Param ( 'id' , new NamedType ( 'string' ) ) ] ,
134
+ returnType : new NullableType ( new NamedType ( 'Account' ) ) ,
135
+ body : `
136
+ return changetype<Account | null>(store.get_in_block('Account', id))
137
+ ` ,
138
+ } ,
130
139
{
131
140
name : 'load' ,
132
141
static : true ,
@@ -307,6 +316,15 @@ describe('Schema code generator', () => {
307
316
}
308
317
` ,
309
318
} ,
319
+ {
320
+ name : 'loadInBlock' ,
321
+ static : true ,
322
+ params : [ new Param ( 'id' , new NamedType ( 'string' ) ) ] ,
323
+ returnType : new NullableType ( new NamedType ( 'Wallet' ) ) ,
324
+ body : `
325
+ return changetype<Wallet | null>(store.get_in_block('Wallet', id))
326
+ ` ,
327
+ } ,
310
328
{
311
329
name : 'load' ,
312
330
static : true ,
@@ -426,6 +444,16 @@ describe('Schema code generator', () => {
426
444
" store.set('Task', id.toBytes().toHexString(), this)\n" +
427
445
' }' ,
428
446
} ,
447
+ {
448
+ name : 'loadInBlock' ,
449
+ static : true ,
450
+ params : [ new Param ( 'id' , new NamedType ( 'Bytes' ) ) ] ,
451
+ returnType : new NullableType ( new NamedType ( 'Task' ) ) ,
452
+ body :
453
+ '\n' +
454
+ " return changetype<Task | null>(store.get_in_block('Task', id.toHexString()))\n" +
455
+ ' ' ,
456
+ } ,
429
457
{
430
458
name : 'load' ,
431
459
static : true ,
Original file line number Diff line number Diff line change @@ -171,6 +171,15 @@ export default class SchemaCodeGenerator {
171
171
}` ,
172
172
) ,
173
173
174
+ tsCodegen . staticMethod (
175
+ 'loadInBlock' ,
176
+ [ tsCodegen . param ( 'id' , tsCodegen . namedType ( idField . typeName ( ) ) ) ] ,
177
+ tsCodegen . nullableType ( tsCodegen . namedType ( entityName ) ) ,
178
+ `
179
+ return changetype<${ entityName } | null>(store.get_in_block('${ entityName } ', ${ idField . tsToString ( ) } ))
180
+ ` ,
181
+ ) ,
182
+
174
183
tsCodegen . staticMethod (
175
184
'load' ,
176
185
[ tsCodegen . param ( 'id' , tsCodegen . namedType ( idField . typeName ( ) ) ) ] ,
Original file line number Diff line number Diff line change @@ -29,13 +29,21 @@ export class MyEntity extends Entity {
29
29
}
30
30
}
31
31
32
+ static loadInBlock ( id : string ) : MyEntity | null {
33
+ return changetype < MyEntity | null > ( store . get_in_block ( "MyEntity" , id ) ) ;
34
+ }
35
+
32
36
static load ( id : string ) : MyEntity | null {
33
37
return changetype < MyEntity | null > ( store . get ( "MyEntity" , id ) ) ;
34
38
}
35
39
36
40
get id ( ) : string {
37
41
let value = this . get ( "id" ) ;
38
- return value ! . toString ( ) ;
42
+ if ( ! value || value . kind == ValueKind . NULL ) {
43
+ throw new Error ( "Cannot return null for a required field." ) ;
44
+ } else {
45
+ return value . toString ( ) ;
46
+ }
39
47
}
40
48
41
49
set id ( value : string ) {
Original file line number Diff line number Diff line change @@ -29,13 +29,21 @@ export class MyEntity extends Entity {
29
29
}
30
30
}
31
31
32
+ static loadInBlock ( id : string ) : MyEntity | null {
33
+ return changetype < MyEntity | null > ( store . get_in_block ( "MyEntity" , id ) ) ;
34
+ }
35
+
32
36
static load ( id : string ) : MyEntity | null {
33
37
return changetype < MyEntity | null > ( store . get ( "MyEntity" , id ) ) ;
34
38
}
35
39
36
40
get id ( ) : string {
37
41
let value = this . get ( "id" ) ;
38
- return value ! . toString ( ) ;
42
+ if ( ! value || value . kind == ValueKind . NULL ) {
43
+ throw new Error ( "Cannot return null for a required field." ) ;
44
+ } else {
45
+ return value . toString ( ) ;
46
+ }
39
47
}
40
48
41
49
set id ( value : string ) {
Original file line number Diff line number Diff line change @@ -23,6 +23,9 @@ export * from './common/value';
23
23
*/
24
24
export declare namespace store {
25
25
function get ( entity : string , id : string ) : Entity | null ;
26
+ /** If the entity was not created in the block, this function will return null. */
27
+ // Matches the host function https://github.com/graphprotocol/graph-node/blob/9f4a1821146b18f6f49165305e9a8c0795120fad/runtime/wasm/src/module/mod.rs#L1091-L1099
28
+ function get_in_block ( entity : string , id : string ) : Entity | null ;
26
29
function set ( entity : string , id : string , data : Entity ) : void ;
27
30
function remove ( entity : string , id : string ) : void ;
28
31
}
You can’t perform that action at this time.
0 commit comments