Mutation type when using Prisma to create connected records #1669
Unanswered
timReynolds
asked this question in
Q&A
Replies: 1 comment 1 reply
-
|
Thought it was worth throwing some more notes down while working with mutations a little more.
// This doesn't include the model types
// import db, { Prisma } from "db"
// However this will
import Prisma from "@prisma/client"
// So you can then do
const field: Prisma.Field = {} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi I've just started using blitz and came across an interesting side effect of reusing the Prisma types in mutations when creating nested relationships.
As per the prisma documentation you can do the following;
This in itself is fine, however due to the reuse of of the Prisma generated
*CreateArgstype in the mutation if you pass something like the following data structure from a form you'll get a type error as the mutation expects the shape of data to match that being passed into the Prisma create method which includes the extracreatekeyword.Note: this shape is probably closer to what you'd expect the form to output if using something like Formik FieldArray
This was touched on in this discussion also and I agree that reshaping the data in the migration itself is probably the correct approach.
What I found misleading however is the reuse of the Prisma types for the mutation. My guess is this has been done for convenience and this isn't something many people are probably doing. Ideally the type for the mutation would be generated and not include the Prisma specific types but my guess is this would be a considerable amount of effort.
In my local setup I've change the mutation type to still use those in Prisma but switch to the
*CreateInputomitting the relationship and then adding it to the type using the entities*CreateInput, this is probably easier to understand with a code example pulled from what I've been working on;I mostly opened this discussion for anyone else that discovers this but on reflection it might be worth considering updating the code generator for the scaffolding command to do something closer to the types outlined above.
Would be interested to hear peoples thoughts.
Beta Was this translation helpful? Give feedback.
All reactions