class ShopifyAPIRetry::GraphQL

Constants

CONVERSION_WARNING

Protected Instance Methods

request() { || ... } click to toggle source
# File lib/shopify_api_retry.rb, line 163
def request
  loop do
    data = og_data = yield

    # Shopify's client does not return a Hash but
    # technically we work with any Hash response
    unless data.is_a?(Hash)
      unless data.respond_to?(:to_h)
        warn CONVERSION_WARNING % "respond_to?(:to_h) is false"
        return og_data
      end

      begin
        data = data.to_h
      rescue TypeError, ArgumentError => e
        warn CONVERSION_WARNING % e.message
        return og_data
      end
    end

    cost = data.dig("extensions", "cost")
    # If this is nil then the X-GraphQL-Cost-Include-Fields header was not set
    # If actualQueryCost is present then the query was not rate limited
    return og_data if cost.nil? || cost["actualQueryCost"]

    handler = handlers["graphql"]
    handler[:wait] ||= sleep_time(cost)

    return og_data unless snooze(handler)
  end
end
sleep_time(cost) click to toggle source
# File lib/shopify_api_retry.rb, line 195
def sleep_time(cost)
  status = cost["throttleStatus"]
  (cost["requestedQueryCost"] - status["currentlyAvailable"]) / status["restoreRate"]# + 0.33
end