Shopify Ruby Gem部署Heroku后GraphQL调用失败,本地运行正常
问题
我使用Shopify Gem(9.0.2版本)开发的Ruby应用,此前在Heroku上运行正常,但近期部署后无法解析GraphQL调用,本地环境执行相同流程却可正常运行。
具体执行流程:
- 本地和Heroku上均使用相同凭证、api_version初始化ShopifyAPI
- 构建查询请求:
query_variables = {'gid' => gid} query_str = <<-'GRAPHQL' query($gid: ID!) { product(id: $gid) { id } } GRAPHQL
- 初始化客户端:
client = ShopifyAPI::GraphQL.client - 尝试解析查询:
query_graph_ql = client.parse(query_str)
本地执行无问题,但Heroku上出现错误:NoMethodError (undefined method 'fields' for nil:NilClass)。且REST API在Heroku上可正常使用,更换店铺测试仍出现该问题。
Heroku rails控制台回溯信息:
app/vendor/bundle/ruby/2.6.0/gems/graphql-client-0.19.0/lib/graphql/client/document_types.rb:62:in `rescue in analyze_types' /app/vendor/bundle/ruby/2.6.0/gems/graphql-client-0.19.0/lib/graphql/client/document_types.rb:45:in `analyze_types' /app/vendor/bundle/ruby/2.6.0/gems/graphql-client-0.19.0/lib/graphql/client.rb:189:in `parse' /app/lib/utilities/graphql.rb:29:in `query_shopify' /app/lib/utilities/graphql.rb:368:in `get_product_by_id' (irb):4:in `irb_binding' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb/workspace.rb:85:in `eval' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb/workspace.rb:85:in `evaluate' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb/context.rb:385:in `evaluate' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb.rb:493:in `block (2 levels) in eval_input' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb.rb:647:in `signal_status' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb.rb:490:in `block in eval_input' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb/ruby-lex.rb:246:in `block (2 levels) in each_top_level_statement' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb/ruby-lex.rb:232:in `loop' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb/ruby-lex.rb:232:in `block in each_top_level_statement' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb/ruby-lex.rb:231:in `catch' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb/ruby-lex.rb:231:in `each_top_level_statement' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb.rb:489:in `eval_input' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb.rb:428:in `block in run' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb.rb:427:in `catch' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb.rb:427:in `run' /app/vendor/ruby-2.6.6/lib/ruby/2.6.0/irb.rb:383:in `start' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.8.1/lib/rails/commands/console/console_command.rb:64:in `start' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.8.1/lib/rails/commands/console/console_command.rb:19:in `start' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.8.1/lib/rails/commands/console/console_command.rb:96:in `perform' /app/vendor/bundle/ruby/2.6.0/gems/thor-1.3.1/lib/thor/command.rb:28:in `run' /app/vendor/bundle/ruby/2.6.0/gems/thor-1.3.1/lib/thor/invocation.rb:127:in `invoke_command' /app/vendor/bundle/ruby/2.6.0/gems/thor-1.3.1/lib/thor.rb:527:in `dispatch' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.8.1/lib/rails/command/base.rb:69:in `perform' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.8.1/lib/rails/command.rb:46:in `invoke' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.8.1/lib/rails/commands.rb:18:in `<top (required)>' /app/bin/rails:4:in `require' /app/bin/rails:4:in `<main>'
可能的故障原因
- GraphQL Schema 未正确加载:ShopifyAPI的GraphQL客户端依赖预加载的Schema解析查询。本地环境可能已缓存Schema,而Heroku部署后未生成或加载Schema文件。检查Heroku上是否存在
.graphql-client目录或预编译的Schema文件,确认部署流程包含Schema生成步骤(比如运行rake shopify_api:graphql:dump)。 - 依赖版本冲突:虽然Shopify Gem是9.0.2,但依赖的
graphql-client(0.19.0)可能在Heroku环境与其他gem存在隐性冲突。本地的Gemfile.lock可能未同步到Heroku,导致Heroku安装的依赖版本与本地不一致。重新部署时推送最新的Gemfile.lock,并在Heroku执行bundle install强制更新依赖。 - 环境变量缺失或错误:初始化ShopifyAPI时,Heroku的环境变量(如
SHOPIFY_API_VERSION、店铺域名、API密钥等)可能缺失或值错误。REST API能正常工作,但GraphQL客户端对Schema版本校验更严格,若API版本不匹配会导致Schema加载失败。核对Heroku环境变量与本地的一致性,重点确认api_version参数是否正确设置。 - Heroku文件系统限制:Heroku的临时文件系统会在dyno重启后清除文件,若Schema是运行时动态生成的,可能导致文件丢失或无法读取。将Schema文件添加到版本控制,确保部署时直接包含预生成的Schema,而非运行时生成。
- 网络访问限制:Heroku环境可能无法访问Shopify的GraphQL Schema端点,导致客户端无法拉取Schema。尽管REST API正常,但Schema拉取的端点可能有不同的网络策略。检查Heroku的出站网络规则,确认允许访问
https://<shop-domain>/admin/api/<version>/graphql.json端点。
内容的提问来源于stack exchange,提问作者bseido
相关产品推荐
相关产品推荐

