【问题标题】:How to do a basic GraphQL query to Shopify with Ruby如何使用 Ruby 对 Shopify 进行基本的 GraphQL 查询
【发布时间】:2021-05-10 19:56:20
【问题描述】:

我正在尝试使用 Sinatra 对 Shopify 商店进行基本的 GraphQL 查询。有人可以帮我弄清楚我做错了什么吗?我查看了他们的 API 来做到这一点:

require 'shopify_api'
require 'sinatra'

class App < Sinatra::Base
  get '/' do
    shop  = 'xxxx.myshopify.com'
    token = 'shpat_xxxxxxxxxxxxxxxxxxxxxx'
    session = ShopifyAPI::Session.new(domain: shop, token: token, api_version: "2021-04")
    ShopifyAPI::Base.activate_session(session)
    
    ShopifyAPI::GraphQL.initialize_clients
    client = ShopifyAPI::GraphQL.client

    SHOP_NAME_QUERY = client.parse <<-'GRAPHQL'
      {
        shop {
          name
        }
      }
    GRAPHQL

    result = client.query(SHOP_NAME_QUERY)
    result.data.shop.name
  end
end

这给出了这个错误,但我不想使用 Rake 或 Rails。是否可以使用 Ruby 对 Shopify 进行 GraphQL 查询?

ShopifyAPI::GraphQL::InvalidClient at /
Client for API version 2021-04 does not exist because no schema file exists at `shopify_graphql_schemas/2021-04.json`. To dump the schema file, use the `rake shopify_api:graphql:dump` task

【问题讨论】:

    标签: ruby graphql shopify sinatra shopify-app


    【解决方案1】:

    由于错误状态,您需要先转储架构(请参阅此链接:https://github.com/Shopify/shopify_api/blob/master/docs/graphql.md#dump-the-schema)。

    然后您在与您的 ruby​​ 脚本相同的根目录中创建一个 shopify_graphql_schemas 目录,并将生成的 JSON 放在那里。

    就像 cmets 中所述,这需要 Rake 任务,因此您需要使用 Rails。 如果您的项目不使用 Rails,则需要快速解决。

    您创建一个临时的准系统 Rails 项目,然后使用该项目生成转储(您可以在完成后删除该项目)。 这有点 hacky,但这是我能看到的唯一可行的方法。

    【讨论】:

    • 我看到了,但这需要Rake 任务,我认为这需要使用 Rails?
    • 确实如此。如果您的项目没有使用 Rails,那么您必须进行额外的操作。我已经用建议更新了答案。
    猜你喜欢
    • 2019-04-06
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2019-10-22
    相关资源
    最近更新 更多