Skip to content

Commit ec225da

Browse files
committed
codegen: Add default setter call for nullable primitives
Bug pointed here: #791 (comment)
1 parent c1ec720 commit ec225da

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/codegen/schema.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,23 @@ module.exports = class SchemaCodeGenerator {
7979
const fieldsSetCalls = fieldsWithoutId
8080
.map(field => {
8181
const name = field.getIn(['name', 'value'])
82-
const type = this._typeFromGraphQl(field.get('type'))
82+
const type = this._typeFromGraphQl(field.get('type'), true, true)
8383

8484
const isNullable = type instanceof tsCodegen.NullableType
85-
const isPrimitive = type.isPrimitive && type.isPrimitive()
8685

8786
const directives = field.get('directives')
8887
const isDerivedFrom = directives.some(directive => directive.getIn(['name', 'value']) === 'derivedFrom')
8988

90-
return { name, type, isNullable, isPrimitive, isDerivedFrom }
89+
return { name, type, isNullable, isDerivedFrom }
9190
})
9291
// We only call the setter with the default value in the constructor for fields that are:
93-
// - Not primitive (such as Int/i32)
9492
// - Not nullable, so that AS doesn't break when subgraph developers try to access them before a `set`
93+
// - It doesn't matter if it's primitive or not
9594
// - Not tagged as `derivedFrom`, because they only exist in query time
9695
.filter(({
9796
isNullable,
98-
isPrimitive,
9997
isDerivedFrom,
100-
}) => !(isNullable || isPrimitive || isDerivedFrom))
98+
}) => !isNullable && !isDerivedFrom)
10199
.map(({ name, type, isNullable }) => {
102100
const fieldTypeString = isNullable ? type.inner.toString() : type.toString()
103101

@@ -236,7 +234,7 @@ Suggestion: add an '!' to the member type of the List, change from '${fieldValue
236234
: gqlType.getIn(['name', 'value'])
237235
}
238236

239-
_typeFromGraphQl(gqlType, nullable = true) {
237+
_typeFromGraphQl(gqlType, nullable = true, nullablePrimitive = false) {
240238
if (gqlType.get('kind') === 'NonNullType') {
241239
return this._typeFromGraphQl(gqlType.get('type'), false)
242240
} else if (gqlType.get('kind') === 'ListType') {
@@ -247,8 +245,13 @@ Suggestion: add an '!' to the member type of the List, change from '${fieldValue
247245
let type = tsCodegen.namedType(
248246
typesCodegen.ascTypeForValue(gqlType.getIn(['name', 'value'])),
249247
)
250-
// In AssemblyScript, primitives cannot be nullable.
251-
return nullable && !type.isPrimitive() ? tsCodegen.nullableType(type) : type
248+
249+
// Will not wrap primitives into NullableType by default.
250+
if (!nullablePrimitive && type.isPrimitive()) {
251+
return type
252+
}
253+
254+
return nullable ? tsCodegen.nullableType(type) : type
252255
}
253256
}
254257
}

src/codegen/schema.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ describe('Schema code generator', () => {
116116
this.set('id', Value.fromString(id))
117117
118118
this.set('name', Value.fromString(''))
119+
this.set('count', Value.fromI32(0))
119120
`,
120121
},
121122
{

0 commit comments

Comments
 (0)