javascript - 棱镜2 : how to fetch nested fields?

标签 javascript node.js graphql prisma prisma-graphql

在 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/

相关文章:

javascript - Gatsby `createPage` 正在生成具有相同数据的页面

javascript - 我的 xul 文件中有 js,我想从主窗口调用特定函数

javascript - Javascript 安全审计的最佳工具是什么?

javascript - 在提交表单时,检查准备上传的文件的大小?

node.js - 如何将配置变量从服务器(Express)传递到 React App

javascript - GraphQL( Apollo ): Can you access directives within resolve function?

javascript - 选择一个选项时过滤其他选择选项

node.js - 环境 : node: No such file or directory in mac

javascript - Node.js 是否强制 setTimeout 的最小延迟?

javascript - vue-apollo 在路由器保护中执行 graphql 查询