构建GraphQL API时,是否可以在嵌套查询属性上允许参数?也就是说,我已经为顶级orderBy
查询实现了tasks
arg,如下所示:
query {
tasks(orderBy: { order: asc }) {
title
}
}
,这很好用,但是我希望能够查询collection
个task
中的tasks
并将查询参数添加到嵌套的query {
collection {
id
name
tasks(orderBy: { order: asc }) {
title
}
}
}
属性中,如下所示:
"Unknown argument \"orderBy\" on field \"tasks\" of type \"Collection\"."
默认情况下它不识别参数,因此我假设如果可能,那么还需要进一步设置。我在尝试查询时遇到此错误:graphql-yoga
P.S。我在后端使用prisma
和row.names<-
。
答案 0 :(得分:0)
您是否使用 nexus / schema 和 nexus-plugin-prisma ?如果这样做,则必须像这样t.model.tasks({ ordering: true })
答案 1 :(得分:0)
啊哈!我最终解决了。我只需要将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
上拥有它)