Skip to content

Commit dab4ca1

Browse files
dotansimhasaihaj
andauthored
Added support for handling Int8 scalar as i64 (#1296)
* Added support for handling Int8 scalar as i64 * build locally and re-run --------- Co-authored-by: Saihajpreet Singh <[email protected]>
1 parent 2375877 commit dab4ca1

File tree

9 files changed

+119
-18
lines changed

9 files changed

+119
-18
lines changed

.changeset/twelve-tables-design.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphprotocol/graph-ts': minor
3+
'@graphprotocol/graph-cli': minor
4+
---
5+
6+
Added support for handling GraphQL `Int8` scalar as `i64` (AssemblyScript)

packages/cli/src/codegen/schema.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ describe('Schema code generator', () => {
7272
7373
# derivedFrom
7474
wallets: [Wallet!] @derivedFrom(field: "account")
75+
76+
# New scalars
77+
int8: Int8!
7578
}
7679
7780
type Wallet @entity {
@@ -262,6 +265,26 @@ describe('Schema code generator', () => {
262265
this.set('isActive', Value.fromBoolean(value))
263266
`,
264267
},
268+
{
269+
name: 'get int8',
270+
params: [],
271+
returnType: new NamedType('i64'),
272+
body: `let value = this.get('int8')
273+
if (!value || value.kind == ValueKind.NULL) {
274+
return 0
275+
} else {
276+
return value.toI64()
277+
}
278+
`,
279+
},
280+
{
281+
name: 'set int8',
282+
params: [new Param('value', new NamedType('i64'))],
283+
returnType: undefined,
284+
body: `
285+
this.set('int8', Value.fromI64(value))
286+
`,
287+
},
265288
{
266289
name: 'get wallets',
267290
params: [],

packages/cli/src/codegen/types/conversions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
270270
['[Bytes]', 'Array<Bytes>', (code: any) => `${code}.toBytesArray()`],
271271
['[Boolean]', 'Array<boolean>', (code: any) => `${code}.toBooleanArray()`],
272272
['[Int]', 'Array<i32>', (code: any) => `${code}.toI32Array()`],
273+
['[Int8]', 'Array<i64>', (code: any) => `${code}.toI64Array()`],
273274
['[BigInt]', 'Array<BigInt>', (code: any) => `${code}.toBigIntArray()`],
274275
['[ID]', 'Array<string>', (code: any) => `${code}.toStringArray()`],
275276
['[String]', 'Array<string>', (code: any) => `${code}.toStringArray()`],
@@ -281,6 +282,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
281282
['Bytes', 'Bytes', (code: any) => `${code}.toBytes()`],
282283
['Boolean', 'boolean', (code: any) => `${code}.toBoolean()`],
283284
['Int', 'i32', (code: any) => `${code}.toI32()`],
285+
['Int8', 'i64', (code: any) => `${code}.toI64()`],
284286
['BigInt', 'BigInt', (code: any) => `${code}.toBigInt()`],
285287
['ID', 'string', (code: any) => `${code}.toString()`],
286288
['String', 'string', (code: any) => `${code}.toString()`],
@@ -302,6 +304,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
302304
['Array<Array<Bytes>>', '[[Bytes]]', (code: any) => `Value.fromBytesMatrix(${code})`],
303305
['Array<Array<boolean>>', '[[Boolean]]', (code: any) => `Value.fromBooleanMatrix(${code})`],
304306
['Array<Array<i32>>', '[[Int]]', (code: any) => `Value.fromI32Matrix(${code})`],
307+
['Array<Array<i64>>', '[[Int8]]', (code: any) => `Value.fromI64Matrix(${code})`],
305308
['Array<Array<BigInt>>', '[[BigInt]]', (code: any) => `Value.fromBigIntMatrix(${code})`],
306309
['Array<Array<string>>', '[[String]]', (code: any) => `Value.fromStringMatrix(${code})`],
307310
['Array<Array<string>>', '[[ID]]', (code: any) => `Value.fromStringMatrix(${code})`],
@@ -319,6 +322,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
319322
['Array<Bytes>', '[Bytes]', (code: any) => `Value.fromBytesArray(${code})`],
320323
['Array<boolean>', '[Boolean]', (code: any) => `Value.fromBooleanArray(${code})`],
321324
['Array<i32>', '[Int]', (code: any) => `Value.fromI32Array(${code})`],
325+
['Array<i64>', '[Int8]', (code: any) => `Value.fromI64Array(${code})`],
322326
['Array<BigInt>', '[BigInt]', (code: any) => `Value.fromBigIntArray(${code})`],
323327
['Array<string>', '[String]', (code: any) => `Value.fromStringArray(${code})`],
324328
['Array<string>', '[ID]', (code: any) => `Value.fromStringArray(${code})`],
@@ -332,6 +336,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
332336
['Bytes', 'Bytes', (code: any) => `Value.fromBytes(${code})`],
333337
['boolean', 'Boolean', (code: any) => `Value.fromBoolean(${code})`],
334338
['i32', 'Int', (code: any) => `Value.fromI32(${code})`],
339+
['i64', 'Int8', (code: any) => `Value.fromI64(${code})`],
335340
['BigInt', 'BigInt', (code: any) => `Value.fromBigInt(${code})`],
336341
['string', 'String', (code: any) => `Value.fromString(${code})`],
337342
['string', 'ID', (code: any) => `Value.fromString(${code})`],

packages/cli/src/validation/schema.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ describe('Schema validation', () => {
1414
expect(typeSuggestion(`int`)).toBe('Int');
1515
expect(typeSuggestion(`uint`)).toBe('BigInt');
1616
expect(typeSuggestion(`uint32`)).toBe('BigInt');
17+
expect(typeSuggestion(`int8`)).toBe('Int8');
18+
expect(typeSuggestion(`i8`)).toBe('Int8');
19+
expect(typeSuggestion(`u8`)).toBe('Int8');
20+
expect(typeSuggestion(`uint8`)).toBe('Int8');
1721

18-
// Test i8..i32, int8..int32
19-
for (let i = 8; i <= 32; i += 8) {
22+
// Test i16..i32, int17..int32
23+
for (let i = 16; i <= 32; i += 8) {
2024
expect(typeSuggestion(`i${i}`)).toBe('Int');
2125
expect(typeSuggestion(`int${i}`)).toBe('Int');
2226
}
2327

24-
// Test u8..u24, uint8..uint24
25-
for (let i = 8; i <= 24; i += 8) {
28+
// Test u16..u24, uint16..uint24
29+
for (let i = 16; i <= 24; i += 8) {
2630
expect(typeSuggestion(`u${i}`)).toBe('Int');
2731
expect(typeSuggestion(`uint${i}`)).toBe('Int');
2832
}

packages/cli/src/validation/schema.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ const List = immutable.List;
66
const Set = immutable.Set;
77

88
// Builtin scalar types
9-
const BUILTIN_SCALAR_TYPES = ['Boolean', 'Int', 'BigDecimal', 'String', 'BigInt', 'Bytes', 'ID'];
9+
const BUILTIN_SCALAR_TYPES = [
10+
'Boolean',
11+
'Int',
12+
'BigDecimal',
13+
'String',
14+
'BigInt',
15+
'Bytes',
16+
'ID',
17+
'Int8',
18+
];
1019

1120
// Type suggestions for common mistakes
1221
const TYPE_SUGGESTIONS = [
@@ -26,9 +35,12 @@ const TYPE_SUGGESTIONS = [
2635
['float', 'BigDecimal'],
2736
['Float', 'BigDecimal'],
2837
['int', 'Int'],
38+
['int8', 'Int8'],
2939
['uint', 'BigInt'],
3040
['owner', 'String'],
3141
['Owner', 'String'],
42+
[/^(u|uint)8$/, 'Int8'],
43+
[/^(i|int)8$/, 'Int8'],
3244
[/^(u|uint)(8|16|24)$/, 'Int'],
3345
[/^(i|int)(8|16|24|32)$/, 'Int'],
3446
[/^(u|uint)32$/, 'BigInt'],

packages/cli/tests/cli/validation/invalid-entity-field-types.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
- Field 'int': Unknown type 'int'. Did you mean 'Int'?
1414
- Field 'uint': Unknown type 'uint'. Did you mean 'BigInt'?
1515
- Field 'uint32': Unknown type 'uint32'. Did you mean 'BigInt'?
16-
- Field 'i8': Unknown type 'i8'. Did you mean 'Int'?
16+
- Field 'i8': Unknown type 'i8'. Did you mean 'Int8'?
1717
- Field 'i16': Unknown type 'i16'. Did you mean 'Int'?
1818
- Field 'i24': Unknown type 'i24'. Did you mean 'Int'?
1919
- Field 'i32': Unknown type 'i32'. Did you mean 'Int'?
@@ -46,7 +46,7 @@
4646
- Field 'i248': Unknown type 'i248'. Did you mean 'BigInt'?
4747
- Field 'i256': Unknown type 'i256'. Did you mean 'BigInt'?
4848
- Field 'i256': Unknown type 'i256'. Did you mean 'BigInt'?
49-
- Field 'int8': Unknown type 'int8'. Did you mean 'Int'?
49+
- Field 'int8': Unknown type 'int8'. Did you mean 'Int8'?
5050
- Field 'int16': Unknown type 'int16'. Did you mean 'Int'?
5151
- Field 'int24': Unknown type 'int24'. Did you mean 'Int'?
5252
- Field 'int32': Unknown type 'int32'. Did you mean 'Int'?
@@ -79,7 +79,7 @@
7979
- Field 'int248': Unknown type 'int248'. Did you mean 'BigInt'?
8080
- Field 'int256': Unknown type 'int256'. Did you mean 'BigInt'?
8181
- Field 'int256': Unknown type 'int256'. Did you mean 'BigInt'?
82-
- Field 'u8': Unknown type 'u8'. Did you mean 'Int'?
82+
- Field 'u8': Unknown type 'u8'. Did you mean 'Int8'?
8383
- Field 'u16': Unknown type 'u16'. Did you mean 'Int'?
8484
- Field 'u24': Unknown type 'u24'. Did you mean 'Int'?
8585
- Field 'u32': Unknown type 'u32'. Did you mean 'BigInt'?
@@ -112,7 +112,7 @@
112112
- Field 'u248': Unknown type 'u248'. Did you mean 'BigInt'?
113113
- Field 'u256': Unknown type 'u256'. Did you mean 'BigInt'?
114114
- Field 'u256': Unknown type 'u256'. Did you mean 'BigInt'?
115-
- Field 'uint8': Unknown type 'uint8'. Did you mean 'Int'?
115+
- Field 'uint8': Unknown type 'uint8'. Did you mean 'Int8'?
116116
- Field 'uint16': Unknown type 'uint16'. Did you mean 'Int'?
117117
- Field 'uint24': Unknown type 'uint24'. Did you mean 'Int'?
118118
- Field 'uint32': Unknown type 'uint32'. Did you mean 'BigInt'?

packages/ts/common/numbers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export declare namespace bigDecimal {
2929
function fromString(s: string): BigDecimal;
3030
}
3131

32+
export type Int8 = i64;
33+
3234
/** An Ethereum address (20 bytes). */
3335
export class Address extends Bytes {
3436
static fromString(s: string): Address {

packages/ts/common/value.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export enum ValueKind {
1414
NULL = 5,
1515
BYTES = 6,
1616
BIGINT = 7,
17+
INT8 = 8,
1718
}
1819

1920
const VALUE_KIND_NAMES = [
@@ -25,6 +26,7 @@ const VALUE_KIND_NAMES = [
2526
'null',
2627
'Bytes',
2728
'BigInt',
29+
'Int8',
2830
];
2931

3032
/**
@@ -66,6 +68,14 @@ export class Value {
6668
return this.data as i32;
6769
}
6870

71+
toI64(): i64 {
72+
if (this.kind == ValueKind.NULL) {
73+
return 0;
74+
}
75+
assert(this.kind == ValueKind.INT8, 'Value is not an i64.');
76+
return this.data as i64;
77+
}
78+
6979
toString(): string {
7080
assert(this.kind == ValueKind.STRING, 'Value is not a string.');
7181
return changetype<string>(this.data as u32);
@@ -131,6 +141,15 @@ export class Value {
131141
return output;
132142
}
133143

144+
toI64Array(): Array<i64> {
145+
const values = this.toArray();
146+
const output = new Array<i64>(values.length);
147+
for (let i: i32 = 0; i < values.length; i++) {
148+
output[i] = values[i].toI32();
149+
}
150+
return output;
151+
}
152+
134153
toBigIntArray(): Array<BigInt> {
135154
const values = this.toArray();
136155
const output = new Array<BigInt>(values.length);
@@ -209,6 +228,19 @@ export class Value {
209228
return out;
210229
}
211230

231+
toI64Matrix(): Array<Array<i64>> {
232+
const valueMatrix = this.toMatrix();
233+
const out = new Array<Array<i64>>(valueMatrix.length);
234+
235+
for (let i: i32 = 0; i < valueMatrix.length; i++) {
236+
out[i] = new Array<i64>(valueMatrix[i].length);
237+
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
238+
out[i][j] = valueMatrix[i][j].toI64();
239+
}
240+
}
241+
return out;
242+
}
243+
212244
toBigIntMatrix(): Array<Array<BigInt>> {
213245
const valueMatrix = this.toMatrix();
214246
const out = new Array<Array<BigInt>>(valueMatrix.length);
@@ -238,6 +270,8 @@ export class Value {
238270
return this.toString();
239271
case ValueKind.INT:
240272
return this.toI32().toString();
273+
case ValueKind.INT8:
274+
return this.toI64().toString();
241275
case ValueKind.BIGDECIMAL:
242276
return this.toBigDecimal().toString();
243277
case ValueKind.BOOL:
@@ -338,6 +372,10 @@ export class Value {
338372
return new Value(ValueKind.INT, n as u64);
339373
}
340374

375+
static fromI64(n: i64): Value {
376+
return new Value(ValueKind.INT8, n as i64);
377+
}
378+
341379
static fromString(s: string): Value {
342380
return new Value(ValueKind.STRING, changetype<u32>(s));
343381
}
@@ -413,6 +451,17 @@ export class Value {
413451
return Value.fromMatrix(out);
414452
}
415453

454+
static fromI64Matrix(values: Array<Array<i64>>): Value {
455+
const out = new Array<Array<Value>>(values.length);
456+
for (let i: i32 = 0; i < values.length; i++) {
457+
out[i] = new Array<Value>(values[i].length);
458+
for (let j: i32 = 0; j < values[i].length; j++) {
459+
out[i][j] = Value.fromI64(values[i][j]);
460+
}
461+
}
462+
return Value.fromMatrix(out);
463+
}
464+
416465
static fromBigIntMatrix(values: Array<Array<BigInt>>): Value {
417466
const out = new Array<Array<Value>>(values.length);
418467
for (let i: i32 = 0; i < values.length; i++) {

pnpm-lock.yaml

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)