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

Compilation fails when importing "OrderByWithRelationInput" types #397

Closed SheaBelsky closed 1 year ago

SheaBelsky commented 1 year ago

Describe the Bug The orderByNulls feature flag was recently promoted from preview to General Availability in Prisma 4.16.0.

https://github.com/prisma/prisma/issues/19377

This shouldn't have impacted existing instances of TypeGraphQL-Prisma because:

  1. This library isn't 4.16.2 compatible (yet)
  2. The underlying changes to the Prisma libraries should not have reflected in TypeGraphQL-Prisma yet.

However, I have experienced consistent compilation failures as a result of this feature. Consider this argument for a query:

import {
  UserOrderByWithRelationInput
} from "@generated/type-graphql";
// In a query
@Arg("orderBy", () => UserOrderByWithRelationInput) orderBy: UserOrderByWithRelationInput,
// With Prisma
return prisma.user.findMany({
  orderBy
  // and other arguments
});

Since this behavior is now the default, my generated resolvers (which believe that the flag is enabled) conflict with the generated Prisma client (which is still on version 4.15.0 with the feature flag off.) I was required to manually override all of the @prisma NPM dependencies to force them to be on version 4.15.0.

The specific error:

error TS2322: Type 'UserOrderByWithRelationInput' is not assignable to type 'Enumerable<UserOrderByWithRelationInput> | undefined'.
  Type 'import("/node_modules/@generated/type-graphql/resolvers/inputs/UserOrderByWithRelationInput").UserOrderByWithRelationInput' is not assignable to type 'import("/node_modules/.prisma/client/index").Prisma.UserOrderByWithRelationInput'.
    Types of property 'lastLogin' are incompatible.
      Type 'SortOrderInput | undefined' is not assignable to type 'SortOrder | undefined'.
        Type 'SortOrderInput' is not assignable to type 'SortOrder | undefined'.

Adding this to the bottom of my package.json file solved the issue:

  "overrides": {
    "@prisma/debug": "4.15.0",
    "@prisma/engines": "4.15.0",
    "@prisma/fetch-engine": "4.15.0",
    "@prisma/generator-helper": "4.15.0",
    "@prisma/get-platform": "4.15.0",
    "@prisma/internals": "4.15.0"
  }

To Reproduce

  1. Import the OrderByWithRelation types as part of a GraphQL resolver (such as an argument to an operation), then use that argument with the Prisma client
  2. Attempt to compile the Prisma client and TypeGraphQL types
  3. Observe the underlying TypeScript error

Expected Behavior I expect the version of typegraphql-prisma to be locked to the current compatible version, and not be flexible when it comes to the version of @prisma dependencies that it relies on.

Logs If applicable, add some console logs to help explain your problem. You can paste the errors with stack trace that were printed when the error occurred.

Environment (please complete the following information):

Additional Context It would be possible to solve this error by locking all @prisma dependencies to the current minor version of @prisma/client that is supported by removing the leading caret ^ from these dependencies.

These are the dependencies that would need to be tweaked:

I also recommend creating an .npmrc file in the root of the repo with save-exact=true as its contents to ensure that any future third-party libraries are always installed with their exact versions, and not with a flexible version.

MichalLytek commented 1 year ago

It's on purpose. Because of #385, I don't have time to keep this lib always up to date. And 90% of the time, Prisma version bumps don't break stuff. So I've changed ~ to ^ to let people try to use the newest version.

Maybe there should be a better warning or disclaimer that version x.y.z is tested and supported and newer might not work?

SheaBelsky commented 1 year ago

I understand that you lack the time/support to maintain this at the same level as before. As a result, I haven't upgraded to Prisma 4.16.2 since this library doesn't explicitly support it yet, exactly for the reason in this issue. If this library only explicitly supports version 4.15, then this library should reflect that so it's clear to implementors what the expectation is.

Maybe there should be a better warning or disclaimer that version x.y.z is tested and supported and newer might not work?

NPM would take care of this based on the version range OR on peer dependencies. If this library specifies peer dependencies for a specific version of Prisma, then NPM would warn developers if they try to use an incompatible version if Prisma. You could also add some sort of manual warning, but that seems like reinventing the wheel if NPM can already do something similar.

https://nodejs.org/fa/blog/npm/peer-dependencies

MichalLytek commented 1 year ago

then NPM would warn developers

No, now NPM yells and errors when you have wrong version. You have to force install the wrong package version.