我正在使用 Evershop Node 电子商务平台构建一个应用程序。根据他们的文档,我添加了产品列表 API 的 GraphQL 查询来获取产品列表。
query GetAllProducts($filters: [FilterInput]) {
products(filters: $filters) {
items {
name
sku
status
image {
alt
thumb
origin
}
price {
regular {
value
text
}
special {
value
text
}
}
weight {
value
unit
}
}
total
currentFilters{
key
value
}
}
}
在此查询中,我必须将过滤器值传递给变量
filters
,如下所示:
const filters = [
{
key: "limit",
operation: "eq",
value: '10'
},
{
key: "page",
operation: "eq",
value: '1'
},
{
key: "name",
operation: "ilike",
value: '%jeans%'
}
];
正如您在我的示例中看到的,您可以看到有 price 字段,它被定义为一个对象。如何根据价格字段按产品列表进行过滤?
要过滤价格对象中的特定字段,请使用点表示法直接访问这些字段。
例如,要过滤正常价格大于 50 的产品,您可以按如下方式修改过滤器数组: JavaScript
const filters = [
// ... other filters
{
key: "price.regular.value", // Access nested field using dot notation
operation: "gt", // Filter for values greater than
value: '50'
}
];