MichalLytek / typegraphql-prisma

Prisma generator to emit TypeGraphQL types and CRUD resolvers from your Prisma schema
https://prisma.typegraphql.com
MIT License
891 stars 113 forks source link

fix: fields with undefined values present in args of generated types #435

Closed shawnjones253 closed 9 months ago

shawnjones253 commented 10 months ago

fix for: https://github.com/MichalLytek/typegraphql-prisma/issues/434

This restores the previous behavior of input keys only being set when specified by the api request via variables.

Unfortunately, this only guarantees a fix when emitTranspiledCode = true is used (the default). However, if output = 'someDirOutsideOfNodeModules' is used WITHOUT emitTranspiledCode = true, then the consuming app must do one of:


UseDefineForClassFields Default: true if [target](https://www.typescriptlang.org/tsconfig#target) is ES2022 or higher, including ESNext; false otherwise.

useDefineForClassFields true false
ES2022 TS Playground TS Playground
<= ES2021 TS Playground TS Playground

some interesting reading that pointed me in this direction: https://github.com/swc-project/swc/pull/7055 https://github.com/microsoft/TypeScript/issues/45995

shawnjones253 commented 10 months ago

if the intent is to not have undefined values at runtime for unspecified request keys, should useDefineForClassFields: false be added to the required tsconfig.json options here? (it is only needed when emitTranspiledCode = false)

shawnjones253 commented 10 months ago

@MichalLytek -- are you open to this PR? our team reverted to 0.27.0 due to the above but we're really missing the speed boost that came with 0.27.1 :)

MichalLytek commented 10 months ago

I found this to be a workaround. I need to play with es2022 in typegraphql core itself I've recently merged there a fix to get rid of undefined values from transformed inputs.

shawnjones253 commented 9 months ago

@MichalLytek any updates on this issue?

MichalLytek commented 9 months ago

@shawnjones253 Setting target to es2022 makes this tests failing:

Summary of all failing tests
 FAIL  tests/functional/resolvers.ts
  ● Resolvers › Functional › shouldn't create properties of nullable args

    expect(received).not.toHaveProperty(path)

    Expected path: not "optionalField"

    Received value: undefined

      1818 |       expect(mutationResult.errors).toBeUndefined();
      1819 |
    > 1820 |       const result = mutationResult.data!.mutationWithTripleNestedInputs;
           |                                            ^
      1821 |       expect(result).toEqual(30);
      1822 |
      1823 |       expect(mutationInputValue).toBeInstanceOf(classes.SampleTripleNestedInput);

      at Object.<anonymous> (tests/functional/resolvers.ts:1820:44)

  ● Resolvers › Functional › should create instances of nested input fields input objects without undefined

    expect(received).not.toHaveProperty(path)

    Expected path: not "optionalNestedField"

    Received value: undefined

      1847 |     it("should create instance of root object when root type is provided", async () => {
      1848 |       const query = `query {
    > 1849 |         sampleQuery {
           |                                ^
      1850 |           fieldResolverWithRoot
      1851 |           getterField
      1852 |         }

      at Object.<anonymous> (tests/functional/resolvers.ts:1849:44)

I think I will just downgrade target in prisma generator to make it matching with type-graphql core.

MichalLytek commented 9 months ago

Closing as superseded by 4c053cc 🔒

shawnjones253 commented 9 months ago

@MichalLytek now could I just bug you for a new release 😅

shawnjones253 commented 9 months ago

@MichalLytek sorry to nag -- making another plea for a release -- our team is eager to get back to the fast version as it saves us hundreds of seconds when generating typegraphql types