【发布时间】:2021-10-27 04:44:57
【问题描述】:
我正在 graphql + apollo 客户端中创建一个查询,其过滤器和订单可以根据客户在前端选择的内容而改变。例如:
query (
$orderPart: String!
$wherePart: String!
) {
getProductInfo(
order {$orderPart}
where {$wherePart}
) {
productID
description
size
model
cathegoryNumber
}
其中 $orderPart 将等于“description: DESC”或“productID: ASC”(取决于客户在某一时刻选择了什么)。 $wherePart 将等于 "cathegoryNumber: {eq: 12}" 或 "productID: {eq: 111111}"。
我需要将 order/filter 子句完全作为参数传递。
但它不起作用。语法错误“语法错误:预期名称,找到 $”。
所以我的问题是......
有没有办法实现这些动态过滤器/订单?如何实现此功能?有没有其他方法来实现这个动态过滤器和订单?
谢谢。
注意: 在官方文档中,我发现只有值可以作为参数传递:
query (
$orderValue: sortEnumType! = DESC
$whereValue: String! = "description1"
) {
getProductInfo(
order {productID: $orderValue}
where {description: {eq: $whereValue} }
) {
productID
description
size
model
cathegoryNumber
}
但这不是我需要的,因为始终无法更改过滤器/订单。第一次 (product:ASC),第二次 (cathegoryNumber:DESC) 等等,它们每次都可能完全不同……
【问题讨论】:
-
“不起作用。”没用。请说明问题。一个错误?出乎意料的反应?
-
变量不用于字符串替换 - 使用
$where和$order(从 API 文档读取类型)并将对象作为变量传递