Tod*_*ddT 1 ruby ruby-on-rails shopify graphql
我在 Shopify API 上使用 Graphql,并且还需要在查询中使用变量。我找到了这篇文章,但它不起作用,因为我正在使用这些查询变量。
这就是我收到的确切错误:
SyntaxError (/home/fc-gui/app/controllers/concerns/product_graphql.rb:26: dynamic constant assignment
FIRST_PRODUCTS = CLIENT.parse <<-'GRAPHQL'
Run Code Online (Sandbox Code Playgroud)
然后这是我尝试运行查询的方法
def run_first_query
FIRST_PRODUCTS = CLIENT.parse <<-'GRAPHQL'
query($first: Int){
products(first: $first) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
featuredImage {
originalSrc
}
}
}
}
}
GRAPHQL
first = { "first": number_of_products_to_return}
@query_result = CLIENT.query(FIRST_PRODUCTS, variables: first)
get_last_cursor
end
Run Code Online (Sandbox Code Playgroud)
我尝试创建类似于上述帖子的客户端,例如这两个选项,但没有运气:
CLIENT = ShopifyAPI::GraphQL.new
##
def graphql_client
ShopifyAPI::GraphQL.new
end
Run Code Online (Sandbox Code Playgroud)
任何人都能够在 Ruby 中使用变量运行 graphql 查询并且不会出现此错误?
这是解决方案。很难相信这是一种通过 API 访问数据的“新”方式。它更慢、更复杂、更冗长。我根本没有得到好处。
def run_first_query
query = <<-'GRAPHQL'
query($first: Int){
products(first: $first) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
featuredImage {
originalSrc
}
}
}
}
}
GRAPHQL
first = {
"first": number_of_products_to_return,
}
Kernel.const_set(:ProductQuery, graphql_client.parse(query))
@query_result = graphql_client.query(ProductQuery, variables: first)
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1140 次 |
| 最近记录: |