@@ -79,25 +79,23 @@ module.exports = class SchemaCodeGenerator {
79
79
const fieldsSetCalls = fieldsWithoutId
80
80
. map ( field => {
81
81
const name = field . getIn ( [ 'name' , 'value' ] )
82
- const type = this . _typeFromGraphQl ( field . get ( 'type' ) )
82
+ const type = this . _typeFromGraphQl ( field . get ( 'type' ) , true , true )
83
83
84
84
const isNullable = type instanceof tsCodegen . NullableType
85
- const isPrimitive = type . isPrimitive && type . isPrimitive ( )
86
85
87
86
const directives = field . get ( 'directives' )
88
87
const isDerivedFrom = directives . some ( directive => directive . getIn ( [ 'name' , 'value' ] ) === 'derivedFrom' )
89
88
90
- return { name, type, isNullable, isPrimitive , isDerivedFrom }
89
+ return { name, type, isNullable, isDerivedFrom }
91
90
} )
92
91
// We only call the setter with the default value in the constructor for fields that are:
93
- // - Not primitive (such as Int/i32)
94
92
// - 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
95
94
// - Not tagged as `derivedFrom`, because they only exist in query time
96
95
. filter ( ( {
97
96
isNullable,
98
- isPrimitive,
99
97
isDerivedFrom,
100
- } ) => ! ( isNullable || isPrimitive || isDerivedFrom ) )
98
+ } ) => ! isNullable && ! isDerivedFrom )
101
99
. map ( ( { name, type, isNullable } ) => {
102
100
const fieldTypeString = isNullable ? type . inner . toString ( ) : type . toString ( )
103
101
@@ -236,7 +234,7 @@ Suggestion: add an '!' to the member type of the List, change from '${fieldValue
236
234
: gqlType . getIn ( [ 'name' , 'value' ] )
237
235
}
238
236
239
- _typeFromGraphQl ( gqlType , nullable = true ) {
237
+ _typeFromGraphQl ( gqlType , nullable = true , nullablePrimitive = false ) {
240
238
if ( gqlType . get ( 'kind' ) === 'NonNullType' ) {
241
239
return this . _typeFromGraphQl ( gqlType . get ( 'type' ) , false )
242
240
} else if ( gqlType . get ( 'kind' ) === 'ListType' ) {
@@ -247,8 +245,13 @@ Suggestion: add an '!' to the member type of the List, change from '${fieldValue
247
245
let type = tsCodegen . namedType (
248
246
typesCodegen . ascTypeForValue ( gqlType . getIn ( [ 'name' , 'value' ] ) ) ,
249
247
)
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
252
255
}
253
256
}
254
257
}
0 commit comments