Skip to content

Commit f5e4b58

Browse files
authored
feat: Add loadInBlock function for entities as part of codegen (#1306)
* feat: Add loadInBlock function for entities as part of codegen * fix tests * match with host fn def
1 parent 285a045 commit f5e4b58

File tree

9 files changed

+88
-2
lines changed

9 files changed

+88
-2
lines changed

.changeset/slow-emus-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': minor
3+
---
4+
5+
Add loadInBlock function for entities as part of codegen

.changeset/warm-goats-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-ts': minor
3+
---
4+
5+
expose Host's `get_in_block` function

examples/ethereum-basic-event-handlers/generated/schema.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ export class NewGravatar extends Entity {
2929
}
3030
}
3131

32+
static loadInBlock(id: string): NewGravatar | null {
33+
return changetype<NewGravatar | null>(
34+
store.get_in_block("NewGravatar", id)
35+
);
36+
}
37+
3238
static load(id: string): NewGravatar | null {
3339
return changetype<NewGravatar | null>(store.get("NewGravatar", id));
3440
}
@@ -104,6 +110,12 @@ export class UpdatedGravatar extends Entity {
104110
}
105111
}
106112

113+
static loadInBlock(id: string): UpdatedGravatar | null {
114+
return changetype<UpdatedGravatar | null>(
115+
store.get_in_block("UpdatedGravatar", id)
116+
);
117+
}
118+
107119
static load(id: string): UpdatedGravatar | null {
108120
return changetype<UpdatedGravatar | null>(store.get("UpdatedGravatar", id));
109121
}

examples/example-subgraph/src/types/schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export class ExampleEntity extends Entity {
2929
}
3030
}
3131

32+
static loadInBlock(id: string): ExampleEntity | null {
33+
return changetype<ExampleEntity | null>(store.get_in_block('ExampleEntity', id));
34+
}
35+
3236
static load(id: string): ExampleEntity | null {
3337
return changetype<ExampleEntity | null>(store.get('ExampleEntity', id));
3438
}
@@ -417,6 +421,10 @@ export class OtherEntity extends Entity {
417421
}
418422
}
419423

424+
static loadInBlock(id: string): OtherEntity | null {
425+
return changetype<OtherEntity | null>(store.get_in_block('OtherEntity', id));
426+
}
427+
420428
static load(id: string): OtherEntity | null {
421429
return changetype<OtherEntity | null>(store.get('OtherEntity', id));
422430
}

packages/cli/src/codegen/schema.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ describe('Schema code generator', () => {
127127
}
128128
`,
129129
},
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+
},
130139
{
131140
name: 'load',
132141
static: true,
@@ -307,6 +316,15 @@ describe('Schema code generator', () => {
307316
}
308317
`,
309318
},
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+
},
310328
{
311329
name: 'load',
312330
static: true,
@@ -426,6 +444,16 @@ describe('Schema code generator', () => {
426444
" store.set('Task', id.toBytes().toHexString(), this)\n" +
427445
' }',
428446
},
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+
},
429457
{
430458
name: 'load',
431459
static: true,

packages/cli/src/codegen/schema.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ export default class SchemaCodeGenerator {
171171
}`,
172172
),
173173

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+
174183
tsCodegen.staticMethod(
175184
'load',
176185
[tsCodegen.param('id', tsCodegen.namedType(idField.typeName()))],

packages/cli/tests/cli/validation/call-handler-with-tuple/generated/schema.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,21 @@ export class MyEntity extends Entity {
2929
}
3030
}
3131

32+
static loadInBlock(id: string): MyEntity | null {
33+
return changetype<MyEntity | null>(store.get_in_block("MyEntity", id));
34+
}
35+
3236
static load(id: string): MyEntity | null {
3337
return changetype<MyEntity | null>(store.get("MyEntity", id));
3438
}
3539

3640
get id(): string {
3741
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+
}
3947
}
4048

4149
set id(value: string) {

packages/cli/tests/cli/validation/example-values-found/generated/schema.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,21 @@ export class MyEntity extends Entity {
2929
}
3030
}
3131

32+
static loadInBlock(id: string): MyEntity | null {
33+
return changetype<MyEntity | null>(store.get_in_block("MyEntity", id));
34+
}
35+
3236
static load(id: string): MyEntity | null {
3337
return changetype<MyEntity | null>(store.get("MyEntity", id));
3438
}
3539

3640
get id(): string {
3741
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+
}
3947
}
4048

4149
set id(value: string) {

packages/ts/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export * from './common/value';
2323
*/
2424
export declare namespace store {
2525
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;
2629
function set(entity: string, id: string, data: Entity): void;
2730
function remove(entity: string, id: string): void;
2831
}

0 commit comments

Comments
 (0)