Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  192] [ 1]  / answers: 1 / hits: 11009  / 4 Years ago, wed, may 27, 2020, 12:00:00

In prisma 1 I have used fragment to fetch the nested fields.



For example:



const mutations = {
async createPost(_, args, ctx) {
const user = await loginChecker(ctx);
const post = await prisma.post
.create({
data: {
author: {
connect: {
id: user.id,
},
},
title: args.title,
body: args.body,
published: args.published,
},
})
.$fragment(fragment);

return post;
},
};


but seems like in prisma2 it is not supported. because by running this on playground,



mutation CREATEPOST {
createPost(
title: How to sleep?
body: Eat, sleep, repaet
published: true
) {
title
body
published
author {
id
}
}
}



I am getting,



prisma.post.create(...).$fragment is not a function,

More From » node.js

 Answers
1

The include option is used to eagerly load relations in Prisma.


Example from docs:


const result = await prisma.user.findOne({
where: { id: 1 },
include: { posts: true },
})

Assuming a user table with a one-to-many posts relation, this will return back the user object with the posts field as well.


Prisma also supports nesting as well, for example:


const result = await prisma.user.findOne({
where: { id: 1 },
include: {
posts: {
include: {
author: true,
}
},
},
})

[#3674] Monday, May 25, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryantc

Total Points: 455
Total Questions: 96
Total Answers: 110

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
bryantc questions
Fri, Aug 13, 21, 00:00, 3 Years ago
Tue, Mar 30, 21, 00:00, 3 Years ago
Fri, Jun 5, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;