Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate types for mappings when int8 is used as id #1934

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
80 changes: 73 additions & 7 deletions packages/cli/src/codegen/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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;

Expand All @@ -27,35 +28,99 @@ 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;
default:
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 +157,7 @@ export default class SchemaCodeGenerator {
'Bytes',
'BigInt',
'BigDecimal',
'Int8',
],
'@graphprotocol/graph-ts',
),
Expand Down