javascript - prisma2:如何获取嵌套字段?
问题描述
在 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 选项用于在 Prisma 2 中急切地加载关系。
来自文档的示例:
const result = await prisma.user.findOne({
where: { id: 1 },
include: { posts: true },
})
假设用户表具有一对多的帖子关系,这也将返回带有帖子字段的用户对象。
推荐阅读
- c - 当输入为 10 并且阶乘值仅返回最多 33 个数字时程序失败
- javascript - 如何通过 JS 禁用或启用 html 组件?
- android - 当用户点击 FCM 通知消息时,如何清除活动堆栈?
- ios - 具有不同字体的 NSMutableAttributedString
- dialogflow-es - 用于流式意图检测的 DialogFlow 与 Lex
- runtime-error - 奇怪的 Perl SIGPIPE 行为(“IGNORE”有效,sub 无效)
- ajax - 如何在 laravel post 方法中从 ajax 中删除 403 错误
- java - synchronized 会自动标记变量 volatile 吗?
- .htaccess - Prestashop URL 制作
- javascript - href 标签下载属性 - 如何使其强制下载外部托管图像?