使用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
相关产品推荐
相关产品推荐

