MichalLytek / typegraphql-prisma

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

Allow connect but not create/connectOrCreate in generated input type #326

Open dickfickling opened 2 years ago

dickfickling commented 2 years ago

Let's say I have two models - employee and company. Company has many employees.

I'd like to restrict the emitted types for the EmployeeCreateOneInput to only allow connecting a company, not createing one. I wouldn't be surprised if this feature already exists or has already been requested. I tried to search for it and couldn't find anything.

MichalLytek commented 2 years ago

Please show me the equivalent of Prisma Client query that you would want to execute by GraphQL API.

dickfickling commented 2 years ago

Here's a specific example:

model Company {
  id   String @id @default(cuid()) @db.Text()
  name String @db.Citext()

  /// @TypeGraphQL.omit(input: ["create"])
  employees Employee[]
}

model Employee {
  id        String @id @default(cuid()) @db.Text()
  name      String @db.Citext()
  companyId String

  company Company @relation(references: [id], fields: [companyId])
}

If I run prisma generate with that schema, the output includes an EmployeeCreateInput class like this:

export declare class EmployeeCreateInput {
    id?: string | undefined;
    name: string;
    company: CompanyCreateNestedOneWithoutEmployeesInput;
}

Looking at CompanyCreateNestedOneWithoutEmployeesInput, I see:

export declare class CompanyCreateNestedOneWithoutEmployeesInput {
    create?: CompanyCreateWithoutEmployeesInput | undefined;
    connectOrCreate?: CompanyCreateOrConnectWithoutEmployeesInput | undefined;
    connect?: CompanyWhereUniqueInput | undefined;
}

My request: I'd like to be able to specify a limitation in the schema file such that CompanyCreateNestedOneWithoutEmployeesInput only allows connect, and not connectOrCreate or create. e.g.

export declare class CompanyCreateNestedOneWithoutEmployeesInput {
    connect: CompanyWhereUniqueInput;
}

Not sure what syntax would make sense for that, maybe something like

  /// @TypeGraphQL.restrict(create: ["connect"])
  company Company @relation(references: [id], fields: [companyId])

Does this make sense?

MichalLytek commented 2 years ago

Ah, so you want to limit the capabilities? Sound like an interesting use case, will investigate 👀

dickfickling commented 1 year ago

I find myself wishing for this feature pretty regularly. @MichalLytek if I wanted to implement it myself, can you give me a very rough overview of how you'd suggest i do so?

MichalLytek commented 1 year ago

@dickfickling A rough overview of my workflow is to: 1) Find a good DX of API for marking relation input to only create (naming is hard, you know 😅) 2) Inspect DMMF to find where the "decorator" is attached. 3) Parse the documentation from DMMF into property of that type (in transform step) - might require more complex step of transferring info from model field into input type, which is hard. 4) Adjust the inputs generator to conditionally emit the fields. 5) Write tests that checks if the new behavior works correctly.