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
6 changes: 6 additions & 0 deletions .changeset/twelve-tables-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphprotocol/graph-ts': minor
'@graphprotocol/graph-cli': minor
---

Added support for handling GraphQL `Int8` scalar as `i64` (AssemblyScript)
23 changes: 23 additions & 0 deletions packages/cli/src/codegen/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ describe('Schema code generator', () => {

# derivedFrom
wallets: [Wallet!] @derivedFrom(field: "account")

# New scalars
int8: Int8!
}

type Wallet @entity {
Expand Down Expand Up @@ -270,6 +273,26 @@ describe('Schema code generator', () => {
this.set('isActive', Value.fromBoolean(value))
`,
},
{
name: 'get int8',
params: [],
returnType: new NamedType('i64'),
body: `let value = this.get('int8')
if (!value || value.kind == ValueKind.NULL) {
return 0
} else {
return value.toI64()
}
`,
},
{
name: 'set int8',
params: [new Param('value', new NamedType('i64'))],
returnType: undefined,
body: `
this.set('int8', Value.fromI64(value))
`,
},
{
name: 'get wallets',
params: [],
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/codegen/types/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
['[Bytes]', 'Array<Bytes>', (code: any) => `${code}.toBytesArray()`],
['[Boolean]', 'Array<boolean>', (code: any) => `${code}.toBooleanArray()`],
['[Int]', 'Array<i32>', (code: any) => `${code}.toI32Array()`],
['[Int8]', 'Array<i64>', (code: any) => `${code}.toI64Array()`],
['[BigInt]', 'Array<BigInt>', (code: any) => `${code}.toBigIntArray()`],
['[ID]', 'Array<string>', (code: any) => `${code}.toStringArray()`],
['[String]', 'Array<string>', (code: any) => `${code}.toStringArray()`],
Expand All @@ -281,6 +282,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
['Bytes', 'Bytes', (code: any) => `${code}.toBytes()`],
['Boolean', 'boolean', (code: any) => `${code}.toBoolean()`],
['Int', 'i32', (code: any) => `${code}.toI32()`],
['Int8', 'i64', (code: any) => `${code}.toI64()`],
['BigInt', 'BigInt', (code: any) => `${code}.toBigInt()`],
['ID', 'string', (code: any) => `${code}.toString()`],
['String', 'string', (code: any) => `${code}.toString()`],
Expand All @@ -302,6 +304,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
['Array<Array<Bytes>>', '[[Bytes]]', (code: any) => `Value.fromBytesMatrix(${code})`],
['Array<Array<boolean>>', '[[Boolean]]', (code: any) => `Value.fromBooleanMatrix(${code})`],
['Array<Array<i32>>', '[[Int]]', (code: any) => `Value.fromI32Matrix(${code})`],
['Array<Array<i64>>', '[[Int8]]', (code: any) => `Value.fromI64Matrix(${code})`],
['Array<Array<BigInt>>', '[[BigInt]]', (code: any) => `Value.fromBigIntMatrix(${code})`],
['Array<Array<string>>', '[[String]]', (code: any) => `Value.fromStringMatrix(${code})`],
['Array<Array<string>>', '[[ID]]', (code: any) => `Value.fromStringMatrix(${code})`],
Expand All @@ -319,6 +322,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
['Array<Bytes>', '[Bytes]', (code: any) => `Value.fromBytesArray(${code})`],
['Array<boolean>', '[Boolean]', (code: any) => `Value.fromBooleanArray(${code})`],
['Array<i32>', '[Int]', (code: any) => `Value.fromI32Array(${code})`],
['Array<i64>', '[Int8]', (code: any) => `Value.fromI64Array(${code})`],
['Array<BigInt>', '[BigInt]', (code: any) => `Value.fromBigIntArray(${code})`],
['Array<string>', '[String]', (code: any) => `Value.fromStringArray(${code})`],
['Array<string>', '[ID]', (code: any) => `Value.fromStringArray(${code})`],
Expand All @@ -332,6 +336,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
['Bytes', 'Bytes', (code: any) => `Value.fromBytes(${code})`],
['boolean', 'Boolean', (code: any) => `Value.fromBoolean(${code})`],
['i32', 'Int', (code: any) => `Value.fromI32(${code})`],
['i64', 'Int8', (code: any) => `Value.fromI64(${code})`],
['BigInt', 'BigInt', (code: any) => `Value.fromBigInt(${code})`],
['string', 'String', (code: any) => `Value.fromString(${code})`],
['string', 'ID', (code: any) => `Value.fromString(${code})`],
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/validation/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ describe('Schema validation', () => {
expect(typeSuggestion(`int`)).toBe('Int');
expect(typeSuggestion(`uint`)).toBe('BigInt');
expect(typeSuggestion(`uint32`)).toBe('BigInt');
expect(typeSuggestion(`int8`)).toBe('Int8');
expect(typeSuggestion(`i8`)).toBe('Int8');
expect(typeSuggestion(`u8`)).toBe('Int8');
expect(typeSuggestion(`uint8`)).toBe('Int8');

// Test i8..i32, int8..int32
for (let i = 8; i <= 32; i += 8) {
// Test i16..i32, int17..int32
for (let i = 16; i <= 32; i += 8) {
expect(typeSuggestion(`i${i}`)).toBe('Int');
expect(typeSuggestion(`int${i}`)).toBe('Int');
}

// Test u8..u24, uint8..uint24
for (let i = 8; i <= 24; i += 8) {
// Test u16..u24, uint16..uint24
for (let i = 16; i <= 24; i += 8) {
expect(typeSuggestion(`u${i}`)).toBe('Int');
expect(typeSuggestion(`uint${i}`)).toBe('Int');
}
Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ const List = immutable.List;
const Set = immutable.Set;

// Builtin scalar types
const BUILTIN_SCALAR_TYPES = ['Boolean', 'Int', 'BigDecimal', 'String', 'BigInt', 'Bytes', 'ID'];
const BUILTIN_SCALAR_TYPES = [
'Boolean',
'Int',
'BigDecimal',
'String',
'BigInt',
'Bytes',
'ID',
'Int8',
];

// Type suggestions for common mistakes
const TYPE_SUGGESTIONS = [
Expand All @@ -26,9 +35,12 @@ const TYPE_SUGGESTIONS = [
['float', 'BigDecimal'],
['Float', 'BigDecimal'],
['int', 'Int'],
['int8', 'Int8'],
['uint', 'BigInt'],
['owner', 'String'],
['Owner', 'String'],
[/^(u|uint)8$/, 'Int8'],
[/^(i|int)8$/, 'Int8'],
[/^(u|uint)(8|16|24)$/, 'Int'],
[/^(i|int)(8|16|24|32)$/, 'Int'],
[/^(u|uint)32$/, 'BigInt'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- Field 'int': Unknown type 'int'. Did you mean 'Int'?
- Field 'uint': Unknown type 'uint'. Did you mean 'BigInt'?
- Field 'uint32': Unknown type 'uint32'. Did you mean 'BigInt'?
- Field 'i8': Unknown type 'i8'. Did you mean 'Int'?
- Field 'i8': Unknown type 'i8'. Did you mean 'Int8'?
- Field 'i16': Unknown type 'i16'. Did you mean 'Int'?
- Field 'i24': Unknown type 'i24'. Did you mean 'Int'?
- Field 'i32': Unknown type 'i32'. Did you mean 'Int'?
Expand Down Expand Up @@ -46,7 +46,7 @@
- Field 'i248': Unknown type 'i248'. Did you mean 'BigInt'?
- Field 'i256': Unknown type 'i256'. Did you mean 'BigInt'?
- Field 'i256': Unknown type 'i256'. Did you mean 'BigInt'?
- Field 'int8': Unknown type 'int8'. Did you mean 'Int'?
- Field 'int8': Unknown type 'int8'. Did you mean 'Int8'?
- Field 'int16': Unknown type 'int16'. Did you mean 'Int'?
- Field 'int24': Unknown type 'int24'. Did you mean 'Int'?
- Field 'int32': Unknown type 'int32'. Did you mean 'Int'?
Expand Down Expand Up @@ -79,7 +79,7 @@
- Field 'int248': Unknown type 'int248'. Did you mean 'BigInt'?
- Field 'int256': Unknown type 'int256'. Did you mean 'BigInt'?
- Field 'int256': Unknown type 'int256'. Did you mean 'BigInt'?
- Field 'u8': Unknown type 'u8'. Did you mean 'Int'?
- Field 'u8': Unknown type 'u8'. Did you mean 'Int8'?
- Field 'u16': Unknown type 'u16'. Did you mean 'Int'?
- Field 'u24': Unknown type 'u24'. Did you mean 'Int'?
- Field 'u32': Unknown type 'u32'. Did you mean 'BigInt'?
Expand Down Expand Up @@ -112,7 +112,7 @@
- Field 'u248': Unknown type 'u248'. Did you mean 'BigInt'?
- Field 'u256': Unknown type 'u256'. Did you mean 'BigInt'?
- Field 'u256': Unknown type 'u256'. Did you mean 'BigInt'?
- Field 'uint8': Unknown type 'uint8'. Did you mean 'Int'?
- Field 'uint8': Unknown type 'uint8'. Did you mean 'Int8'?
- Field 'uint16': Unknown type 'uint16'. Did you mean 'Int'?
- Field 'uint24': Unknown type 'uint24'. Did you mean 'Int'?
- Field 'uint32': Unknown type 'uint32'. Did you mean 'BigInt'?
Expand Down
2 changes: 2 additions & 0 deletions packages/ts/common/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export declare namespace bigDecimal {
function fromString(s: string): BigDecimal;
}

export type Int8 = i64;

/** An Ethereum address (20 bytes). */
export class Address extends Bytes {
static fromString(s: string): Address {
Expand Down
49 changes: 49 additions & 0 deletions packages/ts/common/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export enum ValueKind {
NULL = 5,
BYTES = 6,
BIGINT = 7,
INT8 = 8,
}

const VALUE_KIND_NAMES = [
Expand All @@ -25,6 +26,7 @@ const VALUE_KIND_NAMES = [
'null',
'Bytes',
'BigInt',
'Int8',
];

/**
Expand Down Expand Up @@ -66,6 +68,14 @@ export class Value {
return this.data as i32;
}

toI64(): i64 {
if (this.kind == ValueKind.NULL) {
return 0;
}
assert(this.kind == ValueKind.INT8, 'Value is not an i64.');
return this.data as i64;
}

toString(): string {
assert(this.kind == ValueKind.STRING, 'Value is not a string.');
return changetype<string>(this.data as u32);
Expand Down Expand Up @@ -131,6 +141,15 @@ export class Value {
return output;
}

toI64Array(): Array<i64> {
const values = this.toArray();
const output = new Array<i64>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
output[i] = values[i].toI32();
}
return output;
}

toBigIntArray(): Array<BigInt> {
const values = this.toArray();
const output = new Array<BigInt>(values.length);
Expand Down Expand Up @@ -209,6 +228,19 @@ export class Value {
return out;
}

toI64Matrix(): Array<Array<i64>> {
const valueMatrix = this.toMatrix();
const out = new Array<Array<i64>>(valueMatrix.length);

for (let i: i32 = 0; i < valueMatrix.length; i++) {
out[i] = new Array<i64>(valueMatrix[i].length);
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
out[i][j] = valueMatrix[i][j].toI64();
}
}
return out;
}

toBigIntMatrix(): Array<Array<BigInt>> {
const valueMatrix = this.toMatrix();
const out = new Array<Array<BigInt>>(valueMatrix.length);
Expand Down Expand Up @@ -238,6 +270,8 @@ export class Value {
return this.toString();
case ValueKind.INT:
return this.toI32().toString();
case ValueKind.INT8:
return this.toI64().toString();
case ValueKind.BIGDECIMAL:
return this.toBigDecimal().toString();
case ValueKind.BOOL:
Expand Down Expand Up @@ -338,6 +372,10 @@ export class Value {
return new Value(ValueKind.INT, n as u64);
}

static fromI64(n: i64): Value {
return new Value(ValueKind.INT8, n as i64);
}

static fromString(s: string): Value {
return new Value(ValueKind.STRING, changetype<u32>(s));
}
Expand Down Expand Up @@ -413,6 +451,17 @@ export class Value {
return Value.fromMatrix(out);
}

static fromI64Matrix(values: Array<Array<i64>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromI64(values[i][j]);
}
}
return Value.fromMatrix(out);
}

static fromBigIntMatrix(values: Array<Array<BigInt>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.