Description
The upcoming Prisma 2 integration (#217) will generate TS classes with TypeGraphQL decorators based on data model definition of Prisma schema.
But to allow for further customization, TypeGraphQL needs some operators to transform the generated classes into the user one. Mainly to hide some fields from database like user password, so we need a way to omit some fields of the base class or pick only some selected fields.
Proposed API have two approaches to apply the transformation:
- pick/omit fields from emitted GraphQL schema using decorator option
@ObjectType({ pick: ["firstname", "lastname"] })
export class User extends BaseUser {
// ...
}
@ObjectType({ omit: ["password", "salary"] })
export class User extends BaseUser {
// ...
}
This approach is for use cases where you still want to have access to the hidden fields in other field resolvers, like hidding array or rates but exposing average rating by the field resolver.
- pick/omit fields both from emitted GraphQL schema and TS type using class wrapper
@ObjectType()
export class User extends Pick(BaseUser, ["firstname", "lastname"]) {
// ...
}
@ObjectType()
export class User extends Omit(BaseUser, ["password", "salary"]) {
// ...
}
This approach is better when you want to create a derived type, like a subset of some input type, so you won't accidentally use the not existing field.
Initial version might not support inheritance or have a prototype methods leaks, because it's not needed by the Prisma integration. In the next release cycle I will try to make it work with broader range of use cases.
Later, more types transformation utils will be implemented, like Partial(Foo)
for making fields optional, Required(Foo)
for making fields non-nullable.
If possible, maybe I will try to add even a mapping util that will map the keys of one type to new values in a new type. For example if you want to generate a sorting input (with field types only ASC/DESC) based on an object type which fields are representing database columns 😉