Skip to content

Commit

Permalink
New Timestamp scalar (#1522)
Browse files Browse the repository at this point in the history
* New `Timestamp` scalar

* fix timestamp

---------

Co-authored-by: Saihajpreet Singh <saihajpreet.singh@gmail.com>
  • Loading branch information
dotansimha and saihaj authored Mar 18, 2024
1 parent 561bdd5 commit d132f9c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/cold-shoes-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphprotocol/graph-cli': minor
'@graphprotocol/graph-ts': minor
---

Added support for handling GraphQL `Timestamp` scalar as `i64` (AssemblyScript)
21 changes: 21 additions & 0 deletions packages/cli/src/codegen/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe.concurrent('Schema code generator', () => {
# New scalars
int8: Int8!
timestamp: Timestamp!
}
type Wallet @entity {
Expand Down Expand Up @@ -287,6 +288,26 @@ describe.concurrent('Schema code generator', () => {
this.set('int8', Value.fromI64(value))
`,
},
{
name: 'get timestamp',
params: [],
returnType: new NamedType('i64'),
body: `let value = this.get('timestamp')
if (!value || value.kind == ValueKind.NULL) {
return 0
} else {
return value.toTimestamp()
}
`,
},
{
name: 'set timestamp',
params: [new Param('value', new NamedType('i64'))],
returnType: undefined,
body: `
this.set('timestamp', Value.fromTimestamp(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 @@ -271,6 +271,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
['[Boolean]', 'Array<boolean>', (code: any) => `${code}.toBooleanArray()`],
['[Int]', 'Array<i32>', (code: any) => `${code}.toI32Array()`],
['[Int8]', 'Array<i64>', (code: any) => `${code}.toI64Array()`],
['[Timestamp]', 'Array<i64>', (code: any) => `${code}.toTimestampArray()`],
['[BigInt]', 'Array<BigInt>', (code: any) => `${code}.toBigIntArray()`],
['[ID]', 'Array<string>', (code: any) => `${code}.toStringArray()`],
['[String]', 'Array<string>', (code: any) => `${code}.toStringArray()`],
Expand All @@ -287,6 +288,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
['ID', 'string', (code: any) => `${code}.toString()`],
['String', 'string', (code: any) => `${code}.toString()`],
['BigDecimal', 'BigDecimal', (code: any) => `${code}.toBigDecimal()`],
['Timestamp', 'i64', (code: any) => `${code}.toTimestamp()`],
[/.*/, 'string', (code: any) => `${code}.toString()`],
];

Expand All @@ -305,6 +307,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
['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<i64>>', '[[Timestamp]]', (code: any) => `Value.fromTimestampMatrix(${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 @@ -323,6 +326,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
['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<i64>', '[Timestamp]', (code: any) => `Value.fromTimestampArray(${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 @@ -337,6 +341,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
['boolean', 'Boolean', (code: any) => `Value.fromBoolean(${code})`],
['i32', 'Int', (code: any) => `Value.fromI32(${code})`],
['i64', 'Int8', (code: any) => `Value.fromI64(${code})`],
['i64', 'Timestamp', (code: any) => `Value.fromTimestamp(${code})`],
['BigInt', 'BigInt', (code: any) => `Value.fromBigInt(${code})`],
['string', 'String', (code: any) => `Value.fromString(${code})`],
['string', 'ID', (code: any) => `Value.fromString(${code})`],
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const BUILTIN_SCALAR_TYPES = [
'Bytes',
'ID',
'Int8',
'Timestamp',
];

// Type suggestions for common mistakes
Expand All @@ -39,6 +40,8 @@ const TYPE_SUGGESTIONS = [
['Float', 'BigDecimal'],
['int', 'Int'],
['int8', 'Int8'],
['timestamp', 'Timestamp'],
['ts', 'Timestamp'],
['uint', 'BigInt'],
['owner', 'String'],
['Owner', 'String'],
Expand Down
1 change: 1 addition & 0 deletions packages/ts/common/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export declare namespace bigDecimal {
}

export type Int8 = i64;
export type Timestamp = i64;

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

const VALUE_KIND_NAMES = [
Expand All @@ -27,6 +28,7 @@ const VALUE_KIND_NAMES = [
'Bytes',
'BigInt',
'Int8',
'Timestamp',
];

/**
Expand Down Expand Up @@ -79,6 +81,14 @@ export class Value {
return this.data as i64;
}

toTimestamp(): i64 {
if (this.kind == ValueKind.NULL) {
return 0;
}
assert(this.kind == ValueKind.TIMESTAMP, '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 @@ -153,6 +163,15 @@ export class Value {
return output;
}

toTimestampArray(): 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].toTimestamp();
}
return output;
}

toBigIntArray(): Array<BigInt> {
const values = this.toArray();
const output = new Array<BigInt>(values.length);
Expand Down Expand Up @@ -274,6 +293,7 @@ export class Value {
case ValueKind.INT:
return this.toI32().toString();
case ValueKind.INT8:
case ValueKind.TIMESTAMP:
return this.toI64().toString();
case ValueKind.BIGDECIMAL:
return this.toBigDecimal().toString();
Expand Down Expand Up @@ -379,6 +399,10 @@ export class Value {
return new Value(ValueKind.INT8, n as i64);
}

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

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

static fromTimestampMatrix(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.fromTimestamp(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

0 comments on commit d132f9c

Please sign in to comment.