在 prisma 1 中,我使用片段来获取嵌套字段。
例如:
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;
},
};
但似乎在 prisma2 中不受支持。因为通过在操场上运行它,
mutation CREATEPOST {
createPost(
title: "How to sleep?"
body: "Eat, sleep, repaet"
published: true
) {
title
body
published
author {
id
}
}
}
我正进入(状态,
"prisma.post.create(...).$fragment is not a function",
最佳答案
include option用于在 Prisma 中急切地加载关系。
来自文档的示例:
const result = await prisma.user.findOne({
where: { id: 1 },
include: { posts: true },
})
假设用户表具有一对多的帖子关系,这也将返回带有帖子字段的用户对象。Prisma 也支持嵌套,例如:
const result = await prisma.user.findOne({
where: { id: 1 },
include: {
posts: {
include: {
author: true,
}
},
},
})
关于javascript - 棱镜2 : how to fetch nested fields?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62046070/