Javascript prisma2:如何获取嵌套字段?
在prisma1中,我使用fragment获取嵌套字段 例如:Javascript prisma2:如何获取嵌套字段?,javascript,node.js,graphql,prisma,prisma-graphql,Javascript,Node.js,Graphql,Prisma,Prisma Graphql,在prisma1中,我使用fragment获取嵌套字段 例如: const mutations = { async createPost(_, args, ctx) { const user = await loginChecker(ctx); const post = await prisma.post .create({ data: { author: { connect: {
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",
用于在Prisma 2中快速加载关系
文档中的示例:
const result = await prisma.user.findOne({
where: { id: 1 },
include: { posts: true },
})
假设用户表具有一对多posts关系,这将返回用户对象以及posts字段。假设我在createComment解析器中包含{post and author}。当在操场上运行此命令时:
mutation CREATECOMMENT{CREATECOMMENT(text:“yep,very bad post dude”,postId:4){id text post{id title author:{id}}}author{name}}}
这里我如何获得posts{author:id}?因此,这与模式的定义方式有关,而不是prisma本身。假设CreateComent返回一个Comment
,Comment
类型需要将post
作为可查询字段。而Post
类型需要将author
作为可查询字段。从我的数据模型中可以看到:comment与Post有关系,Post与author有关系。但是,在创建评论时,我包括了{author:true,post:true,}。所以,我得到了帖子和作者的直接查询。但是当我试图获得post{author{id}时,我却无法获得它。prisma客户端也支持嵌套includes.findOne({includes:{post:{includes:{author:true}}}}}}}})
使用嵌套includes,我是否也可以添加where部分,以便只获取那些总记录,其中包括树深处的特定元素具有特定值的所有层次结构?