Skip to content

Commit 7f9b71c

Browse files
authored
codegen: Only call setter in constructors if value is not derivedFrom (#739)
This is because derivedFrom fields only exist in query time. Before this commit, developers would get an error similar to this: field `nftsOwned` is derived and can not be set
1 parent 92ccb0b commit 7f9b71c

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/codegen/schema.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,15 @@ module.exports = class SchemaCodeGenerator {
8383
const type = this._typeFromGraphQl(field.get('type'))
8484
const isNullable = type instanceof tsCodegen.NullableType
8585

86-
return { name, type, isNullable }
86+
const directives = field.get('directives')
87+
const isDerivedFrom = directives.some(directive => directive.getIn(['name', 'value']) === 'derivedFrom')
88+
89+
return { name, type, isNullable, isDerivedFrom }
8790
})
88-
.filter(({ isNullable }) => !isNullable)
91+
// We only call the setter with the default value in the constructor for fields that are:
92+
// - Not nullable, so that AS doesn't break when subgraph developers try to access them before a `set`
93+
// - Not tagged as `derivedFrom`, because they only exist in query time
94+
.filter(({ isNullable, isDerivedFrom }) => !isNullable && !isDerivedFrom)
8995
.map(({ name, type, isNullable }) => {
9096
const fieldTypeString = isNullable ? type.inner.toString() : type.toString()
9197

0 commit comments

Comments
 (0)