Sha256: fd9e562a44aa3be174d5de9e990b06b061e90fcc58ac547e79cd0ec1b1b4f6d8
Contents?: true
Size: 1.57 KB
Versions: 1
Compression:
Stored size: 1.57 KB
Contents
# frozen_string_literal: true module ShopifyApiBruv module Resources module Graphql class Resource < Base attr_reader :client, :variables, :mutation_object_name attr_accessor :query MAX_TRIES = ENV.fetch('SHOPIFY_API_BRUV_REQUEST_MAX_TRIES', 3).to_i SLEEP_TIMER = ENV.fetch('SHOPIFY_API_BRUV_REQUEST_SLEEP_TIMER', 4).to_i def initialize(config:, variables: nil, mutation_object_name: nil) @client = Clients::Graphql::Client.new(config:) @variables = variables @mutation_object_name = mutation_object_name end def request(tries: 0) raise NotImplementedError, 'Please set a query in the derived class' if query.nil? response = client.request(query:, variables:) handle_response(response:, query:, variables:, tries:) end private def handle_response(response:, query:, variables:, tries:) body = response.body handle_response_errors(body:, query:, variables:, tries:) mutation_object_name.nil? ? body['data'] : body.dig('data', mutation_object_name) end def handle_response_errors(body:, query:, variables:, tries:) errors = body['errors'] || body.dig('data', 'errors') raise Errors::ResourceError, errors if tries == MAX_TRIES if errors&.any? { |error| error['message'] == 'Throttled' } sleep(SLEEP_TIMER) request(tries: + 1) end raise Errors::ResourceError, errors unless errors.nil? end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
shopify_api_bruv-0.1.0 | lib/shopify_api_bruv/resources/graphql/resource.rb |