Skip to content

Commit f5f974d

Browse files
authored
Fix codegen issues when using derived loader with Bytes as ID's (#1493)
* Fix Derived loader issue with Bytes as ID's * add changeset
1 parent 326b303 commit f5f974d

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

.changeset/witty-camels-switch.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+
Fix codegen issues when using derived loaders with Bytes as ID's

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,90 @@ describe('Schema code generator', () => {
537537
});
538538
});
539539

540+
test('get related method for WithBytes entity', () => {
541+
const codegen = createSchemaCodeGen(`
542+
type WithBytes @entity {
543+
id: Bytes!
544+
related: [RelatedBytes!]! @derivedFrom(field: "related")
545+
}
546+
547+
type RelatedBytes @entity {
548+
id: ID!
549+
related: WithBytes!
550+
}
551+
`);
552+
553+
const generatedTypes = codegen.generateTypes();
554+
555+
testEntity(generatedTypes, {
556+
name: 'WithBytes',
557+
members: [],
558+
methods: [
559+
{
560+
name: 'constructor',
561+
params: [new Param('id', new NamedType('Bytes'))],
562+
returnType: undefined,
563+
body: `
564+
super()
565+
this.set('id', Value.fromBytes(id));`,
566+
},
567+
{
568+
name: 'save',
569+
params: [],
570+
returnType: new NamedType('void'),
571+
body: `
572+
let id = this.get('id');
573+
assert(id != null, 'Cannot save WithBytes entity without an ID');
574+
if (id) {
575+
assert(id.kind == ValueKind.BYTES, \`Entities of type WithBytes must have an ID of type Bytes but the id '\${id.displayData()}' is of type \${id.displayKind()}\`);
576+
store.set('WithBytes', id.toBytes().toHexString(), this);
577+
}
578+
`,
579+
},
580+
581+
{
582+
name: 'load',
583+
static: true,
584+
params: [new Param('id', new NamedType('Bytes'))],
585+
returnType: new NullableType(new NamedType('WithBytes')),
586+
body: `return changetype<WithBytes | null>(store.get('WithBytes', id.toHexString()));`,
587+
},
588+
{
589+
name: 'loadInBlock',
590+
static: true,
591+
params: [new Param('id', new NamedType('Bytes'))],
592+
returnType: new NullableType(new NamedType('WithBytes')),
593+
body: `return changetype<WithBytes | null>(store.get_in_block('WithBytes', id.toHexString()));`,
594+
},
595+
{
596+
name: 'get id',
597+
params: [],
598+
returnType: new NamedType('Bytes'),
599+
body: `let value = this.get("id")
600+
if (!value || value.kind == ValueKind.NULL) {
601+
throw new Error("Cannot return null for a required field.")
602+
} else {
603+
return value.toBytes()
604+
}
605+
`,
606+
},
607+
{
608+
name: 'set id',
609+
params: [new Param('value', new NamedType('Bytes'))],
610+
returnType: undefined,
611+
body: `this.set('id', Value.fromBytes(value));`,
612+
},
613+
{
614+
name: 'get related',
615+
params: [],
616+
returnType: new NamedType('RelatedBytesLoader'),
617+
body: `return new RelatedBytesLoader('WithBytes', this.get('id')!.toBytes().toHexString(), 'related');`,
618+
},
619+
// Add any additional getters and setters for other fields if necessary
620+
],
621+
});
622+
});
623+
540624
test('no derived loader for interface', () => {
541625
const codegen = createSchemaCodeGen(`
542626
interface IExample {

packages/cli/src/codegen/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export default class SchemaCodeGenerator {
383383
obj,
384384
);
385385

386-
const idf = IdField.fromTypeDef(obj);
386+
const idf = IdField.fromTypeDef(entityDef);
387387
const idIsBytes = idf.typeName() == 'Bytes';
388388
const toValueString = idIsBytes ? '.toBytes().toHexString()' : '.toString()';
389389

0 commit comments

Comments
 (0)