@@ -537,6 +537,90 @@ describe('Schema code generator', () => {
537
537
} ) ;
538
538
} ) ;
539
539
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
+
540
624
test ( 'no derived loader for interface' , ( ) => {
541
625
const codegen = createSchemaCodeGen ( `
542
626
interface IExample {
0 commit comments