簡體   English   中英

GraphQL 嵌套查詢參數?

[英]GraphQL nested query arguments?

在構建 GraphQL API 時,是否可以允許嵌套查詢屬性的參數? 即我已經為頂級tasks查詢實現了一個orderBy arg,如下所示:

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

這工作正常,但我希望能夠查詢taskcollection並將查詢參數添加到嵌套的tasks屬性,如下所示:

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

默認情況下它不識別參數,因此我假設如果可能,則需要進一步設置。 當我嘗試該查詢時出現此錯誤: "Unknown argument \\"orderBy\\" on field \\"tasks\\" of type \\"Collection\\"."

PS我在后端使用graphql-yogaprisma

你使用nexus/schemanexus-plugin-prisma嗎? 如果你這樣做了,你必須像這樣在你的收集任務模型中激活排序t.model.tasks({ ordering: true })

啊哈! 我最后解決了。 我只需要將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上有它)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM