【发布时间】:2020-03-01 23:39:43
【问题描述】:
我在 Shopify API 上使用 Graphql,并且还需要在我的查询中使用变量。 I found this post, but it didn't work because I am using those query variables.
这是我得到的确切错误:
SyntaxError (/home/fc-gui/app/controllers/concerns/product_graphql.rb:26: dynamic constant assignment
FIRST_PRODUCTS = CLIENT.parse <<-'GRAPHQL'
然后这是我尝试运行查询的方法
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
我已经尝试创建类似于上述帖子的客户端,就像这两个选项一样,但没有运气:
CLIENT = ShopifyAPI::GraphQL.new
##
def graphql_client
ShopifyAPI::GraphQL.new
end
任何人都能够在 Ruby 中使用变量运行 graphql 查询并且不会出现此错误?
【问题讨论】:
-
您尝试写入方法体内的常量 FIRST_PRODUCTS 不是问题吗?您可以尝试使用方法变量吗?
标签: ruby-on-rails ruby graphql shopify