Ruby on rails 将Graphql用于Shopify API时的动态常量赋值
我在Shopify API上使用Graphql,还需要在查询中使用变量 这正是我得到的错误:Ruby on rails 将Graphql用于Shopify API时的动态常量赋值,ruby-on-rails,ruby,graphql,shopify,Ruby On Rails,Ruby,Graphql,Shopify,我在Shopify API上使用Graphql,还需要在查询中使用变量 这正是我得到的错误: SyntaxError (/home/fc-gui/app/controllers/concerns/product_graphql.rb:26: dynamic constant assignment FIRST_PRODUCTS = CLIENT.parse <<-'GRAPHQL' 任何能够在Ruby中运行带有变量的graphql查询的人都不会遇到这个错误吗?下面是解决方案。很难相信
SyntaxError (/home/fc-gui/app/controllers/concerns/product_graphql.rb:26: dynamic constant assignment
FIRST_PRODUCTS = CLIENT.parse <<-'GRAPHQL'
任何能够在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
这就是解决办法。很难相信这是一种通过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
使用此样式可以避免使用常量:
query = ShopifyAPI::GraphQL.client.parse <<-'GRAPHQL'
{
shop {
name
}
}
GRAPHQL
result = ShopifyAPI::GraphQL.client.query(query)
puts "Shop name: #{result.data.shop.name}"
我没有设置您的确切查询,但这解决了我正在进行的查询的问题
参考他们的文档:您可以避免使用此样式的常量:
query = ShopifyAPI::GraphQL.client.parse <<-'GRAPHQL'
{
shop {
name
}
}
GRAPHQL
result = ShopifyAPI::GraphQL.client.query(query)
puts "Shop name: #{result.data.shop.name}"
我没有设置您的确切查询,但这解决了我正在进行的查询的问题
他们文档中的引用:您试图写入方法体中的常量FIRST\u产品,这不是问题吗?你能试着用一个方法变量来代替吗?问题不在于你试图写入方法体中的常量FIRST_乘积吗?您可以尝试改用方法变量吗?