【问题标题】:GraphQL nested query arguments?GraphQL 嵌套查询参数?
【发布时间】:2021-01-14 08:40:47
【问题描述】:

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

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

这很好用,但我希望能够查询 collectiontasks 并将查询参数添加到嵌套的 tasks 属性中,如下所示:

query {
  collection {
    id
    name
    tasks(orderBy: { order: asc }) {
      title
    }
  }
}

默认情况下它不识别参数,所以我假设如果可能,则需要进一步设置。尝试该查询时出现此错误:"Unknown argument \"orderBy\" on field \"tasks\" of type \"Collection\"."

附:我在后端使用graphql-yogaprisma

【问题讨论】:

  • 有可能。如果可能的话,您能否分享您的设置,就像您使用 Nexus 和 Prisma 一样?

标签: graphql apollo apollo-server prisma prisma-graphql


【解决方案1】:

你使用 nexus/schemanexus-plugin-prisma 吗?如果你这样做了,你必须像这样t.model.tasks({ ordering: true })

在你的收集任务模型中激活排序

【讨论】:

    【解决方案2】:

    啊哈!我最终解决了它。我只需要将args 传递给Collection.tasks 解析器中的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
      },
    },
    

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

    【讨论】:

      猜你喜欢
      • 2019-05-08
      • 2021-01-29
      • 2020-12-02
      • 2020-12-31
      • 1970-01-01
      • 2018-06-13
      • 2019-05-30
      • 2017-06-16
      • 2017-07-17
      相关资源
      最近更新 更多