如何在 EverShop 中使用 GraphQL 查询嵌套对象

问题描述 投票:0回答:1

我正在使用 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 字段,它被定义为一个对象。如何根据价格字段按产品列表进行过滤?

node.js graphql e-commerce
1个回答
0
投票

要过滤价格对象中的特定字段,请使用点表示法直接访问这些字段。

例如,要过滤正常价格大于 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'
    }
];
© www.soinside.com 2019 - 2024. All rights reserved.