prisma2:如何获取嵌套字段?

prisma2: how to fetch nested fields?

在 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 2 中预先加载关系。

来自文档的示例:

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

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