Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexdoan102 opened this issue Apr 17, 2024 · 7 comments
Open

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

alexdoan102 opened this issue Apr 17, 2024 · 7 comments

Comments

@alexdoan102
Copy link

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

@alexdoan102
Copy link
Author

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
Copy link
Owner

hayes commented Apr 17, 2024

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

@alexdoan102
Copy link
Author

alexdoan102 commented Apr 22, 2024

@hayes i logged the bug with prisma => prisma/prisma#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
Copy link
Owner

hayes commented Apr 24, 2024

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
Copy link
Owner

hayes commented Apr 24, 2024

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
Copy link
Owner

hayes commented Apr 24, 2024

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

@alexdoan102
Copy link
Author

@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 prisma/prisma#23913)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants