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

使用shopify_api gem调用Shopify API如何传递变量执行查询

问题原因

你的GraphQL查询语句存在结构语法错误:变量声明的query操作外层被多余的大括号包裹,导致解析器无法识别变量定义符号$。

修正方案

1. 调整GraphQL查询结构

将query_using_location_ids方法内的GraphQL语句修改为如下格式,移除外层多余的大括号:

def query_using_location_ids
      client.parse <<-'GRAPHQL'
      query($locationId: ID!){
        inventoryItems(first:10){
          pageInfo{
            hasNextPage
            hasPreviousPage
          }
          edges{
            node{
              id
              legacyResourceId
              inventoryLevel(locationId: $locationId){
                id
                available
                incoming
                location{ id }
                item{
                  id
                  legacyResourceId
                  sku
                  variant{
                    id
                    legacyResourceId
                    price
                    product{
                      description
                    }
                  }
                  unitCost{
                    amount
                    currencyCode
                  }
                }
              }
            }
          }
        }
      }
      GRAPHQL
end

2. 可选修正变量类型转换

如果你使用的版本没有内置全局的ID()转换方法,可将fetch_by_location方法中的变量传值调整为直接传入id字符串:

def fetch_by_location(id)
  client.query(query_using_location_ids, variables: {locationId: id}).original_hash["data"]["inventoryItems"]
end

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 06:30:02