I'm trying to learn to use Prisma and Nexus together. I'm hoping that someone more experienced can help me out.
I want to create a post with several images attached to it.
My Prisma models look like this:
model Post {
id String @id @default(cuid())
title String
body String
images Image[] @relation("ImagePost")
}
model Image {
id String @id @default(cuid())
post Post @relation("ImagePost", fields: [postId], references: [id])
postId String
name String
url String
}
I need to write a Nexus mutation that can accept a post with a title, body, and array of image urls. I need to create an Image record for each of the urls, and attach them to the post.
const Mutation = objectType({
name: 'Mutation',
definition(t) {
t.field('createPost', {
type: 'Post',
args: {
title: nonNull(stringArg()),
body: stringArg(),
images: ????
},
resolve: (_, args, context: Context) => {
return context.prisma.post.create({
data: {
title: args.title,
body: args.body,
images: ????
},
})
},
})
})
Can you help me to figure out how to do this correctly?
- I don't know how to pass an array of json objects to the args. There are things like
stringArg()
orintArg()
, but how do you accept an array? - What's the right way to create a bunch of
Image
objects and attach them to thePost
? Should I have a for loop and create them manually one by one, and then attach them somehow? Or is there a better way?
Do you know if there are any examples of people doing this kind of thing?