omar-dulaimi / prisma-trpc-generator

Prisma 2+ generator to emit fully implemented tRPC routers
MIT License
676 stars 34 forks source link

Invalid types for the "groupBy" function #48

Closed qmo-harpuneetghuman closed 1 year ago

qmo-harpuneetghuman commented 1 year ago

Bug description

There is an invalid type error in the case of the groupBy function for all the generated routers. I am building a Next.js app (with Typescript) and it fails during the build process with the following error:

/prisma/generated/routers/Account.router.ts:91:63
Type error: Argument of type '{ where?: AccountWhereInput | undefined; orderBy?: AccountOrderByWithAggregationInput | AccountOrderByWithAggregationInput[] | undefined; take?: number | undefined; skip?: number | undefined; having?: AccountScalarWhereWithAggregatesInput | undefined; by: ("id" | ... 10 more ... | "session_state")[]; }' is not assignable to parameter of type '{ where?: AccountWhereInput | undefined; orderBy?: AccountOrderByWithAggregationInput | AccountOrderByWithAggregationInput[] | undefined; take?: number | undefined; skip?: number | undefined; having?: AccountScalarWhereWithAggregatesInput | undefined; by: ("id" | ... 10 more ... | "session_state")[]; } & { ...; }'.
  Type '{ where?: AccountWhereInput | undefined; orderBy?: AccountOrderByWithAggregationInput | AccountOrderByWithAggregationInput[] | undefined; take?: number | undefined; skip?: number | undefined; having?: AccountScalarWhereWithAggregatesInput | undefined; by: ("id" | ... 10 more ... | "session_state")[]; }' is not assignable to type '{ orderBy: Enumerable<AccountOrderByWithAggregationInput> | undefined; }'.
    Property 'orderBy' is optional in type '{ where?: AccountWhereInput | undefined; orderBy?: AccountOrderByWithAggregationInput | AccountOrderByWithAggregationInput[] | undefined; take?: number | undefined; skip?: number | undefined; having?: AccountScalarWhereWithAggregatesInput | undefined; by: ("id" | ... 10 more ... | "session_state")[]; }' but required in type '{ orderBy: Enumerable<AccountOrderByWithAggregationInput> | undefined; }'.

  89 |     input: AccountGroupBySchema,
  90 |     async resolve({ ctx, input }) {
> 91 |       const groupByAccount = await ctx.prisma.account.groupBy(input);
     |                                                               ^
  92 |       return groupByAccount;
  93 |     },
  94 |   })

How to reproduce

  1. Create a Next.js project with a T3-stack
  2. Install prisma-trpc-generator package
  3. Try to build the app

Expected behavior

The routers should have the correct types for the groupBy functions, resulting in the app being successfully built.

Prisma information

generator client {
  provider = "prisma-client-js"
}

generator trpc {
  provider       = "prisma-trpc-generator"
  withMiddleware = false
  withShield     = false
  contextPath    = "../../../../src/context"
}

datasource db {
  provider = "sqlite"
  // NOTE: When using postgresql, mysql or sqlserver, uncomment the @db.Text annotations in model Account below
  // Further reading:
  // https://next-auth.js.org/adapters/prisma#create-the-prisma-schema
  // https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#string
  url      = env("DATABASE_URL")
}

model Example {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

// Necessary for Next auth
model Account {
  id                String  @id @default(cuid())
  userId            String
  type              String
  provider          String
  providerAccountId String
  refresh_token     String? // @db.Text
  access_token      String? // @db.Text
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? // @db.Text
  session_state     String?
  user              User    @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

Environment & setup

Prisma Version

Environment variables loaded from .env
prisma                  : 4.6.1
@prisma/client          : 4.6.1
Current platform        : darwin-arm64
Query Engine (Node-API) : libquery-engine 694eea289a8462c80264df36757e4fdc129b1b32 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine        : migration-engine-cli 694eea289a8462c80264df36757e4fdc129b1b32 (at node_modules/@prisma/engines/migration-engine-darwin-arm64)
Introspection Engine    : introspection-core 694eea289a8462c80264df36757e4fdc129b1b32 (at node_modules/@prisma/engines/introspection-engine-darwin-arm64)
Format Binary           : prisma-fmt 694eea289a8462c80264df36757e4fdc129b1b32 (at node_modules/@prisma/engines/prisma-fmt-darwin-arm64)
Format Wasm             : @prisma/prisma-fmt-wasm 4.6.1-3.694eea289a8462c80264df36757e4fdc129b1b32
Default Engines Hash    : 694eea289a8462c80264df36757e4fdc129b1b32
Studio                  : 0.476.0
Shahidul1004 commented 1 year ago

I am also facing this error. Removal of the switch case produces this issue. Although @omar-dulaimi warned me about this, I did not get any errors at that time(during integrating include and select fields). I am working on this.

omar-dulaimi commented 1 year ago

Hey @Shahidul1004 Thank you for your continued support of this project. I just looked at the problem, and we actually don't need to revert your change.

We only need to remove the ".optional()" for the orderBy field when generating the groupBy schema.

image

I'll fix and release it now.

Thanks

omar-dulaimi commented 1 year ago

Released in 0.7.1

Let me know if it works for you!

qmo-harpuneetghuman commented 1 year ago

It works now. Thanks!

Shahidul1004 commented 1 year ago

Hi @omar-dulaimi, I am facing an issue with groupBy after the 0.7.1 release. Since you have removed optional from orderBy, the groupBy schema always requires orderBy field. And I think that will create a problem because orderBy is only required if we use take and/or skip in groupBy schema, otherwise, it is optional. Here is the link to the prisma doc. link