简体   繁体   English

GraphQL 嵌套查询参数?

[英]GraphQL nested query arguments?

When building a GraphQL API, is it possible to allow arguments on nested query properties?在构建 GraphQL API 时,是否可以允许嵌套查询属性的参数? ie I've implemented an orderBy arg for a top-level tasks query like so:即我已经为顶级tasks查询实现了一个orderBy arg,如下所示:

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

and this works fine, but I would like to be able to query a collection of task s and add the query arguments to the nested tasks property like this:这工作正常,但我希望能够查询taskcollection并将查询参数添加到嵌套的tasks属性,如下所示:

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

It doesn't recognize the arguments by default, so I assume if it is possible, then there is some further set up required.默认情况下它不识别参数,因此我假设如果可能,则需要进一步设置。 I get this error when I try that query: "Unknown argument \\"orderBy\\" on field \\"tasks\\" of type \\"Collection\\"."当我尝试该查询时出现此错误: "Unknown argument \\"orderBy\\" on field \\"tasks\\" of type \\"Collection\\"."

PS I'm using graphql-yoga with prisma on the backend. PS我在后端使用graphql-yogaprisma

Do you use nexus/schema and nexus-plugin-prisma ?你使用nexus/schemanexus-plugin-prisma吗? If you do, you have to activate ordering in your collection task model like this t.model.tasks({ ordering: true })如果你这样做了,你必须像这样在你的收集任务模型中激活排序t.model.tasks({ ordering: true })

Aha!啊哈! I worked it out in the end.我最后解决了。 I just needed to pass the args to the tasks invocation in the Collection.tasks resolver:我只需要将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
  },
},

I also needed to add the orderBy option in my schema for the Collection.tasks field (I previously only had it on Query.tasks )我还需要在我的模式中为Collection.tasks字段添加orderBy选项(我以前只在Query.tasks上有它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM