Skip to content

Commit 629176c

Browse files
committed
codegen: Revert default set call with non-nullable values
1 parent 48a0599 commit 629176c

File tree

5 files changed

+22
-116
lines changed

5 files changed

+22
-116
lines changed

src/codegen/schema.js

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,41 +120,6 @@ module.exports = class SchemaCodeGenerator {
120120
return klass
121121
}
122122

123-
// Fields that are non-nullable get an empty/zero value set in the class constructor.
124-
_generateDefaultFieldValues(fields) {
125-
const indexOfIdField = fields.findIndex(field => field.getIn(['name', 'value']) === 'id')
126-
const fieldsWithoutId = fields.remove(indexOfIdField)
127-
128-
const fieldsSetCalls = fieldsWithoutId
129-
.map(field => {
130-
const name = field.getIn(['name', 'value'])
131-
const type = this._typeFromGraphQl(field.get('type'), true, true)
132-
133-
const isNullable = type instanceof tsCodegen.NullableType
134-
135-
const directives = field.get('directives')
136-
const isDerivedFrom = directives.some(directive => directive.getIn(['name', 'value']) === 'derivedFrom')
137-
138-
return { name, type, isNullable, isDerivedFrom }
139-
})
140-
// We only call the setter with the default value in the constructor for fields that are:
141-
// - Not nullable, so that AS doesn't break when subgraph developers try to access them before a `set`
142-
// - It doesn't matter if it's primitive or not
143-
// - Not tagged as `derivedFrom`, because they only exist in query time
144-
.filter(({
145-
isNullable,
146-
isDerivedFrom,
147-
}) => !isNullable && !isDerivedFrom)
148-
.map(({ name, type, isNullable }) => {
149-
const fieldTypeString = isNullable ? type.inner.toString() : type.toString()
150-
151-
return `
152-
this.set('${name}', ${typesCodegen.initializedValueFromAsc(fieldTypeString)})`
153-
})
154-
155-
return fieldsSetCalls.join('')
156-
}
157-
158123
_generateConstructor(entityName, fields) {
159124
const idField = IdField.fromFields(fields)
160125
return tsCodegen.method(
@@ -164,7 +129,6 @@ module.exports = class SchemaCodeGenerator {
164129
`
165130
super()
166131
this.set('id', ${idField.tsValueFrom()})
167-
${this._generateDefaultFieldValues(fields)}
168132
`,
169133
)
170134
}
@@ -313,7 +277,7 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
313277
}
314278
}
315279

316-
_typeFromGraphQl(gqlType, nullable = true, nullablePrimitive = false) {
280+
_typeFromGraphQl(gqlType, nullable = true) {
317281
if (gqlType.get('kind') === 'NonNullType') {
318282
return this._typeFromGraphQl(gqlType.get('type'), false)
319283
} else if (gqlType.get('kind') === 'ListType') {
@@ -324,13 +288,8 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
324288
let type = tsCodegen.namedType(
325289
typesCodegen.ascTypeForValue(this._resolveFieldType(gqlType)),
326290
)
327-
328-
// Will not wrap primitives into NullableType by default.
329-
if (!nullablePrimitive && type.isPrimitive()) {
330-
return type
331-
}
332-
333-
return nullable ? tsCodegen.nullableType(type) : type
291+
// In AssemblyScript, primitives cannot be nullable.
292+
return nullable && !type.isPrimitive() ? tsCodegen.nullableType(type) : type
334293
}
335294
}
336295
}

src/codegen/schema.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ describe('Schema code generator', () => {
114114
body: `
115115
super()
116116
this.set('id', Value.fromString(id))
117-
118-
this.set('name', Value.fromString(''))
119-
this.set('count', Value.fromI32(0))
120117
`,
121118
},
122119
{
@@ -277,9 +274,6 @@ describe('Schema code generator', () => {
277274
body: `
278275
super()
279276
this.set('id', Value.fromString(id))
280-
281-
this.set('amount', Value.fromBigInt(BigInt.zero()))
282-
this.set('account', Value.fromString(''))
283277
`,
284278
},
285279
{

src/codegen/types/conversions.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,28 +188,28 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
188188
const ASSEMBLYSCRIPT_TO_VALUE = [
189189
// Arrays
190190

191-
['Array<Address>', '[Bytes]', code => `Value.fromBytesArray(${code})`, 'new Array(0)'],
192-
['Array<Bytes>', '[Bytes]', code => `Value.fromBytesArray(${code})`, 'new Array(0)'],
193-
['Array<boolean>', '[Boolean]', code => `Value.fromBooleanArray(${code})`, 'new Array(0)'],
194-
['Array<i32>', '[Int]', code => `Value.fromI32Array(${code})`, 'new Array(0)'],
195-
['Array<BigInt>', '[BigInt]', code => `Value.fromBigIntArray(${code})`, 'new Array(0)'],
196-
['Array<string>', '[String]', code => `Value.fromStringArray(${code})`, 'new Array(0)'],
197-
['Array<string>', '[ID]', code => `Value.fromStringArray(${code})`, 'new Array(0)'],
198-
['Array<BigDecimal>', '[BigDecimal]', code => `Value.fromBigDecimalArray(${code})`, 'new Array(0)'],
199-
['Array<string>', /\[.*\]/, code => `Value.fromStringArray(${code})`, 'new Array(0)'],
200-
['Array<string | null>', null, code => `Value.fromStringArray(${code})`, 'new Array(0)'],
191+
['Array<Address>', '[Bytes]', code => `Value.fromBytesArray(${code})`],
192+
['Array<Bytes>', '[Bytes]', code => `Value.fromBytesArray(${code})`],
193+
['Array<boolean>', '[Boolean]', code => `Value.fromBooleanArray(${code})`],
194+
['Array<i32>', '[Int]', code => `Value.fromI32Array(${code})`],
195+
['Array<BigInt>', '[BigInt]', code => `Value.fromBigIntArray(${code})`],
196+
['Array<string>', '[String]', code => `Value.fromStringArray(${code})`],
197+
['Array<string>', '[ID]', code => `Value.fromStringArray(${code})`],
198+
['Array<BigDecimal>', '[BigDecimal]', code => `Value.fromBigDecimalArray(${code})`],
199+
['Array<string>', /\[.*\]/, code => `Value.fromStringArray(${code})`],
200+
['Array<string | null>', null, code => `Value.fromStringArray(${code})`],
201201

202202
// Scalar values
203203

204-
['Address', 'Bytes', code => `Value.fromBytes(${code})`, 'Address.zero()'],
205-
['Bytes', 'Bytes', code => `Value.fromBytes(${code})`, 'Bytes.empty()'],
206-
['boolean', 'Boolean', code => `Value.fromBoolean(${code})`, 'false'],
207-
['i32', 'Int', code => `Value.fromI32(${code})`, '0'],
208-
['BigInt', 'BigInt', code => `Value.fromBigInt(${code})`, 'BigInt.zero()'],
209-
['string', 'String', code => `Value.fromString(${code})`, "''"],
210-
['string', 'ID', code => `Value.fromString(${code})`, "''"],
211-
['BigDecimal', 'BigDecimal', code => `Value.fromBigDecimal(${code})`, 'BigDecimal.zero()'],
212-
['string', /.*/, code => `Value.fromString(${code})`, "''"],
204+
['Address', 'Bytes', code => `Value.fromBytes(${code})`],
205+
['Bytes', 'Bytes', code => `Value.fromBytes(${code})`],
206+
['boolean', 'Boolean', code => `Value.fromBoolean(${code})`],
207+
['i32', 'Int', code => `Value.fromI32(${code})`],
208+
['BigInt', 'BigInt', code => `Value.fromBigInt(${code})`],
209+
['string', 'String', code => `Value.fromString(${code})`],
210+
['string', 'ID', code => `Value.fromString(${code})`],
211+
['BigDecimal', 'BigDecimal', code => `Value.fromBigDecimal(${code})`],
212+
['string', /.*/, code => `Value.fromString(${code})`],
213213
]
214214

215215
/**

src/codegen/types/index.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,6 @@ const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
6666
return objectifyConversion(fromTypeSystem, toTypeSystem, conversion)
6767
}
6868

69-
const findInitializationForType = (fromTypeSystem, toTypeSystem, ascType) => {
70-
const conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem)
71-
72-
const conversion = conversions.find(conversion =>
73-
typeof conversion.get(0) === 'string'
74-
? conversion.get(0) === ascType
75-
: ascType.match(conversion.get(0)),
76-
)
77-
78-
if (conversion === undefined) {
79-
throw new Error(
80-
`Conversion from '${fromTypeSystem}' to '${toTypeSystem}' for ` +
81-
`target type '${ascType}' is not supported`,
82-
)
83-
}
84-
85-
const conversionObj = objectifyConversion(fromTypeSystem, toTypeSystem, conversion)
86-
87-
return conversionObj.get('convert')(conversion.get(3))
88-
}
89-
9069
// High-level type system API
9170

9271
const ascTypeForProtocol = (protocol, protocolType) =>
@@ -120,9 +99,6 @@ const valueToAsc = (code, valueType) =>
12099
const valueFromAsc = (code, valueType) =>
121100
findConversionToType('AssemblyScript', 'Value', valueType).get('convert')(code)
122101

123-
const initializedValueFromAsc = ascType =>
124-
findInitializationForType('AssemblyScript', 'Value', ascType)
125-
126102
module.exports = {
127103
// protocol <-> AssemblyScript
128104
ascTypeForProtocol,
@@ -138,5 +114,4 @@ module.exports = {
138114
valueTypeForAsc,
139115
valueToAsc,
140116
valueFromAsc,
141-
initializedValueFromAsc,
142117
}

src/codegen/types/index.test.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -449,26 +449,4 @@ describe('AssemblyScript -> Value', () => {
449449
expect(codegen.valueFromAsc('x', '[String]')).toBe('Value.fromStringArray(x)')
450450
expect(codegen.valueTypeForAsc('Array<string>')).toBe('[String]')
451451
})
452-
453-
describe('Initialization for zero/empty values', () => {
454-
test('Array<string>', () => {
455-
expect(codegen.initializedValueFromAsc('Array<string>')).toBe('Value.fromStringArray(new Array(0))')
456-
})
457-
458-
test('Array<string | null>', () => {
459-
expect(codegen.initializedValueFromAsc('Array<string | null>')).toBe('Value.fromStringArray(new Array(0))')
460-
})
461-
462-
test('i32', () => {
463-
expect(codegen.initializedValueFromAsc('i32')).toBe('Value.fromI32(0)')
464-
})
465-
466-
test('string', () => {
467-
expect(codegen.initializedValueFromAsc('string')).toBe("Value.fromString('')")
468-
})
469-
470-
test('BigInt', () => {
471-
expect(codegen.initializedValueFromAsc('BigInt')).toBe('Value.fromBigInt(BigInt.zero())')
472-
})
473-
})
474452
})

0 commit comments

Comments
 (0)