Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/new-rules-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphprotocol/graph-cli": patch
---

fix: generate types for mappings when int8 is used as id
128 changes: 128 additions & 0 deletions packages/cli/src/codegen/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,134 @@ describe('Schema code generator', { concurrent: true }, () => {
});
});

test('Should handle references with Int8 id types', async () => {
const codegen = createSchemaCodeGen(`
interface Employee {
id: Int8!
name: String!
}

type Worker implements Employee @entity {
id: Int8!
name: String!
tasks: [Task!]
}

type Task @entity {
id: Int8!
employee: Employee!
workers: [Worker!] @derivedFrom(field: "tasks")
worker: Worker!
}
`);

const generatedTypes = codegen.generateTypes();
await testEntity(generatedTypes, {
name: 'Task',
members: [],
methods: [
{
name: 'constructor',
params: [new Param('id', new NamedType('Int8'))],
returnType: undefined,
body: "\n super()\n this.set('id', Value.fromI64(id))\n ",
},
{
name: 'save',
params: [],
returnType: new NamedType('void'),
body:
'\n' +
" let id = this.get('id')\n" +
' assert(id != null,\n' +
" 'Cannot save Task entity without an ID')\n" +
' if (id) {\n' +
' assert(id.kind == ValueKind.INT8,\n' +
" `Entities of type Task must have an ID of type Int8 but the id '${id.displayData()}' is of type ${id.displayKind()}`)\n" +
" store.set('Task', id.toI64().toString(), this)\n" +
' }',
},
{
name: 'loadInBlock',
static: true,
params: [new Param('id', new NamedType('Int8'))],
returnType: new NullableType(new NamedType('Task')),
body:
'\n' +
" return changetype<Task | null>(store.get_in_block('Task', id.toString()))\n" +
' ',
},
{
name: 'load',
static: true,
params: [new Param('id', new NamedType('Int8'))],
returnType: new NullableType(new NamedType('Task')),
body:
'\n' +
" return changetype<Task | null>(store.get('Task', id.toString()))\n" +
' ',
},
{
name: 'get id',
params: [],
returnType: new NamedType('i64'),
body: `let value = this.get("id")
if (!value || value.kind == ValueKind.NULL) {
return 0;
} else {
return value.toI64()
}`,
},
{
name: 'set id',
params: [new Param('value', new NamedType('i64'))],
returnType: undefined,
body: "\n this.set('id', Value.fromI64(value))\n ",
},
{
name: 'get employee',
params: [],
returnType: new NamedType('i64'),
body: `let value = this.get('employee')
if (!value || value.kind == ValueKind.NULL) {
return 0;
} else {
return value.toI64()
}`,
},
{
name: 'set employee',
params: [new Param('value', new NamedType('i64'))],
returnType: undefined,
body: "\n this.set('employee', Value.fromI64(value))\n ",
},
{
name: 'get worker',
params: [],
returnType: new NamedType('i64'),
body: `let value = this.get('worker')
if (!value || value.kind == ValueKind.NULL) {
return 0;
} else {
return value.toI64()
}`,
},
{
name: 'set worker',
params: [new Param('value', new NamedType('i64'))],
returnType: undefined,
body: "\n this.set('worker', Value.fromI64(value))\n ",
},
{
name: 'get workers',
params: [],
returnType: new NamedType('WorkerLoader'),
body: "\n return new WorkerLoader('Task', this.get('id')!.toString(), 'workers')\n ",
},
],
});
});

test('get related method for WithBytes entity', async () => {
const codegen = createSchemaCodeGen(`
type WithBytes @entity {
Expand Down
85 changes: 77 additions & 8 deletions packages/cli/src/codegen/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import * as util from './util.js';
class IdField {
static BYTES = Symbol('Bytes');
static STRING = Symbol('String');
static INT8 = Symbol('Int8');

private kind: typeof IdField.BYTES | typeof IdField.STRING;
private kind: typeof IdField.BYTES | typeof IdField.INT8 | typeof IdField.STRING;

constructor(idField: FieldDefinitionNode | undefined) {
if (idField?.type.kind !== 'NonNullType') {
Expand All @@ -27,35 +28,102 @@ class IdField {
throw Error('id field must be a named type');
}
const typeName = idField.type.type.name.value;
this.kind = typeName === 'Bytes' ? IdField.BYTES : IdField.STRING;
switch (typeName) {
case 'Bytes':
this.kind = IdField.BYTES;
break;
case 'Int8':
this.kind = IdField.INT8;
break;
case 'String':
this.kind = IdField.STRING;
break;
default:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add case 'string' for consistency and future refactoring

this.kind = IdField.STRING;
break;
}
}

typeName() {
return this.kind === IdField.BYTES ? 'Bytes' : 'string';
switch (this.kind) {
case IdField.BYTES:
return 'Bytes';
case IdField.INT8:
return 'Int8';
case IdField.STRING:
return 'string';
default:
return 'string';
}
}

gqlTypeName() {
return this.kind === IdField.BYTES ? 'Bytes' : 'String';
switch (this.kind) {
case IdField.BYTES:
return 'Bytes';
case IdField.INT8:
return 'Int8';
case IdField.STRING:
return 'String';
default:
return 'String';
}
}

tsNamedType() {
return tsCodegen.namedType(this.typeName());
}

tsValueFrom() {
return this.kind === IdField.BYTES ? 'Value.fromBytes(id)' : 'Value.fromString(id)';
switch (this.kind) {
case IdField.BYTES:
return 'Value.fromBytes(id)';
case IdField.INT8:
return 'Value.fromI64(id)';
case IdField.STRING:
return 'Value.fromString(id)';
default:
return 'Value.fromString(id)';
}
}

tsValueKind() {
return this.kind === IdField.BYTES ? 'ValueKind.BYTES' : 'ValueKind.STRING';
switch (this.kind) {
case IdField.BYTES:
return 'ValueKind.BYTES';
case IdField.INT8:
return 'ValueKind.INT8';
case IdField.STRING:
return 'ValueKind.STRING';
default:
return 'ValueKind.STRING';
}
}

tsValueToString() {
return this.kind == IdField.BYTES ? 'id.toBytes().toHexString()' : 'id.toString()';
switch (this.kind) {
case IdField.BYTES:
return 'id.toBytes().toHexString()';
case IdField.INT8:
return 'id.toI64().toString()';
case IdField.STRING:
return 'id.toString()';
default:
return 'id.toString()';
}
}

tsToString() {
return this.kind == IdField.BYTES ? 'id.toHexString()' : 'id';
switch (this.kind) {
case IdField.BYTES:
return 'id.toHexString()';
case IdField.INT8:
return 'id.toString()';
case IdField.STRING:
return 'id';
default:
return 'id';
}
}

static fromFields(fields: readonly FieldDefinitionNode[] | undefined) {
Expand Down Expand Up @@ -92,6 +160,7 @@ export default class SchemaCodeGenerator {
'Bytes',
'BigInt',
'BigDecimal',
'Int8',
],
'@graphprotocol/graph-ts',
),
Expand Down
8 changes: 8 additions & 0 deletions packages/ts/common/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ export class Value {
return Value.fromArray(output);
}

static fromI64Array(input: Array<i64>): Value {
const output = new Array<Value>(input.length);
for (let i: i64 = 0; i < input.length; i++) {
output[i] = Value.fromI64(input[i]);
}
return Value.fromArray(output);
}

static fromBigIntArray(input: Array<BigInt>): Value {
const output = new Array<Value>(input.length);
for (let i: i32 = 0; i < input.length; i++) {
Expand Down