Skip to content

Commit

Permalink
cli: Fix derived loader failing for entities with Bytes as ids (#1406)
Browse files Browse the repository at this point in the history
* cli: Fix derived loader issues with Bytes as ids and added tests

* Separate interface test and derived loader test for bytes as ids
  • Loading branch information
incrypto32 authored Jul 21, 2023
1 parent f0ce6c0 commit faefa94
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tall-vans-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': minor
---

Fix derived loaders not working failing for entities with Bytes as ID's
8 changes: 8 additions & 0 deletions packages/cli/src/codegen/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,13 @@ describe('Schema code generator', () => {
type Worker implements Employee @entity {
id: Bytes!
name: String!
tasks: [Task!]
}
type Task @entity {
id: Bytes!
employee: Employee!
workers: [Worker!] @derivedFrom(field: "tasks")
worker: Worker!
}
`);
Expand Down Expand Up @@ -525,6 +527,12 @@ describe('Schema code generator', () => {
returnType: undefined,
body: "\n this.set('worker', Value.fromBytes(value))\n ",
},
{
name: 'get workers',
params: [],
returnType: new NamedType('WorkerLoader'),
body: "\n return new WorkerLoader('Task', this.get('id')!.toBytes().toHexString(), 'workers')\n ",
},
],
});
});
Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/codegen/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,24 @@ export default class SchemaCodeGenerator {
const name = fieldDef.name.value;
const gqlType = fieldDef.type;
const returnType = this._returnTypeForDervied(gqlType);
const obj = this.schema.ast.definitions.find(def => {
if (def.kind === 'ObjectTypeDefinition') {
const defobj = def as ObjectTypeDefinitionNode;
return defobj.name.value == this._baseType(gqlType);
}
return false;
}) as ObjectTypeDefinitionNode;

const idf = IdField.fromTypeDef(obj);
const idIsBytes = idf.typeName() == 'Bytes';
const toValueString = idIsBytes ? '.toBytes().toHexString()' : '.toString()';

return tsCodegen.method(
`get ${name}`,
[],
returnType,
`
return new ${returnType}('${entityName}', this.get('id')!.toString(), '${name}')
return new ${returnType}('${entityName}', this.get('id')!${toValueString}, '${name}')
`,
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/tests/cli/init/ethereum/duplicate-ids
Submodule duplicate-ids added at ec49a2

0 comments on commit faefa94

Please sign in to comment.