typegraphql-prisma icon indicating copy to clipboard operation
typegraphql-prisma copied to clipboard

Reduntant type info

Open MichalLytek opened this issue 2 years ago • 5 comments

Currently, in some cases, the generator emits reduntant type info, like for fields of input types or models. Also, it not generates that type info for @Args in resolvers.

The first iteration would add the emitRedundantTypesInfo generator option, that would generate all the missing redundant types info, so that 3rd party compilers without types metadata support would be able to work correctly with the generated code.

Second iteration would invole detecting when the type info could be ommited, so that we will reduce the ammout of emitted code, that could speed up and save some space when using bundlers.

The redundancy in inputs, args and models would be disabled by default and enable full info with this option enabled.

MichalLytek avatar Jan 18 '23 12:01 MichalLytek

First iteration done in 36113bee. You can now use emitRedundantTypesInfo generator option: https://prisma.typegraphql.com/docs/advanced/emit-redundant-types-info

MichalLytek avatar Jan 18 '23 13:01 MichalLytek

Really cool!

How would I go about integrating this?

I enabled the emitRedundantTypesInfo flag and did a quick test build with esbuild but I still run into these reflection errors:

/Users/xxx/Projects/boilerplate/apps/server/dist/server.js:69362
        throw new errors_1.NoExplicitTypeError(prototype.constructor.name, propertyKey, parameterIndex, argName);
        ^

NoExplicitTypeError: Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for 'auth' of 'AdminAuthResponse' class.
    at Object.findType (/Users/xxx/Projects/boilerplate/apps/server/dist/server.js:69362:15)
    at /Users/xxx/Projects/boilerplate/apps/server/dist/server.js:69767:53
    at __decorateClass (/Users/xxx/Projects/boilerplate/apps/server/dist/server.js:34:25)
    at Object.<anonymous> (/Users/xxx/Projects/boilerplate/apps/server/dist/server.js:246548:1)
    at Module._compile (node:internal/modules/cjs/loader:1155:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
    at Module.load (node:internal/modules/cjs/loader:1033:32)
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47

The mentioned AdminAuthResponse class:

@ObjectType()
export class Authentication {
  @Field(() => String, { nullable: true })
  token: string | null;

  @Field(() => Date, { nullable: true })
  expiresAt: Date | null;
}

@ObjectType()
export class AdminAuthResponse {
  @Field()
  auth: Authentication;

  @Field(() => Admin, { nullable: true })
  admin: Admin | null;
}

Admin here is a type generated from typegraphql-prisma but seems like auth: Authentication is the problem

nephix avatar Jan 29 '23 16:01 nephix

@nephix

@Field(() => Authentication)
auth: Authentication;

MichalLytek avatar Jan 29 '23 19:01 MichalLytek

Thanks @MichalLytek !

Second iteration would invole detecting when the type info could be ommited, so that we will reduce the ammout of emitted code, that could speed up and save some space when using bundlers.

Would some of that duplication go away with that or is that only about the emitted code?

Or is TypeScript v5 going to help with this? (https://github.com/microsoft/TypeScript/pull/50820)

nephix avatar Jan 29 '23 19:01 nephix

I think TypeScript v5 won't solve esbuild lack of types metadata

MichalLytek avatar Jan 31 '23 13:01 MichalLytek