[英]graphql using nested query arguments on parent or parent arguments on nested query
[英]GraphQL nested query arguments?
在構建 GraphQL API 時,是否可以允許嵌套查詢屬性的參數? 即我已經為頂級tasks
查詢實現了一個orderBy
arg,如下所示:
query {
tasks(orderBy: { order: asc }) {
title
}
}
這工作正常,但我希望能夠查詢task
的collection
並將查詢參數添加到嵌套的tasks
屬性,如下所示:
query {
collection {
id
name
tasks(orderBy: { order: asc }) {
title
}
}
}
默認情況下它不識別參數,因此我假設如果可能,則需要進一步設置。 當我嘗試該查詢時出現此錯誤: "Unknown argument \\"orderBy\\" on field \\"tasks\\" of type \\"Collection\\"."
PS我在后端使用graphql-yoga
和prisma
。
你使用nexus/schema和nexus-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.