You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopify折扣函数部署报错:field_a不存在于Attribute类型

解决Shopify折扣函数中Cart自定义属性查询错误的问题

错误根源

你遇到的Field 'field_a' doesn't exist on type 'Attribute'错误,核心原因是GraphQL查询结构不符合Shopify的CartLine属性定义:

  • Shopify中CartLine的自定义属性字段是复数的attributes,而非单数attribute
  • 每个Attribute类型只有key和value两个字段,不存在直接的field_a字段,自定义属性是以键值对形式存储的

修复步骤

1. 修正GraphQL输入查询

调整查询,获取CartLine的所有自定义属性键值对,后续在代码中筛选出field_a对应的值:

query Input {
  cart {
    lines {
      attributes {
        key
        value
      }
      merchandise {
        ...on ProductVariant {
          id
        }
      }
      quantity
    }
  }
}

2. 调整Rust结构体定义

对应修改Api.rs中的结构体,适配Shopify返回的属性数组结构,并添加逻辑提取field_a的值:

#[derive(Clone, Debug, Deserialize)]
pub struct CartLine {
    pub quantity: Int,
    pub merchandise: Merchandise,
    pub attributes: Vec<Attribute>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct Attribute {
    pub key: String,
    pub value: String,
}

// 为CartLine添加方法,提取field_a的值(转为i64,默认0)
impl CartLine {
    pub fn field_a(&self) -> i64 {
        self.attributes
            .iter()
            .find(|attr| attr.key == "field_a")
            .and_then(|attr| attr.value.parse().ok())
            .unwrap_or(0)
    }
}

3. 代码中使用提取的属性值

在折扣函数逻辑中,直接调用cart_line.field_a()即可获取对应的值,无需依赖结构体字段的默认值处理。

额外说明

  • 即使部分CartLine没有field_a属性,上述代码会自动返回默认值0,符合你之前的需求
  • 部署前可以先通过Shopify GraphQL Playground测试查询结构,确认返回的数据格式正确

内容的提问来源于stack exchange,提问作者main55

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 09:35:21