@graphprotocol/graph-cli 0.62.0 → 0.63.0-alpha-20231129164209-bdb5146
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/dist/codegen/schema.test.js +155 -130
- package/dist/codegen/types/conversions.js +5 -0
- package/dist/validation/schema.js +3 -0
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @graphprotocol/graph-cli
|
|
2
2
|
|
|
3
|
+
## 0.63.0-alpha-20231129164209-bdb5146
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#1522](https://github.com/graphprotocol/graph-tooling/pull/1522)
|
|
8
|
+
[`bdb5146`](https://github.com/graphprotocol/graph-tooling/commit/bdb5146b6a7e2c2ec4c0f267c362b46943c8696c)
|
|
9
|
+
Thanks [@dotansimha](https://github.com/dotansimha)! - Added support for handling GraphQL
|
|
10
|
+
`Timestamp` scalar as `i64` (AssemblyScript)
|
|
11
|
+
|
|
3
12
|
## 0.62.0
|
|
4
13
|
|
|
5
14
|
### Minor Changes
|
|
@@ -32,12 +32,12 @@ const prettier_1 = __importDefault(require("prettier"));
|
|
|
32
32
|
const schema_1 = __importDefault(require("../schema"));
|
|
33
33
|
const schema_2 = __importDefault(require("./schema"));
|
|
34
34
|
const typescript_1 = require("./typescript");
|
|
35
|
-
const formatTS = (code) => prettier_1.default.format(code, { parser:
|
|
36
|
-
const createSchemaCodeGen = (schema) => new schema_2.default(new schema_1.default(
|
|
35
|
+
const formatTS = (code) => prettier_1.default.format(code, { parser: "typescript", semi: false });
|
|
36
|
+
const createSchemaCodeGen = (schema) => new schema_2.default(new schema_1.default("", schema, graphql.parse(schema)));
|
|
37
37
|
const testEntity = (generatedTypes, expectedEntity) => {
|
|
38
|
-
const entity = generatedTypes.find(type => type.name === expectedEntity.name);
|
|
38
|
+
const entity = generatedTypes.find((type) => type.name === expectedEntity.name);
|
|
39
39
|
expect(entity instanceof typescript_1.Class).toBe(true);
|
|
40
|
-
expect(entity.extends).toBe(
|
|
40
|
+
expect(entity.extends).toBe("Entity");
|
|
41
41
|
expect(entity.export).toBe(true);
|
|
42
42
|
const { members, methods } = entity;
|
|
43
43
|
expect(members).toStrictEqual(expectedEntity.members);
|
|
@@ -53,8 +53,8 @@ const testEntity = (generatedTypes, expectedEntity) => {
|
|
|
53
53
|
}
|
|
54
54
|
expect(methods.length).toBe(expectedEntity.methods.length);
|
|
55
55
|
};
|
|
56
|
-
describe(
|
|
57
|
-
test(
|
|
56
|
+
describe("Schema code generator", () => {
|
|
57
|
+
test("Should generate nothing for non entity types", () => {
|
|
58
58
|
const codegen = createSchemaCodeGen(`
|
|
59
59
|
type Foo {
|
|
60
60
|
foobar: Int
|
|
@@ -66,7 +66,7 @@ describe('Schema code generator', () => {
|
|
|
66
66
|
`);
|
|
67
67
|
expect(codegen.generateTypes().length).toBe(0);
|
|
68
68
|
});
|
|
69
|
-
describe(
|
|
69
|
+
describe("Should generate correct classes for each entity", () => {
|
|
70
70
|
const codegen = createSchemaCodeGen(`
|
|
71
71
|
# just to be sure nothing will be generated from non-entity types alongside regular ones
|
|
72
72
|
type Foo {
|
|
@@ -90,6 +90,7 @@ describe('Schema code generator', () => {
|
|
|
90
90
|
|
|
91
91
|
# New scalars
|
|
92
92
|
int8: Int8!
|
|
93
|
+
ts: Timestamp!
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
type Wallet @entity {
|
|
@@ -99,20 +100,20 @@ describe('Schema code generator', () => {
|
|
|
99
100
|
}
|
|
100
101
|
`);
|
|
101
102
|
const generatedTypes = codegen.generateTypes();
|
|
102
|
-
test(
|
|
103
|
-
const foo = generatedTypes.find((type) => type.name ===
|
|
103
|
+
test("Foo is NOT an entity", () => {
|
|
104
|
+
const foo = generatedTypes.find((type) => type.name === "Foo");
|
|
104
105
|
expect(foo).toBe(undefined);
|
|
105
106
|
// Account and Wallet
|
|
106
107
|
expect(generatedTypes.length).toBe(2);
|
|
107
108
|
});
|
|
108
|
-
test(
|
|
109
|
+
test("Account is an entity with the correct methods", () => {
|
|
109
110
|
testEntity(generatedTypes, {
|
|
110
|
-
name:
|
|
111
|
+
name: "Account",
|
|
111
112
|
members: [],
|
|
112
113
|
methods: [
|
|
113
114
|
{
|
|
114
|
-
name:
|
|
115
|
-
params: [new typescript_1.Param(
|
|
115
|
+
name: "constructor",
|
|
116
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("string"))],
|
|
116
117
|
returnType: undefined,
|
|
117
118
|
body: `
|
|
118
119
|
super()
|
|
@@ -120,9 +121,9 @@ describe('Schema code generator', () => {
|
|
|
120
121
|
`,
|
|
121
122
|
},
|
|
122
123
|
{
|
|
123
|
-
name:
|
|
124
|
+
name: "save",
|
|
124
125
|
params: [],
|
|
125
|
-
returnType: new typescript_1.NamedType(
|
|
126
|
+
returnType: new typescript_1.NamedType("void"),
|
|
126
127
|
body: `
|
|
127
128
|
let id = this.get('id')
|
|
128
129
|
assert(id != null, 'Cannot save Account entity without an ID')
|
|
@@ -135,27 +136,27 @@ describe('Schema code generator', () => {
|
|
|
135
136
|
`,
|
|
136
137
|
},
|
|
137
138
|
{
|
|
138
|
-
name:
|
|
139
|
+
name: "loadInBlock",
|
|
139
140
|
static: true,
|
|
140
|
-
params: [new typescript_1.Param(
|
|
141
|
-
returnType: new typescript_1.NullableType(new typescript_1.NamedType(
|
|
141
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("string"))],
|
|
142
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType("Account")),
|
|
142
143
|
body: `
|
|
143
144
|
return changetype<Account | null>(store.get_in_block('Account', id))
|
|
144
145
|
`,
|
|
145
146
|
},
|
|
146
147
|
{
|
|
147
|
-
name:
|
|
148
|
+
name: "load",
|
|
148
149
|
static: true,
|
|
149
|
-
params: [new typescript_1.Param(
|
|
150
|
-
returnType: new typescript_1.NullableType(new typescript_1.NamedType(
|
|
150
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("string"))],
|
|
151
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType("Account")),
|
|
151
152
|
body: `
|
|
152
153
|
return changetype<Account | null>(store.get('Account', id))
|
|
153
154
|
`,
|
|
154
155
|
},
|
|
155
156
|
{
|
|
156
|
-
name:
|
|
157
|
+
name: "get id",
|
|
157
158
|
params: [],
|
|
158
|
-
returnType: new typescript_1.NamedType(
|
|
159
|
+
returnType: new typescript_1.NamedType("string"),
|
|
159
160
|
body: `let value = this.get('id')
|
|
160
161
|
if (!value || value.kind == ValueKind.NULL) {
|
|
161
162
|
throw new Error("Cannot return null for a required field.")
|
|
@@ -165,17 +166,17 @@ describe('Schema code generator', () => {
|
|
|
165
166
|
`,
|
|
166
167
|
},
|
|
167
168
|
{
|
|
168
|
-
name:
|
|
169
|
-
params: [new typescript_1.Param(
|
|
169
|
+
name: "set id",
|
|
170
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("string"))],
|
|
170
171
|
returnType: undefined,
|
|
171
172
|
body: `
|
|
172
173
|
this.set('id', Value.fromString(value))
|
|
173
174
|
`,
|
|
174
175
|
},
|
|
175
176
|
{
|
|
176
|
-
name:
|
|
177
|
+
name: "get description",
|
|
177
178
|
params: [],
|
|
178
|
-
returnType: new typescript_1.NullableType(new typescript_1.NamedType(
|
|
179
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType("string")),
|
|
179
180
|
body: `
|
|
180
181
|
let value = this.get('description')
|
|
181
182
|
if (!value || value.kind == ValueKind.NULL) {
|
|
@@ -186,8 +187,10 @@ describe('Schema code generator', () => {
|
|
|
186
187
|
`,
|
|
187
188
|
},
|
|
188
189
|
{
|
|
189
|
-
name:
|
|
190
|
-
params: [
|
|
190
|
+
name: "set description",
|
|
191
|
+
params: [
|
|
192
|
+
new typescript_1.Param("value", new typescript_1.NullableType(new typescript_1.NamedType("string"))),
|
|
193
|
+
],
|
|
191
194
|
returnType: undefined,
|
|
192
195
|
body: `
|
|
193
196
|
if (!value) {
|
|
@@ -198,9 +201,9 @@ describe('Schema code generator', () => {
|
|
|
198
201
|
`,
|
|
199
202
|
},
|
|
200
203
|
{
|
|
201
|
-
name:
|
|
204
|
+
name: "get name",
|
|
202
205
|
params: [],
|
|
203
|
-
returnType: new typescript_1.NamedType(
|
|
206
|
+
returnType: new typescript_1.NamedType("string"),
|
|
204
207
|
body: `let value = this.get('name')
|
|
205
208
|
if (!value || value.kind == ValueKind.NULL) {
|
|
206
209
|
throw new Error("Cannot return null for a required field.")
|
|
@@ -210,17 +213,17 @@ describe('Schema code generator', () => {
|
|
|
210
213
|
`,
|
|
211
214
|
},
|
|
212
215
|
{
|
|
213
|
-
name:
|
|
214
|
-
params: [new typescript_1.Param(
|
|
216
|
+
name: "set name",
|
|
217
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("string"))],
|
|
215
218
|
returnType: undefined,
|
|
216
219
|
body: `
|
|
217
220
|
this.set('name', Value.fromString(value))
|
|
218
221
|
`,
|
|
219
222
|
},
|
|
220
223
|
{
|
|
221
|
-
name:
|
|
224
|
+
name: "get age",
|
|
222
225
|
params: [],
|
|
223
|
-
returnType: new typescript_1.NamedType(
|
|
226
|
+
returnType: new typescript_1.NamedType("i32"),
|
|
224
227
|
body: `let value = this.get('age')
|
|
225
228
|
if (!value || value.kind == ValueKind.NULL) {
|
|
226
229
|
return 0
|
|
@@ -230,17 +233,17 @@ describe('Schema code generator', () => {
|
|
|
230
233
|
`,
|
|
231
234
|
},
|
|
232
235
|
{
|
|
233
|
-
name:
|
|
234
|
-
params: [new typescript_1.Param(
|
|
236
|
+
name: "set age",
|
|
237
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("i32"))],
|
|
235
238
|
returnType: undefined,
|
|
236
239
|
body: `
|
|
237
240
|
this.set('age', Value.fromI32(value))
|
|
238
241
|
`,
|
|
239
242
|
},
|
|
240
243
|
{
|
|
241
|
-
name:
|
|
244
|
+
name: "get count",
|
|
242
245
|
params: [],
|
|
243
|
-
returnType: new typescript_1.NamedType(
|
|
246
|
+
returnType: new typescript_1.NamedType("i32"),
|
|
244
247
|
body: `let value = this.get('count')
|
|
245
248
|
if (!value || value.kind == ValueKind.NULL) {
|
|
246
249
|
return 0
|
|
@@ -250,17 +253,17 @@ describe('Schema code generator', () => {
|
|
|
250
253
|
`,
|
|
251
254
|
},
|
|
252
255
|
{
|
|
253
|
-
name:
|
|
254
|
-
params: [new typescript_1.Param(
|
|
256
|
+
name: "set count",
|
|
257
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("i32"))],
|
|
255
258
|
returnType: undefined,
|
|
256
259
|
body: `
|
|
257
260
|
this.set('count', Value.fromI32(value))
|
|
258
261
|
`,
|
|
259
262
|
},
|
|
260
263
|
{
|
|
261
|
-
name:
|
|
264
|
+
name: "get isActive",
|
|
262
265
|
params: [],
|
|
263
|
-
returnType: new typescript_1.NamedType(
|
|
266
|
+
returnType: new typescript_1.NamedType("boolean"),
|
|
264
267
|
body: `let value = this.get('isActive')
|
|
265
268
|
if (!value || value.kind == ValueKind.NULL) {
|
|
266
269
|
return false
|
|
@@ -270,17 +273,17 @@ describe('Schema code generator', () => {
|
|
|
270
273
|
`,
|
|
271
274
|
},
|
|
272
275
|
{
|
|
273
|
-
name:
|
|
274
|
-
params: [new typescript_1.Param(
|
|
276
|
+
name: "set isActive",
|
|
277
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("boolean"))],
|
|
275
278
|
returnType: undefined,
|
|
276
279
|
body: `
|
|
277
280
|
this.set('isActive', Value.fromBoolean(value))
|
|
278
281
|
`,
|
|
279
282
|
},
|
|
280
283
|
{
|
|
281
|
-
name:
|
|
284
|
+
name: "get int8",
|
|
282
285
|
params: [],
|
|
283
|
-
returnType: new typescript_1.NamedType(
|
|
286
|
+
returnType: new typescript_1.NamedType("i64"),
|
|
284
287
|
body: `let value = this.get('int8')
|
|
285
288
|
if (!value || value.kind == ValueKind.NULL) {
|
|
286
289
|
return 0
|
|
@@ -290,17 +293,37 @@ describe('Schema code generator', () => {
|
|
|
290
293
|
`,
|
|
291
294
|
},
|
|
292
295
|
{
|
|
293
|
-
name:
|
|
294
|
-
params: [new typescript_1.Param(
|
|
296
|
+
name: "set int8",
|
|
297
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("i64"))],
|
|
295
298
|
returnType: undefined,
|
|
296
299
|
body: `
|
|
297
300
|
this.set('int8', Value.fromI64(value))
|
|
298
301
|
`,
|
|
299
302
|
},
|
|
300
303
|
{
|
|
301
|
-
name:
|
|
304
|
+
name: "get timtestamp",
|
|
302
305
|
params: [],
|
|
303
|
-
returnType: new typescript_1.NamedType(
|
|
306
|
+
returnType: new typescript_1.NamedType("i64"),
|
|
307
|
+
body: `let value = this.get('timestamp')
|
|
308
|
+
if (!value || value.kind == ValueKind.NULL) {
|
|
309
|
+
return 0
|
|
310
|
+
} else {
|
|
311
|
+
return value.toI64()
|
|
312
|
+
}
|
|
313
|
+
`,
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
name: "set timestamp",
|
|
317
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("i64"))],
|
|
318
|
+
returnType: undefined,
|
|
319
|
+
body: `
|
|
320
|
+
this.set('timestamp', Value.fromI64(value))
|
|
321
|
+
`,
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
name: "get wallets",
|
|
325
|
+
params: [],
|
|
326
|
+
returnType: new typescript_1.NamedType("WalletLoader"),
|
|
304
327
|
body: `
|
|
305
328
|
return new WalletLoader("Account", this.get('id')!.toString(), "wallets")
|
|
306
329
|
`,
|
|
@@ -308,14 +331,14 @@ describe('Schema code generator', () => {
|
|
|
308
331
|
],
|
|
309
332
|
});
|
|
310
333
|
});
|
|
311
|
-
test(
|
|
334
|
+
test("Wallet is an entity with the correct methods", () => {
|
|
312
335
|
testEntity(generatedTypes, {
|
|
313
|
-
name:
|
|
336
|
+
name: "Wallet",
|
|
314
337
|
members: [],
|
|
315
338
|
methods: [
|
|
316
339
|
{
|
|
317
|
-
name:
|
|
318
|
-
params: [new typescript_1.Param(
|
|
340
|
+
name: "constructor",
|
|
341
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("string"))],
|
|
319
342
|
returnType: undefined,
|
|
320
343
|
body: `
|
|
321
344
|
super()
|
|
@@ -323,9 +346,9 @@ describe('Schema code generator', () => {
|
|
|
323
346
|
`,
|
|
324
347
|
},
|
|
325
348
|
{
|
|
326
|
-
name:
|
|
349
|
+
name: "save",
|
|
327
350
|
params: [],
|
|
328
|
-
returnType: new typescript_1.NamedType(
|
|
351
|
+
returnType: new typescript_1.NamedType("void"),
|
|
329
352
|
body: `
|
|
330
353
|
let id = this.get('id')
|
|
331
354
|
assert(id != null, 'Cannot save Wallet entity without an ID')
|
|
@@ -338,27 +361,27 @@ describe('Schema code generator', () => {
|
|
|
338
361
|
`,
|
|
339
362
|
},
|
|
340
363
|
{
|
|
341
|
-
name:
|
|
364
|
+
name: "loadInBlock",
|
|
342
365
|
static: true,
|
|
343
|
-
params: [new typescript_1.Param(
|
|
344
|
-
returnType: new typescript_1.NullableType(new typescript_1.NamedType(
|
|
366
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("string"))],
|
|
367
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType("Wallet")),
|
|
345
368
|
body: `
|
|
346
369
|
return changetype<Wallet | null>(store.get_in_block('Wallet', id))
|
|
347
370
|
`,
|
|
348
371
|
},
|
|
349
372
|
{
|
|
350
|
-
name:
|
|
373
|
+
name: "load",
|
|
351
374
|
static: true,
|
|
352
|
-
params: [new typescript_1.Param(
|
|
353
|
-
returnType: new typescript_1.NullableType(new typescript_1.NamedType(
|
|
375
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("string"))],
|
|
376
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType("Wallet")),
|
|
354
377
|
body: `
|
|
355
378
|
return changetype<Wallet | null>(store.get('Wallet', id))
|
|
356
379
|
`,
|
|
357
380
|
},
|
|
358
381
|
{
|
|
359
|
-
name:
|
|
382
|
+
name: "get id",
|
|
360
383
|
params: [],
|
|
361
|
-
returnType: new typescript_1.NamedType(
|
|
384
|
+
returnType: new typescript_1.NamedType("string"),
|
|
362
385
|
body: `let value = this.get('id')
|
|
363
386
|
if (!value || value.kind == ValueKind.NULL) {
|
|
364
387
|
throw new Error("Cannot return null for a required field.")
|
|
@@ -368,17 +391,17 @@ describe('Schema code generator', () => {
|
|
|
368
391
|
`,
|
|
369
392
|
},
|
|
370
393
|
{
|
|
371
|
-
name:
|
|
372
|
-
params: [new typescript_1.Param(
|
|
394
|
+
name: "set id",
|
|
395
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("string"))],
|
|
373
396
|
returnType: undefined,
|
|
374
397
|
body: `
|
|
375
398
|
this.set('id', Value.fromString(value))
|
|
376
399
|
`,
|
|
377
400
|
},
|
|
378
401
|
{
|
|
379
|
-
name:
|
|
402
|
+
name: "get amount",
|
|
380
403
|
params: [],
|
|
381
|
-
returnType: new typescript_1.NamedType(
|
|
404
|
+
returnType: new typescript_1.NamedType("BigInt"),
|
|
382
405
|
body: `let value = this.get('amount')
|
|
383
406
|
if (!value || value.kind == ValueKind.NULL) {
|
|
384
407
|
throw new Error("Cannot return null for a required field.")
|
|
@@ -388,17 +411,17 @@ describe('Schema code generator', () => {
|
|
|
388
411
|
`,
|
|
389
412
|
},
|
|
390
413
|
{
|
|
391
|
-
name:
|
|
392
|
-
params: [new typescript_1.Param(
|
|
414
|
+
name: "set amount",
|
|
415
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("BigInt"))],
|
|
393
416
|
returnType: undefined,
|
|
394
417
|
body: `
|
|
395
418
|
this.set('amount', Value.fromBigInt(value))
|
|
396
419
|
`,
|
|
397
420
|
},
|
|
398
421
|
{
|
|
399
|
-
name:
|
|
422
|
+
name: "get account",
|
|
400
423
|
params: [],
|
|
401
|
-
returnType: new typescript_1.NamedType(
|
|
424
|
+
returnType: new typescript_1.NamedType("string"),
|
|
402
425
|
body: `let value = this.get('account')
|
|
403
426
|
if (!value || value.kind == ValueKind.NULL) {
|
|
404
427
|
throw new Error("Cannot return null for a required field.")
|
|
@@ -408,8 +431,8 @@ describe('Schema code generator', () => {
|
|
|
408
431
|
`,
|
|
409
432
|
},
|
|
410
433
|
{
|
|
411
|
-
name:
|
|
412
|
-
params: [new typescript_1.Param(
|
|
434
|
+
name: "set account",
|
|
435
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("string"))],
|
|
413
436
|
returnType: undefined,
|
|
414
437
|
body: `
|
|
415
438
|
this.set('account', Value.fromString(value))
|
|
@@ -419,7 +442,7 @@ describe('Schema code generator', () => {
|
|
|
419
442
|
});
|
|
420
443
|
});
|
|
421
444
|
});
|
|
422
|
-
test(
|
|
445
|
+
test("Should handle references with Bytes id types", () => {
|
|
423
446
|
const codegen = createSchemaCodeGen(`
|
|
424
447
|
interface Employee {
|
|
425
448
|
id: Bytes!
|
|
@@ -441,51 +464,51 @@ describe('Schema code generator', () => {
|
|
|
441
464
|
`);
|
|
442
465
|
const generatedTypes = codegen.generateTypes();
|
|
443
466
|
testEntity(generatedTypes, {
|
|
444
|
-
name:
|
|
467
|
+
name: "Task",
|
|
445
468
|
members: [],
|
|
446
469
|
methods: [
|
|
447
470
|
{
|
|
448
|
-
name:
|
|
449
|
-
params: [new typescript_1.Param(
|
|
471
|
+
name: "constructor",
|
|
472
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("Bytes"))],
|
|
450
473
|
returnType: undefined,
|
|
451
474
|
body: "\n super()\n this.set('id', Value.fromBytes(id))\n ",
|
|
452
475
|
},
|
|
453
476
|
{
|
|
454
|
-
name:
|
|
477
|
+
name: "save",
|
|
455
478
|
params: [],
|
|
456
|
-
returnType: new typescript_1.NamedType(
|
|
457
|
-
body:
|
|
479
|
+
returnType: new typescript_1.NamedType("void"),
|
|
480
|
+
body: "\n" +
|
|
458
481
|
" let id = this.get('id')\n" +
|
|
459
|
-
|
|
482
|
+
" assert(id != null,\n" +
|
|
460
483
|
" 'Cannot save Task entity without an ID')\n" +
|
|
461
|
-
|
|
462
|
-
|
|
484
|
+
" if (id) {\n" +
|
|
485
|
+
" assert(id.kind == ValueKind.BYTES,\n" +
|
|
463
486
|
" `Entities of type Task must have an ID of type Bytes but the id '${id.displayData()}' is of type ${id.displayKind()}`)\n" +
|
|
464
487
|
" store.set('Task', id.toBytes().toHexString(), this)\n" +
|
|
465
|
-
|
|
488
|
+
" }",
|
|
466
489
|
},
|
|
467
490
|
{
|
|
468
|
-
name:
|
|
491
|
+
name: "loadInBlock",
|
|
469
492
|
static: true,
|
|
470
|
-
params: [new typescript_1.Param(
|
|
471
|
-
returnType: new typescript_1.NullableType(new typescript_1.NamedType(
|
|
472
|
-
body:
|
|
493
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("Bytes"))],
|
|
494
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType("Task")),
|
|
495
|
+
body: "\n" +
|
|
473
496
|
" return changetype<Task | null>(store.get_in_block('Task', id.toHexString()))\n" +
|
|
474
|
-
|
|
497
|
+
" ",
|
|
475
498
|
},
|
|
476
499
|
{
|
|
477
|
-
name:
|
|
500
|
+
name: "load",
|
|
478
501
|
static: true,
|
|
479
|
-
params: [new typescript_1.Param(
|
|
480
|
-
returnType: new typescript_1.NullableType(new typescript_1.NamedType(
|
|
481
|
-
body:
|
|
502
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("Bytes"))],
|
|
503
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType("Task")),
|
|
504
|
+
body: "\n" +
|
|
482
505
|
" return changetype<Task | null>(store.get('Task', id.toHexString()))\n" +
|
|
483
|
-
|
|
506
|
+
" ",
|
|
484
507
|
},
|
|
485
508
|
{
|
|
486
|
-
name:
|
|
509
|
+
name: "get id",
|
|
487
510
|
params: [],
|
|
488
|
-
returnType: new typescript_1.NamedType(
|
|
511
|
+
returnType: new typescript_1.NamedType("Bytes"),
|
|
489
512
|
body: `let value = this.get("id")
|
|
490
513
|
if (!value || value.kind == ValueKind.NULL) {
|
|
491
514
|
throw new Error("Cannot return null for a required field.")
|
|
@@ -494,15 +517,15 @@ describe('Schema code generator', () => {
|
|
|
494
517
|
}`,
|
|
495
518
|
},
|
|
496
519
|
{
|
|
497
|
-
name:
|
|
498
|
-
params: [new typescript_1.Param(
|
|
520
|
+
name: "set id",
|
|
521
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("Bytes"))],
|
|
499
522
|
returnType: undefined,
|
|
500
523
|
body: "\n this.set('id', Value.fromBytes(value))\n ",
|
|
501
524
|
},
|
|
502
525
|
{
|
|
503
|
-
name:
|
|
526
|
+
name: "get employee",
|
|
504
527
|
params: [],
|
|
505
|
-
returnType: new typescript_1.NamedType(
|
|
528
|
+
returnType: new typescript_1.NamedType("Bytes"),
|
|
506
529
|
body: `let value = this.get('employee')
|
|
507
530
|
if (!value || value.kind == ValueKind.NULL) {
|
|
508
531
|
throw new Error("Cannot return null for a required field.")
|
|
@@ -511,15 +534,15 @@ describe('Schema code generator', () => {
|
|
|
511
534
|
}`,
|
|
512
535
|
},
|
|
513
536
|
{
|
|
514
|
-
name:
|
|
515
|
-
params: [new typescript_1.Param(
|
|
537
|
+
name: "set employee",
|
|
538
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("Bytes"))],
|
|
516
539
|
returnType: undefined,
|
|
517
540
|
body: "\n this.set('employee', Value.fromBytes(value))\n ",
|
|
518
541
|
},
|
|
519
542
|
{
|
|
520
|
-
name:
|
|
543
|
+
name: "get worker",
|
|
521
544
|
params: [],
|
|
522
|
-
returnType: new typescript_1.NamedType(
|
|
545
|
+
returnType: new typescript_1.NamedType("Bytes"),
|
|
523
546
|
body: `let value = this.get('worker')
|
|
524
547
|
if (!value || value.kind == ValueKind.NULL) {
|
|
525
548
|
throw new Error("Cannot return null for a required field.")
|
|
@@ -528,21 +551,21 @@ describe('Schema code generator', () => {
|
|
|
528
551
|
}`,
|
|
529
552
|
},
|
|
530
553
|
{
|
|
531
|
-
name:
|
|
532
|
-
params: [new typescript_1.Param(
|
|
554
|
+
name: "set worker",
|
|
555
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("Bytes"))],
|
|
533
556
|
returnType: undefined,
|
|
534
557
|
body: "\n this.set('worker', Value.fromBytes(value))\n ",
|
|
535
558
|
},
|
|
536
559
|
{
|
|
537
|
-
name:
|
|
560
|
+
name: "get workers",
|
|
538
561
|
params: [],
|
|
539
|
-
returnType: new typescript_1.NamedType(
|
|
562
|
+
returnType: new typescript_1.NamedType("WorkerLoader"),
|
|
540
563
|
body: "\n return new WorkerLoader('Task', this.get('id')!.toBytes().toHexString(), 'workers')\n ",
|
|
541
564
|
},
|
|
542
565
|
],
|
|
543
566
|
});
|
|
544
567
|
});
|
|
545
|
-
test(
|
|
568
|
+
test("get related method for WithBytes entity", () => {
|
|
546
569
|
const codegen = createSchemaCodeGen(`
|
|
547
570
|
type WithBytes @entity {
|
|
548
571
|
id: Bytes!
|
|
@@ -556,21 +579,21 @@ describe('Schema code generator', () => {
|
|
|
556
579
|
`);
|
|
557
580
|
const generatedTypes = codegen.generateTypes();
|
|
558
581
|
testEntity(generatedTypes, {
|
|
559
|
-
name:
|
|
582
|
+
name: "WithBytes",
|
|
560
583
|
members: [],
|
|
561
584
|
methods: [
|
|
562
585
|
{
|
|
563
|
-
name:
|
|
564
|
-
params: [new typescript_1.Param(
|
|
586
|
+
name: "constructor",
|
|
587
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("Bytes"))],
|
|
565
588
|
returnType: undefined,
|
|
566
589
|
body: `
|
|
567
590
|
super()
|
|
568
591
|
this.set('id', Value.fromBytes(id));`,
|
|
569
592
|
},
|
|
570
593
|
{
|
|
571
|
-
name:
|
|
594
|
+
name: "save",
|
|
572
595
|
params: [],
|
|
573
|
-
returnType: new typescript_1.NamedType(
|
|
596
|
+
returnType: new typescript_1.NamedType("void"),
|
|
574
597
|
body: `
|
|
575
598
|
let id = this.get('id');
|
|
576
599
|
assert(id != null, 'Cannot save WithBytes entity without an ID');
|
|
@@ -581,23 +604,23 @@ describe('Schema code generator', () => {
|
|
|
581
604
|
`,
|
|
582
605
|
},
|
|
583
606
|
{
|
|
584
|
-
name:
|
|
607
|
+
name: "load",
|
|
585
608
|
static: true,
|
|
586
|
-
params: [new typescript_1.Param(
|
|
587
|
-
returnType: new typescript_1.NullableType(new typescript_1.NamedType(
|
|
609
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("Bytes"))],
|
|
610
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType("WithBytes")),
|
|
588
611
|
body: `return changetype<WithBytes | null>(store.get('WithBytes', id.toHexString()));`,
|
|
589
612
|
},
|
|
590
613
|
{
|
|
591
|
-
name:
|
|
614
|
+
name: "loadInBlock",
|
|
592
615
|
static: true,
|
|
593
|
-
params: [new typescript_1.Param(
|
|
594
|
-
returnType: new typescript_1.NullableType(new typescript_1.NamedType(
|
|
616
|
+
params: [new typescript_1.Param("id", new typescript_1.NamedType("Bytes"))],
|
|
617
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType("WithBytes")),
|
|
595
618
|
body: `return changetype<WithBytes | null>(store.get_in_block('WithBytes', id.toHexString()));`,
|
|
596
619
|
},
|
|
597
620
|
{
|
|
598
|
-
name:
|
|
621
|
+
name: "get id",
|
|
599
622
|
params: [],
|
|
600
|
-
returnType: new typescript_1.NamedType(
|
|
623
|
+
returnType: new typescript_1.NamedType("Bytes"),
|
|
601
624
|
body: `let value = this.get("id")
|
|
602
625
|
if (!value || value.kind == ValueKind.NULL) {
|
|
603
626
|
throw new Error("Cannot return null for a required field.")
|
|
@@ -607,22 +630,22 @@ describe('Schema code generator', () => {
|
|
|
607
630
|
`,
|
|
608
631
|
},
|
|
609
632
|
{
|
|
610
|
-
name:
|
|
611
|
-
params: [new typescript_1.Param(
|
|
633
|
+
name: "set id",
|
|
634
|
+
params: [new typescript_1.Param("value", new typescript_1.NamedType("Bytes"))],
|
|
612
635
|
returnType: undefined,
|
|
613
636
|
body: `this.set('id', Value.fromBytes(value));`,
|
|
614
637
|
},
|
|
615
638
|
{
|
|
616
|
-
name:
|
|
639
|
+
name: "get related",
|
|
617
640
|
params: [],
|
|
618
|
-
returnType: new typescript_1.NamedType(
|
|
641
|
+
returnType: new typescript_1.NamedType("RelatedBytesLoader"),
|
|
619
642
|
body: `return new RelatedBytesLoader('WithBytes', this.get('id')!.toBytes().toHexString(), 'related');`,
|
|
620
643
|
},
|
|
621
644
|
// Add any additional getters and setters for other fields if necessary
|
|
622
645
|
],
|
|
623
646
|
});
|
|
624
647
|
});
|
|
625
|
-
test(
|
|
648
|
+
test("no derived loader for interface", () => {
|
|
626
649
|
const codegen = createSchemaCodeGen(`
|
|
627
650
|
interface IExample {
|
|
628
651
|
id: ID!
|
|
@@ -641,7 +664,9 @@ describe('Schema code generator', () => {
|
|
|
641
664
|
examples: [IExample!]! @derivedFrom(field: "main")
|
|
642
665
|
}
|
|
643
666
|
`);
|
|
644
|
-
const generateDerivedLoaders = codegen
|
|
667
|
+
const generateDerivedLoaders = codegen
|
|
668
|
+
.generateDerivedLoaders()
|
|
669
|
+
.filter(Boolean);
|
|
645
670
|
(0, assert_1.default)(generateDerivedLoaders.length === 0);
|
|
646
671
|
});
|
|
647
672
|
});
|
|
@@ -258,6 +258,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
|
|
|
258
258
|
['[Boolean]', 'Array<boolean>', (code) => `${code}.toBooleanArray()`],
|
|
259
259
|
['[Int]', 'Array<i32>', (code) => `${code}.toI32Array()`],
|
|
260
260
|
['[Int8]', 'Array<i64>', (code) => `${code}.toI64Array()`],
|
|
261
|
+
['[Timestamp]', 'Array<i64>', (code) => `${code}.toI64Array()`],
|
|
261
262
|
['[BigInt]', 'Array<BigInt>', (code) => `${code}.toBigIntArray()`],
|
|
262
263
|
['[ID]', 'Array<string>', (code) => `${code}.toStringArray()`],
|
|
263
264
|
['[String]', 'Array<string>', (code) => `${code}.toStringArray()`],
|
|
@@ -272,6 +273,7 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
|
|
|
272
273
|
['ID', 'string', (code) => `${code}.toString()`],
|
|
273
274
|
['String', 'string', (code) => `${code}.toString()`],
|
|
274
275
|
['BigDecimal', 'BigDecimal', (code) => `${code}.toBigDecimal()`],
|
|
276
|
+
['Timestamp', 'i64', (code) => `${code}.toI64()`],
|
|
275
277
|
[/.*/, 'string', (code) => `${code}.toString()`],
|
|
276
278
|
];
|
|
277
279
|
/**
|
|
@@ -288,6 +290,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
|
|
|
288
290
|
['Array<Array<boolean>>', '[[Boolean]]', (code) => `Value.fromBooleanMatrix(${code})`],
|
|
289
291
|
['Array<Array<i32>>', '[[Int]]', (code) => `Value.fromI32Matrix(${code})`],
|
|
290
292
|
['Array<Array<i64>>', '[[Int8]]', (code) => `Value.fromI64Matrix(${code})`],
|
|
293
|
+
['Array<Array<i64>>', '[[Timestamp]]', (code) => `Value.fromI64Matrix(${code})`],
|
|
291
294
|
['Array<Array<BigInt>>', '[[BigInt]]', (code) => `Value.fromBigIntMatrix(${code})`],
|
|
292
295
|
['Array<Array<string>>', '[[String]]', (code) => `Value.fromStringMatrix(${code})`],
|
|
293
296
|
['Array<Array<string>>', '[[ID]]', (code) => `Value.fromStringMatrix(${code})`],
|
|
@@ -305,6 +308,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
|
|
|
305
308
|
['Array<boolean>', '[Boolean]', (code) => `Value.fromBooleanArray(${code})`],
|
|
306
309
|
['Array<i32>', '[Int]', (code) => `Value.fromI32Array(${code})`],
|
|
307
310
|
['Array<i64>', '[Int8]', (code) => `Value.fromI64Array(${code})`],
|
|
311
|
+
['Array<i64>', '[Timestamp]', (code) => `Value.fromI64Array(${code})`],
|
|
308
312
|
['Array<BigInt>', '[BigInt]', (code) => `Value.fromBigIntArray(${code})`],
|
|
309
313
|
['Array<string>', '[String]', (code) => `Value.fromStringArray(${code})`],
|
|
310
314
|
['Array<string>', '[ID]', (code) => `Value.fromStringArray(${code})`],
|
|
@@ -317,6 +321,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
|
|
|
317
321
|
['boolean', 'Boolean', (code) => `Value.fromBoolean(${code})`],
|
|
318
322
|
['i32', 'Int', (code) => `Value.fromI32(${code})`],
|
|
319
323
|
['i64', 'Int8', (code) => `Value.fromI64(${code})`],
|
|
324
|
+
['i64', 'Timestamp', (code) => `Value.fromI64(${code})`],
|
|
320
325
|
['BigInt', 'BigInt', (code) => `Value.fromBigInt(${code})`],
|
|
321
326
|
['string', 'String', (code) => `Value.fromString(${code})`],
|
|
322
327
|
['string', 'ID', (code) => `Value.fromString(${code})`],
|
|
@@ -44,6 +44,7 @@ const BUILTIN_SCALAR_TYPES = [
|
|
|
44
44
|
'Bytes',
|
|
45
45
|
'ID',
|
|
46
46
|
'Int8',
|
|
47
|
+
'Timestamp'
|
|
47
48
|
];
|
|
48
49
|
// Type suggestions for common mistakes
|
|
49
50
|
const TYPE_SUGGESTIONS = [
|
|
@@ -64,6 +65,8 @@ const TYPE_SUGGESTIONS = [
|
|
|
64
65
|
['Float', 'BigDecimal'],
|
|
65
66
|
['int', 'Int'],
|
|
66
67
|
['int8', 'Int8'],
|
|
68
|
+
['timestamp', 'Timestamp'],
|
|
69
|
+
['ts', 'Timestamp'],
|
|
67
70
|
['uint', 'BigInt'],
|
|
68
71
|
['owner', 'String'],
|
|
69
72
|
['Owner', 'String'],
|
package/oclif.manifest.json
CHANGED