Skip to content

Commit b4311d8

Browse files
authored
feat: for nullable primitives return a default value (#1284)
* feat: return default values for non-null fields, since primitives cannot be null in assembly script * update examples * ci
1 parent e5c1fbc commit b4311d8

File tree

7 files changed

+3544
-46
lines changed

7 files changed

+3544
-46
lines changed

.changeset/fifty-eyes-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': minor
3+
---
4+
5+
Changing code generation so we reduce the non-null assertions for primitive types. This way we can return null for primitive types and still have the generated code compile.

examples/basic-event-handlers/generated/schema.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ export class NewGravatar extends Entity {
3535

3636
get id(): string {
3737
let value = this.get("id");
38-
return value!.toString();
38+
if (!value || value.kind == ValueKind.NULL) {
39+
throw new Error("Cannot return null for a required field.");
40+
} else {
41+
return value.toString();
42+
}
3943
}
4044

4145
set id(value: string) {
@@ -44,7 +48,11 @@ export class NewGravatar extends Entity {
4448

4549
get owner(): Bytes {
4650
let value = this.get("owner");
47-
return value!.toBytes();
51+
if (!value || value.kind == ValueKind.NULL) {
52+
throw new Error("Cannot return null for a required field.");
53+
} else {
54+
return value.toBytes();
55+
}
4856
}
4957

5058
set owner(value: Bytes) {
@@ -53,7 +61,11 @@ export class NewGravatar extends Entity {
5361

5462
get displayName(): string {
5563
let value = this.get("displayName");
56-
return value!.toString();
64+
if (!value || value.kind == ValueKind.NULL) {
65+
throw new Error("Cannot return null for a required field.");
66+
} else {
67+
return value.toString();
68+
}
5769
}
5870

5971
set displayName(value: string) {
@@ -62,7 +74,11 @@ export class NewGravatar extends Entity {
6274

6375
get imageUrl(): string {
6476
let value = this.get("imageUrl");
65-
return value!.toString();
77+
if (!value || value.kind == ValueKind.NULL) {
78+
throw new Error("Cannot return null for a required field.");
79+
} else {
80+
return value.toString();
81+
}
6682
}
6783

6884
set imageUrl(value: string) {
@@ -94,7 +110,11 @@ export class UpdatedGravatar extends Entity {
94110

95111
get id(): string {
96112
let value = this.get("id");
97-
return value!.toString();
113+
if (!value || value.kind == ValueKind.NULL) {
114+
throw new Error("Cannot return null for a required field.");
115+
} else {
116+
return value.toString();
117+
}
98118
}
99119

100120
set id(value: string) {
@@ -103,7 +123,11 @@ export class UpdatedGravatar extends Entity {
103123

104124
get owner(): Bytes {
105125
let value = this.get("owner");
106-
return value!.toBytes();
126+
if (!value || value.kind == ValueKind.NULL) {
127+
throw new Error("Cannot return null for a required field.");
128+
} else {
129+
return value.toBytes();
130+
}
107131
}
108132

109133
set owner(value: Bytes) {
@@ -112,7 +136,11 @@ export class UpdatedGravatar extends Entity {
112136

113137
get displayName(): string {
114138
let value = this.get("displayName");
115-
return value!.toString();
139+
if (!value || value.kind == ValueKind.NULL) {
140+
throw new Error("Cannot return null for a required field.");
141+
} else {
142+
return value.toString();
143+
}
116144
}
117145

118146
set displayName(value: string) {
@@ -121,7 +149,11 @@ export class UpdatedGravatar extends Entity {
121149

122150
get imageUrl(): string {
123151
let value = this.get("imageUrl");
124-
return value!.toString();
152+
if (!value || value.kind == ValueKind.NULL) {
153+
throw new Error("Cannot return null for a required field.");
154+
} else {
155+
return value.toString();
156+
}
125157
}
126158

127159
set imageUrl(value: string) {

0 commit comments

Comments
 (0)