首页 > 解决方案 > 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",

标签: javascriptnode.jsgraphqlprismaprisma-graphql

解决方案


include 选项用于在 Prisma 2 中急切地加载关系。

来自文档的示例:

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

假设用户表具有一对多的帖子关系,这也将返回带有帖子字段的用户对象。


推荐阅读