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

qgzx9mmu  于 9个月前  发布在  Node.js
关注(0)|答案(1)|浏览(108)

我正在使用Evershop Node电子商务平台构建一个应用程序。根据他们的文档,我添加了GraphQL查询产品列表API以获取产品列表。

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

lp0sw83n

lp0sw83n1#

要过滤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'
    }
];

字符串

相关问题