Skip to content

Commit 8318074

Browse files
committed
codegen: Remove default values for primitive schema fields
In the AssemblyScript update a new feature was added to the CLI. Schemas with types such as: ```graphql type Total @entity { id: ID! amount: BigInt } ``` Started to have their setters called on the constructor with the default value for the type, eg: ```typescript export class Total extends Entity { constructor(id: string) { super(); this.set("id", Value.fromString(id)); this.set("amount", Value.fromBigInt(BigInt.zero())); } // ... } ``` The problem is that these new set calls were being called with primitives as well, that is a bug. This commit fixes the issue by checking if the field is primitive to avoid generating the wrong call.
1 parent 77546f3 commit 8318074

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/codegen/schema.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,24 @@ module.exports = class SchemaCodeGenerator {
8080
.map(field => {
8181
const name = field.getIn(['name', 'value'])
8282
const type = this._typeFromGraphQl(field.get('type'))
83+
8384
const isNullable = type instanceof tsCodegen.NullableType
85+
const isPrimitive = type.isPrimitive && type.isPrimitive()
8486

8587
const directives = field.get('directives')
8688
const isDerivedFrom = directives.some(directive => directive.getIn(['name', 'value']) === 'derivedFrom')
8789

88-
return { name, type, isNullable, isDerivedFrom }
90+
return { name, type, isNullable, isPrimitive, isDerivedFrom }
8991
})
9092
// We only call the setter with the default value in the constructor for fields that are:
93+
// - Not primitive (such as Int/i32)
9194
// - Not nullable, so that AS doesn't break when subgraph developers try to access them before a `set`
9295
// - Not tagged as `derivedFrom`, because they only exist in query time
93-
.filter(({ isNullable, isDerivedFrom }) => !isNullable && !isDerivedFrom)
96+
.filter(({
97+
isNullable,
98+
isPrimitive,
99+
isDerivedFrom,
100+
}) => !(isNullable || isPrimitive || isDerivedFrom))
94101
.map(({ name, type, isNullable }) => {
95102
const fieldTypeString = isNullable ? type.inner.toString() : type.toString()
96103

0 commit comments

Comments
 (0)