GraphQL嵌套查询参数?

问题描述

构建GraphQL API时,是否可以在嵌套查询属性上允许参数?也就是说,我已经为顶级orderBy查询实现了tasks arg,如下所示:

query {
  tasks(orderBy: { order: asc }) {
    title
  }
}

,这很好用,但是我希望能够查询collectiontask中的tasks并将查询参数添加到嵌套的query { collection { id name tasks(orderBy: { order: asc }) { title } } } 属性中,如下所示:

"UnkNown argument \"orderBy\" on field \"tasks\" of type \"Collection\"."

认情况下它不识别参数,因此我假设如果可能,那么还需要进一步设置。我在尝试查询时遇到此错误graphql-yoga

P.S。我在后端使用prismarow.names<-

解决方法

您是否使用 nexus / schema nexus-plugin-prisma ?如果这样做,则必须像这样t.model.tasks({ ordering: true })

那样激活收集任务模型中的订购。 ,

啊哈!我最终解决了。我只需要将args传递给tasks解析器中的Collection.tasks调用:

Collection: {
  tasks: (parent,args,context) => {
    const { id } = parent

    const collection = context.prisma.collection.findOne({
      where: { id },})

    return collection.tasks(args) // ← pass in `args` here
  },},

我还需要在我的架构中为orderBy字段添加Collection.tasks选项(我以前只在Query.tasks上拥有它)