我在Shopify API上使用了Graphql,还需要在查询中使用变量。我找到了这篇文章,但是由于我正在使用那些查询变量,所以它不起作用。
这是我得到的确切错误:
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查询,但不会收到此错误?
这是解决方案。很难相信这是通过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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句