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

Enforce pagination on generated resolvers #446

Open shawnjones253 opened 8 months ago

shawnjones253 commented 8 months ago

Is your feature request related to a problem? Please describe. It is not possible to enforce pagination using auto-generated resolvers. This makes it easy for API requests to ask for more data than can fit into memory, crashing the API.

Describe the solution you'd like For context, we're using the generated relational resolvers currently, but are hoping to implement graphql amount limiting / pagination across the API. Something similar to this.

In resolvers we've written ourselves, I'd simply replace the type of take in any FindManyXYZArgs with a custom scalar that enforces minimum and maximum values, additionally making it required rather than nullable.

Unfortunately this isn't possible with generated resolvers.

I'd imagine an ideal solution would allow me to do something like:

Would you consider a PR to add this to the generator?

generator typegraphql {
  provider            = "typegraphql-prisma"
  findManyTakeType = "./path/to/myCustomScalar.ts"
}

Describe alternatives you've considered As an alternative, I've considered doing a one-time generation of the relational resolvers to my repository, then discontinuing use of the generated relational resolvers going forward. This creates a fairly large (new) burden on developers for future work however.

MichalLytek commented 8 months ago

I would rather see defaultTake = 20 or maxTake = 100 config that would generate proper statement in generated resolver to set value for take/limit.

For overwriting scalars we have a different ticket and a different approach.

electrovir commented 7 months ago

@MichalLytek is there currently a way to add those defaultTake or maxTake configs you mentioned? Or were you saying that that's what this requested feature needs to add?

shawnjones253 commented 7 months ago

He’s saying that’s what I’d need to add if submitting a PR. I do plan to make such a PR in the next few weeks.