@graphprotocol/graph-cli 0.46.2-rc-20230411194044-db9df1d → 0.47.0-alpha-20230413171255-6070c2c

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 CHANGED
@@ -1,6 +1,14 @@
1
1
  # @graphprotocol/graph-cli
2
2
 
3
- ## 0.46.2-rc-20230411194044-db9df1d
3
+ ## 0.47.0-alpha-20230413171255-6070c2c
4
+
5
+ ### Minor Changes
6
+
7
+ - [#1284](https://github.com/graphprotocol/graph-tooling/pull/1284)
8
+ [`74e7bac`](https://github.com/graphprotocol/graph-tooling/commit/74e7bacbcb48b4b663cfd0e7b137f57394347174)
9
+ Thanks [@saihaj](https://github.com/saihaj)! - Changing code generation so we reduce the non-null
10
+ assertions for primitive types. This way we can return null for primitive types and still have the
11
+ generated code compile.
4
12
 
5
13
  ### Patch Changes
6
14
 
@@ -18,6 +26,13 @@
18
26
  - Updated dependency [`@oclif/core@2.8.1` ↗︎](https://www.npmjs.com/package/@oclif/core/v/2.8.1)
19
27
  (from `2.8.0`, in `dependencies`)
20
28
 
29
+ - [#1288](https://github.com/graphprotocol/graph-tooling/pull/1288)
30
+ [`7090159`](https://github.com/graphprotocol/graph-tooling/commit/7090159be5b40ec78702ce9c870536f4fb7e048d)
31
+ Thanks [@renovate](https://github.com/apps/renovate)! - dependencies updates:
32
+
33
+ - Updated dependency [`@oclif/core@2.8.2` ↗︎](https://www.npmjs.com/package/@oclif/core/v/2.8.2)
34
+ (from `2.8.1`, in `dependencies`)
35
+
21
36
  - [#1259](https://github.com/graphprotocol/graph-tooling/pull/1259)
22
37
  [`5ccb466`](https://github.com/graphprotocol/graph-tooling/commit/5ccb466019d4d3de693f5b7a89898b1598c6c4be)
23
38
  Thanks [@saihaj](https://github.com/saihaj)! - refactor out immutable.js usage from schema
@@ -158,7 +158,14 @@ class SchemaCodeGenerator {
158
158
  const fieldValueType = this._valueTypeFromGraphQl(gqlType);
159
159
  const returnType = this._typeFromGraphQl(gqlType);
160
160
  const isNullable = returnType instanceof tsCodegen.NullableType;
161
- const getNonNullable = `return ${typesCodegen.valueToAsc('value!', fieldValueType)}`;
161
+ const primitiveDefault = returnType instanceof tsCodegen.NamedType ? returnType.getPrimitiveDefault() : null;
162
+ const getNonNullable = `if (!value || value.kind == ValueKind.NULL) {
163
+ ${primitiveDefault === null
164
+ ? "throw new Error('Cannot return null for a required field.')"
165
+ : `return ${primitiveDefault}`}
166
+ } else {
167
+ return ${typesCodegen.valueToAsc('value', fieldValueType)}
168
+ }`;
162
169
  const getNullable = `if (!value || value.kind == ValueKind.NULL) {
163
170
  return null
164
171
  } else {
@@ -82,6 +82,7 @@ describe('Schema code generator', () => {
82
82
  # two primitive types (i32)
83
83
  age: Int
84
84
  count: Int!
85
+ isActive: Boolean
85
86
 
86
87
  # derivedFrom
87
88
  wallets: [Wallet!] @derivedFrom(field: "account")
@@ -142,9 +143,12 @@ describe('Schema code generator', () => {
142
143
  name: 'get id',
143
144
  params: [],
144
145
  returnType: new typescript_1.NamedType('string'),
145
- body: `
146
- let value = this.get('id')
147
- return value!.toString()
146
+ body: `let value = this.get('id')
147
+ if (!value || value.kind == ValueKind.NULL) {
148
+ throw new Error("Cannot return null for a required field.")
149
+ } else {
150
+ return value.toString()
151
+ }
148
152
  `,
149
153
  },
150
154
  {
@@ -184,9 +188,12 @@ describe('Schema code generator', () => {
184
188
  name: 'get name',
185
189
  params: [],
186
190
  returnType: new typescript_1.NamedType('string'),
187
- body: `
188
- let value = this.get('name')
189
- return value!.toString()
191
+ body: `let value = this.get('name')
192
+ if (!value || value.kind == ValueKind.NULL) {
193
+ throw new Error("Cannot return null for a required field.")
194
+ } else {
195
+ return value.toString()
196
+ }
190
197
  `,
191
198
  },
192
199
  {
@@ -201,9 +208,12 @@ describe('Schema code generator', () => {
201
208
  name: 'get age',
202
209
  params: [],
203
210
  returnType: new typescript_1.NamedType('i32'),
204
- body: `
205
- let value = this.get('age')
206
- return value!.toI32()
211
+ body: `let value = this.get('age')
212
+ if (!value || value.kind == ValueKind.NULL) {
213
+ return 0
214
+ } else {
215
+ return value.toI32()
216
+ }
207
217
  `,
208
218
  },
209
219
  {
@@ -218,9 +228,12 @@ describe('Schema code generator', () => {
218
228
  name: 'get count',
219
229
  params: [],
220
230
  returnType: new typescript_1.NamedType('i32'),
221
- body: `
222
- let value = this.get('count')
223
- return value!.toI32()
231
+ body: `let value = this.get('count')
232
+ if (!value || value.kind == ValueKind.NULL) {
233
+ return 0
234
+ } else {
235
+ return value.toI32()
236
+ }
224
237
  `,
225
238
  },
226
239
  {
@@ -229,6 +242,26 @@ describe('Schema code generator', () => {
229
242
  returnType: undefined,
230
243
  body: `
231
244
  this.set('count', Value.fromI32(value))
245
+ `,
246
+ },
247
+ {
248
+ name: 'get isActive',
249
+ params: [],
250
+ returnType: new typescript_1.NamedType('boolean'),
251
+ body: `let value = this.get('isActive')
252
+ if (!value || value.kind == ValueKind.NULL) {
253
+ return false
254
+ } else {
255
+ return value.toBoolean()
256
+ }
257
+ `,
258
+ },
259
+ {
260
+ name: 'set isActive',
261
+ params: [new typescript_1.Param('value', new typescript_1.NamedType('boolean'))],
262
+ returnType: undefined,
263
+ body: `
264
+ this.set('isActive', Value.fromBoolean(value))
232
265
  `,
233
266
  },
234
267
  {
@@ -289,9 +322,12 @@ describe('Schema code generator', () => {
289
322
  name: 'get id',
290
323
  params: [],
291
324
  returnType: new typescript_1.NamedType('string'),
292
- body: `
293
- let value = this.get('id')
294
- return value!.toString()
325
+ body: `let value = this.get('id')
326
+ if (!value || value.kind == ValueKind.NULL) {
327
+ throw new Error("Cannot return null for a required field.")
328
+ } else {
329
+ return value.toString()
330
+ }
295
331
  `,
296
332
  },
297
333
  {
@@ -306,9 +342,12 @@ describe('Schema code generator', () => {
306
342
  name: 'get amount',
307
343
  params: [],
308
344
  returnType: new typescript_1.NamedType('BigInt'),
309
- body: `
310
- let value = this.get('amount')
311
- return value!.toBigInt()
345
+ body: `let value = this.get('amount')
346
+ if (!value || value.kind == ValueKind.NULL) {
347
+ throw new Error("Cannot return null for a required field.")
348
+ } else {
349
+ return value.toBigInt()
350
+ }
312
351
  `,
313
352
  },
314
353
  {
@@ -323,9 +362,12 @@ describe('Schema code generator', () => {
323
362
  name: 'get account',
324
363
  params: [],
325
364
  returnType: new typescript_1.NamedType('string'),
326
- body: `
327
- let value = this.get('account')
328
- return value!.toString()
365
+ body: `let value = this.get('account')
366
+ if (!value || value.kind == ValueKind.NULL) {
367
+ throw new Error("Cannot return null for a required field.")
368
+ } else {
369
+ return value.toString()
370
+ }
329
371
  `,
330
372
  },
331
373
  {
@@ -396,10 +438,12 @@ describe('Schema code generator', () => {
396
438
  name: 'get id',
397
439
  params: [],
398
440
  returnType: new typescript_1.NamedType('Bytes'),
399
- body: '\n' +
400
- " let value = this.get('id')\n" +
401
- ' return value!.toBytes()\n' +
402
- ' ',
441
+ body: `let value = this.get("id")
442
+ if (!value || value.kind == ValueKind.NULL) {
443
+ throw new Error("Cannot return null for a required field.")
444
+ } else {
445
+ return value.toBytes()
446
+ }`,
403
447
  },
404
448
  {
405
449
  name: 'set id',
@@ -411,10 +455,12 @@ describe('Schema code generator', () => {
411
455
  name: 'get employee',
412
456
  params: [],
413
457
  returnType: new typescript_1.NamedType('Bytes'),
414
- body: '\n' +
415
- " let value = this.get('employee')\n" +
416
- ' return value!.toBytes()\n' +
417
- ' ',
458
+ body: `let value = this.get('employee')
459
+ if (!value || value.kind == ValueKind.NULL) {
460
+ throw new Error("Cannot return null for a required field.")
461
+ } else {
462
+ return value.toBytes()
463
+ }`,
418
464
  },
419
465
  {
420
466
  name: 'set employee',
@@ -426,10 +472,12 @@ describe('Schema code generator', () => {
426
472
  name: 'get worker',
427
473
  params: [],
428
474
  returnType: new typescript_1.NamedType('Bytes'),
429
- body: '\n' +
430
- " let value = this.get('worker')\n" +
431
- ' return value!.toBytes()\n' +
432
- ' ',
475
+ body: `let value = this.get('worker')
476
+ if (!value || value.kind == ValueKind.NULL) {
477
+ throw new Error("Cannot return null for a required field.")
478
+ } else {
479
+ return value.toBytes()
480
+ }`,
433
481
  },
434
482
  {
435
483
  name: 'set worker',
@@ -100,6 +100,36 @@ class NamedType {
100
100
  this.name = this.name.charAt(0).toUpperCase() + this.name.slice(1);
101
101
  return this;
102
102
  }
103
+ /**
104
+ * Returns the default value for the type, or null if the type is not a primitive
105
+ *
106
+ * Learn more: https://www.assemblyscript.org/types.html
107
+ */
108
+ getPrimitiveDefault() {
109
+ const isPrimitive = this.isPrimitive();
110
+ if (isPrimitive) {
111
+ switch (this.name) {
112
+ case 'boolean':
113
+ return false;
114
+ case 'u8':
115
+ case 'i8':
116
+ case 'u16':
117
+ case 'i16':
118
+ case 'u32':
119
+ case 'i32':
120
+ case 'u64':
121
+ case 'i64':
122
+ case 'f32':
123
+ case 'f64':
124
+ case 'usize':
125
+ case 'isize':
126
+ return 0;
127
+ default:
128
+ throw new Error(`Unknown primitive type: ${this.name}`);
129
+ }
130
+ }
131
+ return null;
132
+ }
103
133
  isPrimitive() {
104
134
  const primitives = [
105
135
  'boolean',
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.46.2-rc-20230411194044-db9df1d",
2
+ "version": "0.47.0-alpha-20230413171255-6070c2c",
3
3
  "commands": {
4
4
  "add": {
5
5
  "id": "add",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphprotocol/graph-cli",
3
- "version": "0.46.2-rc-20230411194044-db9df1d",
3
+ "version": "0.47.0-alpha-20230413171255-6070c2c",
4
4
  "description": "CLI for building for and deploying to The Graph",
5
5
  "license": "(Apache-2.0 OR MIT)",
6
6
  "engines": {
@@ -18,7 +18,7 @@
18
18
  ],
19
19
  "dependencies": {
20
20
  "@float-capital/float-subgraph-uncrashable": "^0.0.0-alpha.4",
21
- "@oclif/core": "2.8.1",
21
+ "@oclif/core": "2.8.2",
22
22
  "@whatwg-node/fetch": "^0.8.4",
23
23
  "assemblyscript": "0.19.23",
24
24
  "binary-install-raw": "0.0.13",