MichalLytek / MichalLytek/type-graphql

Types transformation utils

Open
#453 18 comments 23 reactions 1 assignee View on GitHub

@MichalLytek is already working on this.

Since Oct 30, 2019.

Discussion :speech_balloon: Enhancement :new:
Dominant language
TypeScript
Stars
8.1k
Forks
672
PR merge metrics
No merged PRs in 30d

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
```ts
@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
```ts
@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 😉

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.