如何在EverShop中使用GraphQL对嵌套价格对象进行过滤查询
如何在EverShop的GraphQL产品查询中基于嵌套的price字段进行过滤?
我正在使用EverShop Node电商平台开发应用,按照官方文档实现了获取产品列表的GraphQL查询,查询代码及filters变量示例如下:
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 } } }
const filters = [ { key: "limit", operation: "eq", value: '10' }, { key: "page", operation: "eq", value: '1' }, { key: "name", operation: "ilike", value: '%jeans%' } ];
查询中price为嵌套对象,包含regular(常规价)和special(特价)两个子字段,请问如何基于该字段对产品列表进行过滤?
解决方案
EverShop的FilterInput支持通过点语法指定嵌套字段的完整路径,以此实现对嵌套对象的过滤。以下是几种常见的price字段过滤场景示例:
1. 过滤常规价在指定范围内的产品
筛选常规价大于等于100且小于等于200的产品:
const filters = [ { key: "limit", operation: "eq", value: '10' }, { key: "page", operation: "eq", value: '1' }, { key: "price.regular.value", operation: "gte", value: '100' }, { key: "price.regular.value", operation: "lte", value: '200' } ];
2. 过滤存在特价的产品
筛选有特价(special.value不为空)的产品:
const filters = [ { key: "limit", operation: "eq", value: '10' }, { key: "page", operation: "eq", value: '1' }, { key: "price.special.value", operation: "isNotNull", value: '' } ];
3. 过滤特价等于指定金额的产品
筛选特价为99的产品:
const filters = [ { key: "limit", operation: "eq", value: '10' }, { key: "page", operation: "eq", value: '1' }, { key: "price.special.value", operation: "eq", value: '99' } ];
4. 组合多个price过滤条件
筛选常规价大于150,且特价小于120的产品:
const filters = [ { key: "limit", operation: "eq", value: '10' }, { key: "page", operation: "eq", value: '1' }, { key: "price.regular.value", operation: "gt", value: '150' }, { key: "price.special.value", operation: "lt", value: '120' } ];
关键说明
- 用
key: "price.regular.value"或key: "price.special.value"指定嵌套字段的完整路径 - 支持的操作符和普通字段一致,包括
eq(等于)、gt(大于)、lt(小于)、gte(大于等于)、lte(小于等于)、isNotNull(非空)等 - 数值类型的过滤值需以字符串形式传入(和示例中的limit、page参数格式保持一致)
内容的提问来源于stack exchange,提问作者Sukanya Purushothaman
相关产品推荐
相关产品推荐

