Skip to content

Commit d132f9c

Browse files
dotansimhasaihaj
andauthored
New Timestamp scalar (#1522)
* New `Timestamp` scalar * fix timestamp --------- Co-authored-by: Saihajpreet Singh <[email protected]>
1 parent 561bdd5 commit d132f9c

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

.changeset/cold-shoes-thank.md

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

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

+21
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ describe.concurrent('Schema code generator', () => {
7777
7878
# New scalars
7979
int8: Int8!
80+
timestamp: Timestamp!
8081
}
8182
8283
type Wallet @entity {
@@ -287,6 +288,26 @@ describe.concurrent('Schema code generator', () => {
287288
this.set('int8', Value.fromI64(value))
288289
`,
289290
},
291+
{
292+
name: 'get timestamp',
293+
params: [],
294+
returnType: new NamedType('i64'),
295+
body: `let value = this.get('timestamp')
296+
if (!value || value.kind == ValueKind.NULL) {
297+
return 0
298+
} else {
299+
return value.toTimestamp()
300+
}
301+
`,
302+
},
303+
{
304+
name: 'set timestamp',
305+
params: [new Param('value', new NamedType('i64'))],
306+
returnType: undefined,
307+
body: `
308+
this.set('timestamp', Value.fromTimestamp(value))
309+
`,
310+
},
290311
{
291312
name: 'get wallets',
292313
params: [],

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

+5
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
271271
['[Boolean]', 'Array<boolean>', (code: any) => `${code}.toBooleanArray()`],
272272
['[Int]', 'Array<i32>', (code: any) => `${code}.toI32Array()`],
273273
['[Int8]', 'Array<i64>', (code: any) => `${code}.toI64Array()`],
274+
['[Timestamp]', 'Array<i64>', (code: any) => `${code}.toTimestampArray()`],
274275
['[BigInt]', 'Array<BigInt>', (code: any) => `${code}.toBigIntArray()`],
275276
['[ID]', 'Array<string>', (code: any) => `${code}.toStringArray()`],
276277
['[String]', 'Array<string>', (code: any) => `${code}.toStringArray()`],
@@ -287,6 +288,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
287288
['ID', 'string', (code: any) => `${code}.toString()`],
288289
['String', 'string', (code: any) => `${code}.toString()`],
289290
['BigDecimal', 'BigDecimal', (code: any) => `${code}.toBigDecimal()`],
291+
['Timestamp', 'i64', (code: any) => `${code}.toTimestamp()`],
290292
[/.*/, 'string', (code: any) => `${code}.toString()`],
291293
];
292294

@@ -305,6 +307,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
305307
['Array<Array<boolean>>', '[[Boolean]]', (code: any) => `Value.fromBooleanMatrix(${code})`],
306308
['Array<Array<i32>>', '[[Int]]', (code: any) => `Value.fromI32Matrix(${code})`],
307309
['Array<Array<i64>>', '[[Int8]]', (code: any) => `Value.fromI64Matrix(${code})`],
310+
['Array<Array<i64>>', '[[Timestamp]]', (code: any) => `Value.fromTimestampMatrix(${code})`],
308311
['Array<Array<BigInt>>', '[[BigInt]]', (code: any) => `Value.fromBigIntMatrix(${code})`],
309312
['Array<Array<string>>', '[[String]]', (code: any) => `Value.fromStringMatrix(${code})`],
310313
['Array<Array<string>>', '[[ID]]', (code: any) => `Value.fromStringMatrix(${code})`],
@@ -323,6 +326,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
323326
['Array<boolean>', '[Boolean]', (code: any) => `Value.fromBooleanArray(${code})`],
324327
['Array<i32>', '[Int]', (code: any) => `Value.fromI32Array(${code})`],
325328
['Array<i64>', '[Int8]', (code: any) => `Value.fromI64Array(${code})`],
329+
['Array<i64>', '[Timestamp]', (code: any) => `Value.fromTimestampArray(${code})`],
326330
['Array<BigInt>', '[BigInt]', (code: any) => `Value.fromBigIntArray(${code})`],
327331
['Array<string>', '[String]', (code: any) => `Value.fromStringArray(${code})`],
328332
['Array<string>', '[ID]', (code: any) => `Value.fromStringArray(${code})`],
@@ -337,6 +341,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
337341
['boolean', 'Boolean', (code: any) => `Value.fromBoolean(${code})`],
338342
['i32', 'Int', (code: any) => `Value.fromI32(${code})`],
339343
['i64', 'Int8', (code: any) => `Value.fromI64(${code})`],
344+
['i64', 'Timestamp', (code: any) => `Value.fromTimestamp(${code})`],
340345
['BigInt', 'BigInt', (code: any) => `Value.fromBigInt(${code})`],
341346
['string', 'String', (code: any) => `Value.fromString(${code})`],
342347
['string', 'ID', (code: any) => `Value.fromString(${code})`],

packages/cli/src/validation/schema.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const BUILTIN_SCALAR_TYPES = [
1818
'Bytes',
1919
'ID',
2020
'Int8',
21+
'Timestamp',
2122
];
2223

2324
// Type suggestions for common mistakes
@@ -39,6 +40,8 @@ const TYPE_SUGGESTIONS = [
3940
['Float', 'BigDecimal'],
4041
['int', 'Int'],
4142
['int8', 'Int8'],
43+
['timestamp', 'Timestamp'],
44+
['ts', 'Timestamp'],
4245
['uint', 'BigInt'],
4346
['owner', 'String'],
4447
['Owner', 'String'],

packages/ts/common/numbers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export declare namespace bigDecimal {
3030
}
3131

3232
export type Int8 = i64;
33+
export type Timestamp = i64;
3334

3435
/** An Ethereum address (20 bytes). */
3536
export class Address extends Bytes {

packages/ts/common/value.ts

+35
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export enum ValueKind {
1515
BYTES = 6,
1616
BIGINT = 7,
1717
INT8 = 8,
18+
TIMESTAMP = 9,
1819
}
1920

2021
const VALUE_KIND_NAMES = [
@@ -27,6 +28,7 @@ const VALUE_KIND_NAMES = [
2728
'Bytes',
2829
'BigInt',
2930
'Int8',
31+
'Timestamp',
3032
];
3133

3234
/**
@@ -79,6 +81,14 @@ export class Value {
7981
return this.data as i64;
8082
}
8183

84+
toTimestamp(): i64 {
85+
if (this.kind == ValueKind.NULL) {
86+
return 0;
87+
}
88+
assert(this.kind == ValueKind.TIMESTAMP, 'Value is not an i64.');
89+
return this.data as i64;
90+
}
91+
8292
toString(): string {
8393
assert(this.kind == ValueKind.STRING, 'Value is not a string.');
8494
return changetype<string>(this.data as u32);
@@ -153,6 +163,15 @@ export class Value {
153163
return output;
154164
}
155165

166+
toTimestampArray(): Array<i64> {
167+
const values = this.toArray();
168+
const output = new Array<i64>(values.length);
169+
for (let i: i32 = 0; i < values.length; i++) {
170+
output[i] = values[i].toTimestamp();
171+
}
172+
return output;
173+
}
174+
156175
toBigIntArray(): Array<BigInt> {
157176
const values = this.toArray();
158177
const output = new Array<BigInt>(values.length);
@@ -274,6 +293,7 @@ export class Value {
274293
case ValueKind.INT:
275294
return this.toI32().toString();
276295
case ValueKind.INT8:
296+
case ValueKind.TIMESTAMP:
277297
return this.toI64().toString();
278298
case ValueKind.BIGDECIMAL:
279299
return this.toBigDecimal().toString();
@@ -379,6 +399,10 @@ export class Value {
379399
return new Value(ValueKind.INT8, n as i64);
380400
}
381401

402+
static fromTimestamp(n: i64): Value {
403+
return new Value(ValueKind.TIMESTAMP, n as i64);
404+
}
405+
382406
static fromString(s: string): Value {
383407
return new Value(ValueKind.STRING, changetype<u32>(s));
384408
}
@@ -465,6 +489,17 @@ export class Value {
465489
return Value.fromMatrix(out);
466490
}
467491

492+
static fromTimestampMatrix(values: Array<Array<i64>>): Value {
493+
const out = new Array<Array<Value>>(values.length);
494+
for (let i: i32 = 0; i < values.length; i++) {
495+
out[i] = new Array<Value>(values[i].length);
496+
for (let j: i32 = 0; j < values[i].length; j++) {
497+
out[i][j] = Value.fromTimestamp(values[i][j]);
498+
}
499+
}
500+
return Value.fromMatrix(out);
501+
}
502+
468503
static fromBigIntMatrix(values: Array<Array<BigInt>>): Value {
469504
const out = new Array<Array<Value>>(values.length);
470505
for (let i: i32 = 0; i < values.length; i++) {

0 commit comments

Comments
 (0)