hayes / pothos

Pothos GraphQL is library for creating GraphQL schemas in typescript using a strongly typed code first approach
https://pothos-graphql.dev
ISC License
2.32k stars 158 forks source link

Prisma plugin failing on call to builder.prismaObject() #1190

Closed ad6025b closed 2 months ago

ad6025b commented 5 months ago

In a Prisma+Pothos+CloudFlare Worker context the following error happens at boot.

✘ [ERROR]  Uncaught TypeError: Cannot read properties of undefined (reading 'find')
  at index.js:72422:45 in getDefaultFindBy
  at index.js:72446:25 in getDefaultIDSelection
  at index.js:73247:35 in prismaObject
  at index.js:73676:9

Using

"@pothos/core": "^3.41.0",
"@pothos/plugin-prisma": "^3.65.0",
"@prisma/client": "^^5.12.1",

NOTE: as of Prisma >=5.11, they support native database connections (using pg) more info: https://www.prisma.io/docs/orm/prisma-client/deployment/edge/deploy-to-cloudflare#postgresql-traditional ==> it no longer need @prisma/client/edge to run in cloudflare worker edge environment (no need for prisma accelerate)

image

ad6025b commented 5 months ago

NOTE: the builder.toSchema({}) does load in a vitest unit test. (in test environment it works)

In cloudflare worker, when you run [wrangler dev], the schemal builder fails loading the PrismaObject

hayes commented 5 months ago

It looks like prisma has remove the dmmf from the client when it's running in edge environments (likely to improve startup time and reduce bundle size). Unfortunately Pothos is dependent on this information to construct the correct queries to resolve the schema.

I think potentially the fix for this would be to update the pothos prisma generator to genersate a minimal version of the dmmf that includes the information needed for pothos to build the graphql schema. I haven't done a deep dive on what this would require, or if there are other options. Definitely open to help on figuring out the best way to fix this

ad6025b commented 5 months ago

@hayes i logged the bug with prisma => https://github.com/prisma/prisma/issues/23913
==> I asked them to renable it for wasm/edge. The strange thing is the datamodel info is already generated even for the edge clients. They just disable you from accessing that property if you are in edge environment. (which is curious, since you are really not saving any efficiencies here. (they simply wont allow you to access the value ==> see the bug #23913 attached here)

I looked at your code, and the only thing pothos SchemaBuilder needs is the slimmed down version of the Prisma.dmmf. (the latest dmmf removes the extra "schema" and "mapping' properties. => so if we can access Prisma.dmmf then we are good for the SchemaBuilder. ==> i tested the Prisma.dmmf json (i persisted json to file manually, and set dmmf property for SchemaBuilder and it it works. ==> i asked the Prisma team to reenable Prisma.dmmf for edge (as per ticket #23913) => lets see what they say in the coming weeks.

I also looked your pothos plugin-prisma generator, and its easy enough to extract the [dmmf.datamodel] property either to separate file, or same ./types file that you generate (and add in extra property). => I have a simple test that extract dmmf.datamodel and compresses the json using "fflate" compression library. this is nice, it was able to compress the dmmf.datamodel down to 5Kb.

i can either create a PR and request to update your plugin-prisma, or i can create a new Prisma generator, just to create this dmmf.datramodel json temporarily until the Prisma team renables Prisma.dmmf.

what are your thought? Should i do the PR or create a new prisma generator?

hayes commented 5 months ago

Let's wait to see what Prisma recommends before fixing in Pothos I don't want to update this multiple times of they can provide a quick fix

hayes commented 5 months ago

If prismas recommendation is to generate the output in a pothos generator, I'd probably add it as a flag similar to the prismaUtils flag in the generator, and have the generate also export a variable likedmmf or datamodel when that flag is set to true.

Then you would just import the datamodel alongside the prisma types and pass it into the existing dmmf option for the prisma plugin

hayes commented 5 months ago

I probably wouldn't add compression in Pothos itself, I think compressing/decomressing could manually in apps, but I wouldn't want a pothos plugin to ship with a dependency on a compression package

ad6025b commented 5 months ago

@hayes i think Prisma. the [runtimeDataModel] will work. we just need access to this value accessible by the edge runtime via Prisma.dmmf

Prisma was open to doing this, as as the latest comments on https://github.com/prisma/prisma/issues/23913)

hayes commented 2 months ago

I've added a new option in the generator in the 4.0 release to generate a custom data model that doesn't depend on Prisma at all https://pothos-graphql.dev/docs/plugins/prisma/setup#edge-run-times

ad6025b commented 1 month ago

@hayes thehttps://pothos-graphql.dev/docs/plugins/prisma/setup#edge-run-times worked great! thx!

but there is peer dependency issue with your other package that i need [prisma-generator-pothos-codegen] ==> this package is still has older dependencies:

hayes commented 1 month ago

That package is maintained by someone else, id recommend opening an issue there instead

hayes commented 1 month ago

you can probably just open a PR to update the deps here: https://github.com/Cauen/prisma-generator-pothos-codegen/blob/master/package.json