Inherit and modify access / visibility on fields from ObjectType in InputType #876
-
What's the recommended way to inherit fields from an ObjectType into an InputType but modify the access to a subset of those fields. // This would be the @ObjectType, but inheritance between types with differing decorators is not supported
class User {
@Field()
firstName = '';
@Field()
lastName = '';
@Field()
fullName = this.firstName + ' ' + this.lastName;
}
// This would be the @InputType, but with the fullName field losing the field decorator so it is not user editable
class UserInput extends User {
fullName = this.firstName + ' ' + this.lastName;
} I'm aware you can reuse code between types but they both have to use the same decorators which does not meet the constraint here From https://typegraphql.com/docs/inheritance.html#types-inheritance
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You should use the mixin-classes pattern. You can take a look at the example in the repo: |
Beta Was this translation helpful? Give feedback.
You should use the mixin-classes pattern.
This way you can declare the subparts of classes, like
firstName
andlastName
, and then reuse them to compose input types and output types.You can take a look at the example in the repo:
https://github.com/MichalLytek/type-graphql/tree/master/examples/mixin-classes